SQL Data Definition Language (DDL) is the foundation for creating and managing database structures. It provides commands to define tables, indexes, and constraints, enabling developers to establish the framework for storing and organizing data efficiently. Understanding DDL is crucial for database design and maintenance. It allows for creating robust schemas, enforcing data integrity, and adapting database structures to evolving requirements, ensuring optimal performance and data consistency in database systems.
CREATE
, ALTER
, and DROP
used to manipulate database objectsCREATE
statement used to create new database objects such as tables, indexes, views, and procedures
ALTER
statement used to modify the structure of existing database objects
DROP
statement used to remove database objects from the database
INT
, VARCHAR
, DATE
, BOOLEAN
, and DECIMAL
CREATE TABLE
statement used to create a new table in the database
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50), salary DECIMAL(10,2))
CREATE INDEX
statement used to create an index on one or more columns of a table
CREATE INDEX idx_employee_name ON employees (name)
CREATE VIEW
statement used to create a virtual table based on the result of a SELECT statement
CREATE VIEW employee_info AS SELECT id, name FROM employees
CREATE PROCEDURE
statement used to create a stored procedure in the database
CREATE TRIGGER
statement used to create a trigger that automatically executes in response to specific database events
CREATE TRIGGER update_employee_log AFTER UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO employee_log VALUES (OLD.id, NEW.salary); END
ALTER TABLE
statement used to modify the structure of an existing table
ALTER TABLE employees ADD COLUMN email VARCHAR(100)
RENAME
statement
ALTER TABLE employees RENAME TO staff
ALTER TABLE
ALTER TABLE employees MODIFY COLUMN salary DECIMAL(12,2)
CREATE INDEX
or DROP INDEX
DROP INDEX idx_employee_name ON employees
ALTER
statements
ALTER VIEW employee_info AS SELECT id, name, email FROM employees
DROP TABLE
statement used to remove a table and all its associated data from the database
DROP TABLE employees
DROP INDEX
statement used to remove an index from a table
DROP INDEX idx_employee_name ON employees
DROP VIEW
statement used to remove a view from the database
DROP VIEW employee_info
DROP PROCEDURE
statement used to remove a stored procedure from the database
DROP PROCEDURE calculate_bonus
DROP TRIGGER
statement used to remove a trigger from the database
DROP TRIGGER update_employee_log
DROP
statement
DROP TABLE employees, departments, locations
INT
, DECIMAL
, FLOAT
, and BIGINT
CHAR
, VARCHAR
, and TEXT
DATE
, TIME
, DATETIME
, and TIMESTAMP
BOOLEAN
or TINYINT
(0 for false, 1 for true)CREATE TABLE employees (id INT PRIMARY KEY, ...)
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(id))
CREATE TABLE users (username VARCHAR(50) UNIQUE, ...)
CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, ...)
CREATE TABLE employees (id INT PRIMARY KEY, age INT CHECK (age >= 18), ...)
Creating a table to store customer information:
CREATE TABLE customers ( id INT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE, phone VARCHAR(20), address VARCHAR(200), city VARCHAR(50), state VARCHAR(50), zip_code VARCHAR(10), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Adding a new column to the customers table:
ALTER TABLE customers ADD COLUMN date_of_birth DATE;
Creating an index on the customers table:
CREATE INDEX idx_customers_last_name ON customers (last_name);
Creating a table with a foreign key constraint:
CREATE TABLE orders ( id INT PRIMARY KEY, customer_id INT, order_date DATE, total_amount DECIMAL(10,2), FOREIGN KEY (customer_id) REFERENCES customers(id) );
Dropping a table:
DROP TABLE orders;
Renaming a column in the customers table:
ALTER TABLE customers RENAME COLUMN phone TO contact_number;
Creating a view to retrieve customer details:
CREATE VIEW customer_details AS SELECT id, first_name, last_name, email, city, state FROM customers;
Modifying a column data type:
ALTER TABLE customers MODIFY COLUMN zip_code VARCHAR(20);