SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
DDL Commands in SQL
DDL is an abbreviation of Data Definition Language.
The DDL Commands in Structured Query Language are used to create and modify the schema of the database and its objects. The syntax of DDL commands is predefined for describing the data. The commands of Data Definition Language deal with how the data should exist in the database.
Following are the five DDL commands in SQL:
- CREATE Command
- DROP Command
- ALTER Command
- TRUNCATE Command
- RENAME Command
CREATE Command
CREATE is a DDL command used to create databases, tables, triggers and other database objects.
Examples of CREATE Command in SQL
Example 1: This example describes how to create a new database using the CREATE DDL command.
Syntax to Create a Database:
Suppose, you want to create a Books database in the SQL database. To do this, you have to write the following DDL Command:
Example 2: This example describes how to create a new table using the CREATE DDL command.
Syntax to create a new table:
(
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
column_Name3 data_type ( size of the column) ,
...
column_NameN data_type ( size of the column )
) ;
Suppose, you want to create a Student table with five columns in the SQL database. To do this, you have to write the following DDL command:
(
Roll_No. Int ,
First_Name Varchar (20) ,
Last_Name Varchar (20) ,
Age Int ,
Marks Int ,
) ;
Example 3: This example describes how to create a new index using the CREATE DDL command.
Syntax to Create a new index:
Let's take the Student table:
Stu_Id | Name | Marks | City | State |
---|---|---|---|---|
100 | Abhay | 80 | Noida | U.P |
101 | Sushil | 75 | Jaipur | Rajasthan |
102 | Ankit | 90 | Gurgaon | Haryana |
103 | Yogesh | 93 | Lucknow | U.P |
Suppose, you want to create an index on the combination of the City and State field of the Student table. For this, we have to use the following DDL command:
Example 4: This example describes how to create a trigger in the SQL database using the DDL CREATE command.
Syntax to create a trigger:
[ BEFORE | AFTER ]
{ INSERT | UPDATE | DELETE }
ON [table_name] ;
DROP Command
DROP is a DDL command used to delete/remove the database objects from the SQL database. We can easily remove the entire table, view, or index from the database using this DDL command.
Examples of DROP Command in SQL
Example 1: This example describes how to remove a database from the SQL database.
Syntax to remove a database:
Suppose, you want to delete the Books database from the SQL database. To do this, you have to write the following DDL command:
Example 2: This example describes how to remove the existing table from the SQL database.
Syntax to remove a table:
Suppose, you want to delete the Student table from the SQL database. To do this, you have to write the following DDL command:
Example 3: This example describes how to remove the existing index from the SQL database.
Syntax to remove an index:
Suppose, you want to delete the index_city from the SQL database. To do this, you have to write the following DDL command:
ALTER Command
ALTER is a DDL command which changes or modifies the existing structure of the database, and it also changes the schema of database objects.
We can also add and drop constraints of the table using the ALTER command.
Examples of ALTER Command in SQL
Example 1: This example shows how to add a new field to the existing table.
Syntax to add a newfield in the table:
Suppose, you want to add the 'Father's_Name' column in the existing Student table. To do this, you have to write the following DDL command:
Example 2: This example describes how to remove the existing column from the table.
Syntax to remove a column from the table:
Suppose, you want to remove the Age and Marks column from the existing Student table. To do this, you have to write the following DDL command:
Example 3: This example describes how to modify the existing column of the existing table.
Syntax to modify the column of the table:
Suppose, you want to change the character size of the Last_Namefield of the Student table. To do this, you have to write the following DDL command:
TRUNCATE Command
TRUNCATE is another DDL command which deletes or removes all the records from the table.
This command also removes the space allocated for storing the table records.
Syntax of TRUNCATE command
Example
Suppose, you want to delete the record of the Student table. To do this, you have to write the following TRUNCATE DDL command:
The above query successfully removed all the records from the student table. Let's verify it by using the following SELECT statement:
RENAME Command
RENAME is a DDL command which is used to change the name of the database table.
Syntax of RENAME command
Example
This query changes the name of the table from Student to Student_Details.