Loading...
Loading...
00:00:00

DDL (Data Definition Language)

SQL Data Definition Language (DDL) commands are used to create, modify, and delete database objects such as tables, views, indexes, and sequences. DDL commands are essential for managing database structures and ensuring that data is stored and retrieved correctly. In this article, we will explain all the commands of SQL DDL in simple English.

  1. CREATE: The CREATE command is used to create a new database object such as a table, view, index, or sequence. The syntax of the CREATE command varies depending on the type of object being created. The syntax for creating a table is as follows:

    CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    ....
    );
    

    For example:

    CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    age INT,
    gender CHAR(1)
    );
    
  2. ALTER: The ALTER command is used to modify the structure of an existing database object such as a table or view. The syntax of the ALTER command varies depending on the type of object being modified. The syntax for modifying a table is as follows:

    ALTER TABLE table_name action;
    

    For example:

    ALTER TABLE students ADD email VARCHAR(50);
    
  3. DROP: The DROP command is used to delete an existing database object such as a table, view, index, or sequence. The syntax of the DROP command varies depending on the type of object being deleted. The syntax for deleting a table is as follows:

    DROP TABLE table_name;
    

    For example:

    DROP TABLE students;
    
  4. TRUNCATE: The TRUNCATE command is used to remove all data from a table. Unlike the DELETE command, which removes individual rows from a table, the TRUNCATE command removes all rows from a table at once. This command is useful when you need to delete all data from a table quickly. The syntax for truncating a table is as follows:

    TRUNCATE TABLE table_name;
    

    For example:

    TRUNCATE TABLE students;
    
  5. COMMENT: The COMMENT command is used to add a comment to a database object such as a table, column, or view. Comments can be used to provide additional information about the object, such as its purpose or how it is used. The syntax for adding a comment to a column is as follows:

    COMMENT ON COLUMN table_name.column_name IS 'comment';
    

    For Example: 

    COMMENT ON COLUMN students.name IS 'Student Name';
    
  6. RENAME: The RENAME command is used to change the name of an existing database object such as a table, column, or view. This command is useful when you need to rename an object to reflect a change in its purpose or function. The syntax for renaming a table is as follows:

    RENAME TABLE old_table_name TO new_table_name;
    

     For Example:

    RENAME TABLE students TO class_students;
    
    The CONSTRAINT command is used to add a constraint to a table. Constraints are used to enforce rules on the data that is stored in a table, such as ensuring that a column only contains unique values or that a column cannot be null.
  7.  PRIMARY KEY: This command is used to define a primary key constraint on a table, which ensures that each row in the table is uniquely identified by a particular column or combination of columns. The syntax for defining a primary key is as follows: 

    ALTER TABLE table_name ADD PRIMARY KEY (column1, column2, ...);
    

    For Example: 

    ALTER TABLE students ADD PRIMARY KEY (id);
    
  8. FOREIGN KEY: This command is used to define a foreign key constraint on a table, which ensures that the values in a column or combination of columns in one table match the values in a primary key column or combination of columns in another table. The syntax for defining a foreign key is as follows:
    ALTER TABLE table_name1
    ADD CONSTRAINT fk_constraint_name
    FOREIGN KEY (column1, column2, ...)
    REFERENCES table_name2 (column1, column2, ...);
    

    For Example: 

    ALTER TABLE orders
    ADD CONSTRAINT fk_customer_id
    FOREIGN KEY (customer_id)
    REFERENCES customers (id);
    
  9. CHECK: This command is used to define a check constraint on a table, which ensures that the values in a column or combination of columns meet a certain condition. The syntax for defining a check constraint is as follows:
    ALTER TABLE table_name
    ADD CONSTRAINT check_constraint_name
    CHECK (condition);
    

    For Example: 

    ALTER TABLE students
    ADD CONSTRAINT ck_age_range
    CHECK (age >= 18 AND age <= 25);
    
  10. INDEX: The INDEX command is used to create an index on a table. Indexes are used to speed up queries by allowing the database to quickly locate the data that is being requested. The syntax for creating an index is as follows:

    CREATE INDEX index_name ON table_name (column1, column2, ...);
    

    For Example: 

    CREATE INDEX idx_age_gender ON students (age, gender);
    
  11. VIEW: The VIEW command is used to create a virtual table that is based on the data from one or more tables. Views are useful when you need to retrieve data from multiple tables in a single query.

  12. SEQUENCE: The SEQUENCE command is used to create a sequence object that generates a sequence of unique numbers. Sequence objects are useful when you need to generate unique identifiers for rows in a table.

In conclusion, SQL DDL commands are used to create, modify, and delete database objects. These commands are essential for managing database structures and ensuring that data is stored and retrieved correctly. By understanding and using these commands, you can effectively manage your database and ensure that it meets the needs of your organization.