Resources | Subject Notes | Computer Science
This section covers Data Definition Language (DDL) and Data Manipulation Language (DML) in SQL. DDL is used to define and manage the structure of the database, while DML is used to manipulate the data within the database.
DDL commands are used to create, modify, and delete database objects such as tables, indexes, and views. Common DDL statements include:
CREATE
: Creates a new database object (e.g., a table).ALTER
: Modifies the structure of an existing database object.DROP
: Deletes a database object.TRUNCATE
: Removes all rows from a table, but the table structure remains.Example DDL Script:
SQL Statement | Description |
---|---|
|
Creates a table named 'Customers' with columns for customer information. |
|
Creates a table named 'Orders' with columns for order information, including a foreign key referencing the 'Customers' table. |
DML commands are used to manage the data stored in the database. Common DML statements include:
SELECT
: Retrieves data from one or more tables.INSERT
: Adds new data into a table.UPDATE
: Modifies existing data in a table.DELETE
: Removes data from a table.Example DML Script:
Consider the Customers
and Orders
tables created above.
Retrieving all customers:
SQL Statement | Description |
---|---|
|
Retrieves all columns and all rows from the 'Customers' table. |
|
Retrieves the first name and last name of the customer with the email 'john.doe@example.com'. |
Adding a new customer:
SQL Statement | Description |
---|---|
|
Inserts a new row into the 'Customers' table with the specified values. |
Updating a customer's email:
SQL Statement | Description |
---|---|
|
Updates the email address of the customer with CustomerID 1. |
Deleting a customer:
SQL Statement | Description |
---|---|
|
Deletes the customer with CustomerID 1 from the 'Customers' table. |
These examples demonstrate basic DDL and DML operations. SQL provides a powerful and flexible way to manage and manipulate data within databases.