Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

SQL Fundamentals

Introduction

Structured Query Language (SQL) is a standardized programming language used to manage and manipulate relational databases. SQL enables users to create, read, update, and delete data stored in databases.

Key Concepts

  • Relational Database: A type of database that stores data in tables.
  • Table: A collection of related data entries consisting of rows and columns.
  • Row: A single record in a table, also called a tuple.
  • Column: A set of data values of a particular type within a table.
  • Primary Key: A unique identifier for each record in a table.
  • Foreign Key: A column that creates a relationship between two tables.

SQL Commands

SQL commands are categorized into several types:

  • Data Query Language (DQL): Used to query the database (e.g., SELECT).
  • Data Definition Language (DDL): Used to define or modify database structure (e.g., CREATE, ALTER, DROP).
  • Data Manipulation Language (DML): Used to manipulate data (e.g., INSERT, UPDATE, DELETE).
  • Data Control Language (DCL): Used to control access to data (e.g., GRANT, REVOKE).
  • Example SQL Queries

    -- Create a new table
    CREATE TABLE Employees (
        EmployeeID INT PRIMARY KEY,
        FirstName VARCHAR(50),
        LastName VARCHAR(50),
        HireDate DATE
    );
    
    -- Insert a new record
    INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate)
    VALUES (1, 'John', 'Doe', '2023-01-15');
    
    -- Select records
    SELECT * FROM Employees;

    Best Practices

    When working with SQL, consider the following best practices:

    • Use meaningful table and column names.
    • Always back up your database before making significant changes.
    • Use transactions for critical operations to maintain data integrity.
    • Regularly review and optimize your queries for performance.
    • Limit the use of SELECT * to improve efficiency.

    FAQ

    What is a primary key?

    A primary key is a unique identifier for each record in a database table. It ensures that no two rows have the same value in that column.

    Can SQL be used to manipulate data?

    Yes, SQL is specifically designed to manipulate data in relational databases through commands like INSERT, UPDATE, and DELETE.

    What is the difference between DDL and DML?

    DDL (Data Definition Language) is used to define and manage all database objects, while DML (Data Manipulation Language) is used for managing data stored in the database.

    Flowchart of SQL Commands

    graph TD;
                A[Start] --> B[Identify the database];
                B --> C{Type of SQL Command};
                C -->|DDL| D[Define/Modify Structure];
                C -->|DML| E[Manipulate Data];
                C -->|DQL| F[Query Data];
                D --> G[Create/Alter/Drop];
                E --> H[Insert/Update/Delete];
                F --> I[Select];
                G --> J[End];
                H --> J;
                I --> J;