22
DecData Definition Language (DDL) in DBMS: Structuring Your Database
Data Definition Language (DDL) in DBMS
Data Definition Language (DDL) in DBMS is a component of SQL (Structured Query Language). DDL in DBMS is utilized in managing database systems (DBMS) to define and oversee all the elements within a database. DDL commands are employed to create, alter, and remove database configurations without affecting the data. Through the application of DDL commands, database frameworks can be Upheld, guaranteeing the arrangement and format of information stored in the database.
In this DBMS Tutorial, we will learn deep down what is the Data Definition Language(DDL), what are DDL commands, the types of DDL commands, create commands, alter commands, update commands, truncate commands, comment commands, and many more.
What is Data Definition Language in DBMS?
In an easy way, Data Definition Language (DDL) is a bunch of SQL commands used to explain and customize the structure of a database management system, such as tables, indexes, and schemas. It provides several commands to the user by which you can operate the database, such as creating commands, altering commands, and updating commands. Let us learn about Data Definition Language Commands first.
What are Data Definition Language(DDL) Commands in DBMS?
The structure of database objects is defined and managed using DDL (Data Definition Language) commands. This is a succinct explanation:
- CREATE: Generates new indexes and tables in a database.
- ALTER: Changes an object's existing structure.
- DROP: Permanently deletes database objects.
- TRUNCATE: A table's structure is preserved when all of its rows are removed.
- RENAME: Changes the name of tables or columns in a database.
You have four types of DDL commands that are:
- Create Command
- Alter COmmand
- Drop Command
- Truncate Command
- Comment Command
- Rename Command
If you are using the DDL commands, you should remember that DDL commands don't change or experiment with the data that is stored in the database.
1. CREATE Command
You can use the Create Command in the SQL to create the table, create the index, and create the view. These SQL commands result in the newly formatted table.
Create Command Syntax
You can use this syntax to make a table in the database:
CREATE TABLE table_name
(
column_1 datatype,
column_2 datatype,
column_3 datatype,
....
);
Example
By following the above syntax, we are creating a table of Students.
CREATE TABLE Students(
stud_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
admission_date DATE
);
After running the above command, it generates the table of Students:
Students Table:
stud_id | first_name | last_name | admission_date |
2. ALTER Command
The following database objects can be reorganized by adding, removing, or changing table columns using the Alter command.
Alter Command Syntax
You can use Alter Command syntax to alter a table in the database:
ALTER TABLE table_name
ADD column_name datatype;
Example
Here, we will add a new column in the above-made 'Students' table by using the Alter syntax.
ALTER TABLE students ADD COLUMN email VARCHAR(100);
After running the above command, it adds a new column in the table of Students:
Student Table:
stud_id | first_name | last_name | admisssion_date |
3. DROP Command
You can use the DROP command to permanently delete existing database objects.
DROP Command Syntax
You can use the drop Command syntax to delete an object in the database:
DROP TABLE table_name;
Example
Here, you can delete the above Student table by using the above Drop Commands.
After using the DROP command, the above Students table will be deleted.
4. TRUNCATE Command
You can use the TRUNCATE command to delete all the existing rows in a table without deleting the table itself. It resets any auto-incremented columns.
TRUNCATE Command Syntax
You can use the TRUNCATE Command syntax to delete all the rows in the database:
TRUNCATE TABLE table_name;
Example
Suppose we have the Students table, and we want to delete all the rows from the table.
Students Table:
stud_id | first_name | last_name | admission_date |
001 | Aman | Mishra | 16-06-2017 |
002 | Ankita | Dubey | 25-06-2017 |
003 | Shailesh | Ojha | 27-06-2017 |
TRUNCATE TABLE Students;
After performing the above query, all the rows of the Students table will be deleted.
Students Table:
stud_id | first_name | last_name |
5. RENAME Command
You can use the RENAME command to change the name of the table.
Rename Commands Syntax
Here is the syntax for changing the name of the table by using RENAME commands:
Rename <OLD_TABLENAME> to <NEW_TABLENAME>;
Example
You can use Rename commands to change the 'Students' table to the 'Student_details' table.
RENAME Students to Student_details;
After running the above query, the renamed table is Student_details.
Student_details
stud_id | first_name | last_name | admission_date |
6. COMMENT Command
We use the COMMENT commands to write the comments inside the SQL queries. These comments, which are meant for documentation purposes only, can help developers and database administrators understand the function or application of particular database objects.
There are two ways to write comments on SQL queries that are:
- Single-Line Comments
- Multi-Line Comments
1. Single-Line Comments
We are using the (--) operator to add single-line comments in the SQL query.
Example
2. Multi-Line Comments
We can use “/ ** /” to write the multi-line comments in the SQL query.
Example
Best Practices of using Data Definition Language(DDL)
Here, we will discuss the best practice of using DDL commands
1. Creating Database Objects
We can use the Data Definition Language for creating different types of database objects:
1. Creating Databases: Creating a new database involves defining the database name and establishing its initial structure.
CREATE DATABASE company_db;
2. Creating Tables: Creating tables involves defining the table's columns, data types, and constraints.
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);
3. Creating Indexes: Indexes are created to enhance the performance of data retrieval operations.
CREATE INDEX idx_employee_name ON employees (last_name);
2. Modifying Database
We can use the Data Definition Language to modify the database.
1. Objects Altering Tables: Altering tables allows for adding, dropping, or modifying columns and constraints.
ALTER TABLE employees MODIFY COLUMN email VARCHAR(255);
2. Renaming Database: Objects Renaming database objects involves changing the name of existing objects.
ALTER TABLE employees RENAME TO staff;
3. Deleting Database
We can also use the DDL to delete the database.
1. Dropping Tables and Databases: Dropping tables and databases removes the objects and their data permanently.
DROP DATABASE company_db;
DROP TABLE departments;
2. Truncating Tables: Truncating tables removes all data from a table quickly without affecting the table structure.
TRUNCATE TABLE departments;
Constraints in Data Definition Language
There are seven types of SQL Constraints in DDL that are:
1. NOT NULL: Ensures that a column cannot have NULL values.
Example
CREATE TABLE Employees (
employee_id INT NOT NULL,
employee_name VARCHAR(100) NOT NULL
);
2. UNIQUE: Guarantees that all values in a column are distinct.
Example
CREATE TABLE Employees (
employee_id INT UNIQUE,
employee_email VARCHAR(100) UNIQUE
);
3. PRIMARY KEY: Combines NOT NULL and UNIQUE, ensuring each row is uniquely ident
Example
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100) NOT NULL
);
4. FOREIGN KEY: Connecting a column to the primary key of another table ensures referential integrity.
Example
CREATE TABLE Departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);
5. CHECK: Verifies if values in a column satisfy certain requirements.
Example
CREATE TABLE Employees (
employee_id INT,
employee_age INT CHECK (employee_age >= 18)
);
6. DEFAULT: When a column has no value entered, a default value is assigned to it.
Example
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
employee_status VARCHAR(20) DEFAULT 'Active'
);
7. AUTO_INCREMENT: Produces distinct values for a column automatically; this column is usually used for primary keys.
Example
CREATE TABLE Employees (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
employee_name VARCHAR(100)
);
Difference between DDL and DML
Factors | Data Definition Language(DDL) | Data Manipulation Language(DML) |
Purpose | Defines and manages database schema/structure. | Manipulates the data within the database. |
Operations | CREATE, ALTER, DROP, TRUNCATE, RENAME. | SELECT, INSERT, UPDATE, DELETE. |
Effect on Data | Does not directly work with the data; it works with the database schema (structure). | Directly affects the data stored in the tables (insert, modify, delete data). |
Effect on Schema | Alters or defines the schema (table, database, or object structure). | Does not alter the database schema; it only works with the data. |
Transaction Control | Usually auto-committed (changes are permanent immediately). | Changes can be rolled back if they are part of the transaction. |
Examples | Creating a new table, altering the structure of a table. | Inserting new records, updating existing records, deleting records. |
Commands | CREATE TABLE, ALTER TABLE, DROP DATABASE, etc. | INSERT INTO, UPDATE, DELETE FROM, SELECT, etc. |
Read More: |
Top 50 DBMS Viva Questions and Answers |
DBMS Interview Questions For Freshers |
Conclusion
FAQs
DML – Data Manipulation Language.
DCL – Data Control Language. TCL – Transaction Control Language.