DDL Commands Overview
Introduction
DDL stands for Data Definition Language and it is used to define and modify the structure of the database objects. DDL commands are used to create, alter, and drop database tables, indexes, views, and other database objects.
In this article, we will cover the syntax, example, output, explanation, use, important points and summary of the most common DDL commands.
Syntax
The general syntax of DDL commands is as follows:
COMMAND_NAME [OBJECT_TYPE] OBJECT_NAME [(COLUMN_NAME DATA_TYPE [CONSTRAINTS])];
- COMMAND_NAME: It is the name of the DDL command, such as CREATE, ALTER, or DROP.
- OBJECT_TYPE: It is the type of database object like TABLE, VIEW, INDEX, etc.
- OBJECT_NAME: It is the name of the database object.
- COLUMN_NAME: It is the name of the column in the table.
- DATA_TYPE: It is the data type of the column.
- CONSTRAINTS: It defines constraints on the column.
Example
CREATE TABLE
CREATE TABLE employees (
emp_id INT NOT NULL,
emp_name VARCHAR(50),
emp_salary DECIMAL(10,2),
emp_join_date DATE
);
ALTER TABLE
ALTER TABLE employees ADD emp_address VARCHAR(200);
DROP TABLE
DROP TABLE employees;
Output
The output of DDL commands is not the same for all commands. For example, the output of the CREATE TABLE command is the message "Table created successfully" while the output of the ALTER TABLE command is "Table altered successfully". The DROP TABLE command does not produce any output.
Explanation
CREATE TABLE
The CREATE TABLE command is used to create a new table in the database. We can specify the column names, data type, and constraints while creating a table.
ALTER TABLE
The ALTER TABLE command is used to modify an existing table in the database. We can add, delete, or modify a column in the table.
DROP TABLE
The DROP TABLE command is used to delete an existing table from the database.
Use
DDL commands are used to build the database schema. By using DDL commands, we can create and modify tables, indexes, views, and other schema objects.
Important Points
- DDL commands are used to define and modify the structure of the database objects.
- The CREATE TABLE command is used to create a new table in the database.
- The ALTER TABLE command is used to modify an existing table in the database.
- The DROP TABLE command is used to delete an existing table from the database.
Summary
DDL commands are very important for building and modifying the database schema. In this article, we have discussed the syntax, example, output, explanation, use, important points and summary of the most common DDL commands.