Data Analyst Full Course for Beginners | Sql Part 3

SQL (Structured Query Language) is the standard language used to manage and manipulate relational databases. Whether you’re a beginner stepping into the world of databases or an experienced developer, understanding SQL commands is essential. In this article, we will explore the different types of SQL commands and their functions to help you effectively manage your database operations.

 


What Are SQL Commands?

SQL commands are standardized instructions used to interact with a database. These commands help in creating, managing, and manipulating data stored in a relational database system. Let’s dive into the various types of SQL commands.


Types of SQL Commands

  1. DDL (Data Definition Language) DDL commands define and manage the structure of the database. They are used to create, alter, or delete database objects like tables, schemas, and indexes.

    Common DDL Commands:

    • CREATE: Used to create a new table, database, or index.

    • ALTER: Used to modify the structure of an existing table or database.

    • DROP: Deletes an entire table or database.

    • TRUNCATE: Removes all records from a table but retains its structure.

    Example:

    CREATE TABLE Employees (
        ID INT PRIMARY KEY,
        Name VARCHAR(50),
        Department VARCHAR(50)
    );
  2. DML (Data Manipulation Language) DML commands are used to manage data within database tables. They help in inserting, updating, or deleting data.

    Common DML Commands:

    • INSERT: Adds new records to a table.

    • UPDATE: Modifies existing records in a table.

    • DELETE: Removes specific records based on a condition.

    Example:

    INSERT INTO Employees (ID, Name, Department)
    VALUES (1, 'John Doe', 'HR');
  3. DQL (Data Query Language) DQL commands are primarily used to retrieve data from a database. The main command under this category is SELECT.

    Common SELECT Clauses:

    • WHERE: Filters records based on a condition.

    • ORDER BY: Sorts the result in ascending or descending order.

    • GROUP BY: Groups records based on a column.

    • HAVING: Filters groups based on a condition.

    Example:

    SELECT Name, Department
    FROM Employees
    WHERE Department = 'HR';
  4. TCL (Transaction Control Language) TCL commands manage transactions in a database, ensuring the integrity of operations.

    Common TCL Commands:

    • COMMIT: Saves all changes made during the current transaction.

    • ROLLBACK: Reverts changes made during the current transaction.

    • SAVEPOINT: Sets a point within a transaction to roll back to later.

    Example:

    BEGIN TRANSACTION;
    UPDATE Employees
    SET Department = 'IT'
    WHERE ID = 1;
    SAVEPOINT Update1;
    ROLLBACK TO Update1;
  5. DCL (Data Control Language) DCL commands control access to data in a database by granting or revoking permissions.

    Common DCL Commands:

    • GRANT: Gives specific permissions to users.

    • REVOKE: Removes previously granted permissions.

    Example:

    GRANT SELECT, INSERT ON Employees TO User1;
  6. Utility Commands Utility commands perform database operations like backups, performance tuning, and schema analysis.

    Common Utility Commands:

    • EXPLAIN: Analyzes the execution plan for a query.

    • SHOW: Displays database information.

    • DESCRIBE: Displays the structure of a table.

    Example:

    DESCRIBE Employees;

Key SQL Data Types

When defining tables in SQL, it’s crucial to specify the type of data that each column will store. Below are some common data types:

  1. INTEGER (INT): Stores whole numbers (e.g., 10, -50).

  2. DECIMAL: Stores fixed-point numbers with precision and scale (e.g., 123.45).

  3. VARCHAR: Stores variable-length strings (e.g., ‘John’).

  4. CHAR: Stores fixed-length strings.

  5. BOOLEAN: Stores true/false values.

  6. DATE: Stores dates in the format YYYY-MM-DD.

  7. DATETIME: Stores both date and time.

  8. TIMESTAMP: Stores date and time with time zone information.


Constraints in SQL

Constraints are rules enforced on data to ensure its integrity and accuracy. Here are some commonly used constraints:

  1. NOT NULL: Ensures that a column cannot have a null value.

  2. UNIQUE: Ensures that all values in a column are unique.

  3. PRIMARY KEY: A combination of NOT NULL and UNIQUE. Uniquely identifies each record in a table.

  4. FOREIGN KEY: Establishes a relationship between two tables.

Example:

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

Conclusion

SQL commands are the backbone of database management. Understanding these commands allows you to efficiently create, manage, and query databases. Whether you’re defining the structure with DDL, manipulating data with DML, or ensuring data security with DCL, mastering SQL is a vital skill for anyone working with databases.

Start practicing these commands today and take a step closer to becoming a database expert!