Data Definition Language (DDL) is a subset of SQL (Structured Query Language) used for defining, managing, and modifying the structure of a database. DDL commands are primarily concerned with creating, altering, and deleting database objects like tables, indexes, constraints, and views. They do not deal with the actual data stored in the database, but rather with the metadata that defines the database schema.
Here are the main parts and types of DDL commands, along with examples:
1. CREATE:
The CREATE statement is used to define and create new database objects, such as tables, indexes, and views.
CREATE TABLE:
This command is used to create a new table with specified columns and data types
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);
CREATE INDEX:
This command is used to create an index on one or more columns to improve query performance.
CREATE INDEX idx_last_name ON employees(last_name);
ALTER:
The ALTER statement is used to modify existing database objects, such as tables or views.
ALTER TABLE:
This command can be used to add, modify, or delete columns within an existing table.
ALTER TABLE employees
ADD email VARCHAR(100);
DROP:
The DROP statement is used to delete database objects like tables, indexes, or views.
DROP TABLE:
This command deletes an entire table and its data. Be cautious when using it, as the data will be permanently lost.
DROP TABLE employees;
TRUNCATE:
The TRUNCATE statement is used to remove all rows from a table while retaining the table structure. It's faster and more efficient than DELETE for removing all data from a table.
TRUNCATE TABLE:
This command removes all rows from a table.
0 Comments