Data Definition Language (DDL)
A subset of SQL used to define and manage the structure of a database.
- A DDL includes commands that create, modify, and delete database objects such as:
- Tables
- Indexes
- Schemas
- DDL is distinct from Data Manipulation Language (DML), which handles the manipulation of data within the database, such as inserting, updating, or deleting records.
Basic DDL Commands
- CREATE: Defines new database objects.
- ALTER: Modifies existing database objects.
- DROP: Deletes database objects.
- TRUNCATE: Removes all rows from a table but retains its structure for future use.
- A table can be created using DDL as follows:
- CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(50), Age INT, EnrollmentDate DATE),
- This command creates a table named Students with four columns: StudentID, Name, Age, and EnrollmentDate.
- The following command below adds a new column Email to the existing Students table.
- ALTER TABLE Students
- ADD Email VARCHAR(100);
Importance of DDL in a Data Model
Defines the Structure of the Database
- DDL translates the conceptual data model (e.g., ER diagrams) into a physical schema that the database can understand and manage.
- It ensures that the database structure aligns with the logical design created during the modeling phase.
Enforces Data Integrity
- DDL ensures that data adheres to predefined rules, maintaining accuracy and consistency through constraints such as:
- PRIMARY KEY
- FOREIGN KEY
- UNIQUE
- CHECK