Data Definition Language (DDL) is a type of database language that is used to define and manage the schema or structure of a database. Here is a full guide on DDL:
- CREATE: This command is used to create a new table, view, or other database objects. The syntax for creating a table is as follows: - CREATE TABLE table_name ( column1 datatype [constraint], column2 datatype [constraint], ... );- Example: - CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), age INT, salary DECIMAL(10,2) );
- ALTER: This command is used to modify the structure of an existing database object. The syntax for altering a table is as follows: - ALTER TABLE table_name ADD column_name datatype [constraint];- Example: - ALTER TABLE employees ADD email VARCHAR(50);
- DROP: This command is used to delete an existing database object. The syntax for dropping a table is as follows: - DROP TABLE table_name;- Example: - DROP TABLE employees;
- TRUNCATE: This command is used to delete all the data from an existing table. The syntax for truncating a table is as follows: - TRUNCATE TABLE table_name;- Example: - TRUNCATE TABLE employees;
- COMMENT: This command is used to add a comment to a database object. The syntax for commenting a table is as follows: - COMMENT ON TABLE table_name IS 'Comment Text';- Example: - COMMENT ON TABLE employees IS 'This table stores information about employees';
- INDEX: This command is used to create an index on a table. The syntax for creating an index is as follows: - CREATE INDEX index_name ON table_name (column1, column2, ...);- Example: - CREATE INDEX emp_name_idx ON employees (name);
These are some of the most common commands used in DDL. By using these commands, you can create, modify, and delete database objects in a structured manner.