SQL (Structured Query Language) comprises two main categories of statements: DDL (Data Definition Language) and DML (Data Manipulation Language). These two categories serve different purposes in managing and working with relational databases:
*DDL (Data Definition Language):*
The purpose of DDL statements is to define, modify, and manage the structure of a database, including tables, schemas, constraints, indexes, and other database objects. Key DDL statements and their purposes include:
1. *CREATE:* DDL statements like `CREATE TABLE`, `CREATE INDEX`, and `CREATE SCHEMA` are used to define new database objects. For example, `CREATE TABLE` defines a new table with its columns, data types, and constraints.
2. *ALTER:* DDL statements like `ALTER TABLE` are used to modify the structure of existing database objects. You can use `ALTER TABLE` to add, modify, or drop columns, change data types, and apply constraints.
3. *DROP:* DDL statements like `DROP TABLE`, `DROP INDEX`, and `DROP SCHEMA` are used to delete or remove database objects. For example, `DROP TABLE` deletes an entire table and its data.
4. *TRUNCATE:* The `TRUNCATE TABLE` statement is used to quickly remove all data from a table, effectively resetting it, but the table structure remains intact.
5. *CREATE/ALTER/DROP SCHEMA:* These statements are used to define, modify, or delete database schemas, which help organize database objects into logical groups.
6. *CREATE/ALTER/DROP CONSTRAINT:* These statements define, modify, or delete constraints, such as primary keys, foreign keys, and check constraints, which enforce data integrity rules.
*DML (Data Manipulation Language):*
DML statements are used to manipulate the data stored in the database. They are responsible for querying, inserting, updating, and deleting data. Key DML statements and their purposes include:
1. *SELECT:* The `SELECT` statement is used to retrieve data from one or more tables. It allows you to specify the columns to retrieve, filter data, and perform various data retrieval operations like sorting and grouping.
2. *INSERT:* The `INSERT` statement is used to add new rows of data to a table. You provide values for each column in the new row, either explicitly or through a subquery.
3. *UPDATE:* The `UPDATE` statement is used to modify existing data in a table. You specify the columns to update and the new values for those columns, along with a condition that determines which rows to update.
4. *DELETE:* The `DELETE` statement is used to remove one or more rows from a table. You specify a condition to determine which rows to delete.
5. *MERGE:* The `MERGE` statement combines `INSERT`, `UPDATE`, and `DELETE` operations into a single statement, making it useful for performing "upsert" (update or insert) operations.
The DML statements enable you to interact with and manipulate the data stored in the database. DDL statements, on the other hand, provide the means to define and manage the database's structure, including tables, constraints, and indexes. Together, DDL and DML statements allow you to create, modify, query, and maintain the data within a relational database.
#sql