Data Definition Language (DDL)

DDL Commands:

These Commands are used to define and modify the structure of a database and its objects, such as tables, indexes, views, and constraints.

Most popular Commands are:

  • CREATE
  • ALTER
  • DROP
  • TRUNCATE

CREATE Table:

This command is used to create a new table with specified columns and data types.

Syntax:

CREATE TABLE table_name (

    column1 data_type,

    column2 data_type,

    — …

);

Example:

 

 

 

 

 

 

 

 

 

DROP Table:

This command is used to Deletes a table and its data from Existing Database.

Syntax:

DROP Table table_name;

Example:

DROP Table Employee;

TRUNCATE Table:

This command is used to delete the data inside a table but not table structure.

Syntax:

TRUNCATE Table table_name;

ALTER TABLE:

This command is used to add, delete, and modify columns in an existing table.

Syntax:

ALTER Table table_name

ADD column_name  datatype;

Example:

ALTER Table Employee

ADD Email varchar(50);

ALTER TABLE – DROP Column:

To Drop a column Email from Employee Table use the following syntax:

ALTER Table Employee

DROP Column Email;

Alter Table –  Rename Column:

To rename a column in a Table, use the following syntax:

ALTER Table table_name

RENAME Column old_name to new_name;

 

ALTER TABLE – Modify Datatype:

MYSQL:

ALTER TABLE table_name

MODIFY Column column-name datatype;

 

SQL Server:

ALTER TABLE table_name

ALTER Column column-name datatype;

 

Related Posts

Join Our Newsletter