Data definition commands are used to create, modify and remove database objects such as schemas, tables, views, indexes etc.
Common Data Definition commands −
The main use of create command is to create a new table in database. It has a predefined syntax in which we specify the columns and their respective data types.
CREATE TABLE <TABLE NAME> ( <COLUMN NAME> <DATA TYPE>, <COLUMN NAME> <DATA TYPE>, <COLUMN NAME> <DATA TYPE>, <COLUMN NAME> <DATA TYPE> );
Create a student table with columns student name and roll number.
CREATE TABLE STUDENT (STUDENT_NAME VARCHAR(30), ROLL_NUMBER INT );
An existing database object can be modified using the alter command. Alter command can do following changes to any table-
Add new columns.
Add new integrity constraints.
Modify existing columns.
Drop integrity constraints.
General Syntax of the ALTER command is mentioned below −
For adding a new column
ALTER TABLE <table_name> ADD <column_name>
For renaming a table
ALTER TABLE <table_name> RENAME To <new_table_name >
For modifying a column
ALTER TABLE <table_name> MODIFY <column_name > <data type >
For deleting a column
ALTER TABLE <table_name> DROP COLUMN <column_name>
This command can delete an index, table or view. Basically, any component from a relational database management system can be removed using the Drop command. Once the object is dropped, it cannot be reused.
The general syntax of drop command is as follows −
DROP TABLE <table_name>; DROP DATABASE <database_name>; DROP TABLE <index_name>;
Using the truncate command, all the records in a database are deleted, but the database structure is maintained.
TRUNCATE TABLE <table name>
This command is used to add comments to the data dictionary.
The rename command renames an object
Rename <old name> to <new name>