SQL Certification - SPLessons

SQL Certification DDL Commands

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL Certification DDL Commands

DDL Commands

shape Description

SQL DDL commands are used to define data in database server. DDL statements explains, how the pattern of data should be there in the database schema table. SQL DDL statements are classified into the following types:

Create Table

shape Description

Create table articulation is utilized to make a table in the database server. And each table in the database should contain unique name and these tables are organised into rows and columns, where appropriate data or values should be inserted. And the column parameters specifies the name of the columns of the table and the datatypes parameters specifies the type of data that the column is holding that may vary from varchar, decimal, date and integers. 

shape Syntax

Syntax for SQL Create table:
Create table <table_name>(<Column_name1>data_type(size),<Column_name2>data_type(size),<Column_name3> data_type(size));
Table name => Any accurate table in the database. Column name =>The operation that can be performed on a  column in the table.

shape Examples

The below example describes the creation of a table in the database. [c]sql> Create table employee15(emp_id int,ename varchar(255),job varchar(255),salary int); Query OK, 0 rows affected (0.64 sec) [/c] In the above example, the table employee15 has been successfully created by specifying the datatypes and size.

Alter Table

shape Description

SQL Alter Table statement is used to add, modify, or delete a columns from the existing table. Alter Table command performs adding of column or modifying column size and is divided into two types.
  • Add
  • Modify

Add

shape Description

Using add command new columns can be included in the current SQL Alter Table.

shape Syntax

Alter table<table_name> ADD(<column_name1> datatype(size),<column_name2> datatype(size)); Table_name => Any accurate table name. Column_name =>The operation that can be performed on a column in the table.

shape Examples

By viewing the below example, the table can be altered by utilizing alter adding command. [c]sql> alter table student1 add(fee number(7,2)); Query OK, 0 rows affected (0.64 sec) Records: 0 Duplicates: 0 Warnings: 0 sql>desc student1; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | stu_id | int(11) | YES | | NULL | | | stu_name | varchar(255) | YES | | NULL | | | stu_result | varchar(255) | YES | | NULL | | | branch | varchar(255) | YES | | NULL | | | fee | int(11) | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)[/c] The above example tells that, how to include new column_name fee with size in  a table.

Modify

shape Description

The structure of an existing table object can be modified using modify command.

shape Syntax

Alter table modify( datatype(size));
Table_name => Any accurate table. Column_name =>The operation that can be performed on a column in the table.

shape Examples

By viewing the below example, the concept of modify command can be easily understood. [c]sql> desc student1; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | stu_id | int(11) | YES | | NULL | | | stu_name | varchar(255) | YES | | NULL | | | stu_result | varchar(255) | YES | | NULL | | | branch | varchar(250) | YES | | NULL | | | fee | int(11) | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) sql> alter table student1 modify branch varchar2(20); Query OK, 0 rows affected (0.50 sec) Records: 0 Duplicates: 0 Warnings: 0 sql> desc student1; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | stu_id | int(11) | YES | | NULL | | | stu_name | varchar(255) | YES | | NULL | | | stu_result | varchar(255) | YES | | NULL | | | branch | varchar(255) | YES | | NULL | | | fee | int(11) | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)[/c] The above example tells that, how to Modify the column_name branch with size in a table.

Drop Table

shape Description

Drop Table command can be performed on existing database. Indexes, tables, and databases can easily be erased/evacuated with the DROP statement. The following are the actions performed by drop command.
  • Dropping a section from the table.
  • Dropping a table from the database.

Dropping a column from the table

shape Syntax

Alter table drop column;
Table_name => Any accurate table. Column_name =>The operation that  can be performed on a column in the table.

shape Examples

By viewing the below example, the concept of drop command can be understood easily. [c]sql> desc student1; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | stu_id | int(11) | YES | | NULL | | | stu_name | varchar(255) | YES | | NULL | | | stu_result | varchar(255) | YES | | NULL | | | branch | varchar(255) | YES | | NULL | | | fee | int(11) | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) sql> alter table student1 drop column fee; Query OK, 0 rows affected (0.53 sec) Records: 0 Duplicates: 0 Warnings: 0 sql> desc student1; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | stu_id | int(11) | YES | | NULL | | | stu_name | varchar(255) | YES | | NULL | | | stu_result | varchar(255) | YES | | NULL | | | branch | varchar(255) | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)[/c] The above example tells that, how to drop a column_name fee from the table.

Dropping a table from the database

shape Syntax

drop table <table name>;

shape Examples

By viewing the below example, the concept of SQL Drop Table command can be understood easily.
[c]sql> desc student1; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | stu_id | int(11) | YES | | NULL | | | stu_name | varchar(255) | YES | | NULL | | | stu_result | varchar(255) | YES | | NULL | | | branch | varchar(255) | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) sql>drop table student1; Query OK, 0 rows affected (0.48 sec) sql> select * from student1; ERROR 1146 (42S02): Table 'employee.student1' doesn't exist[/c] The above example tells that, how to drop all the columns from the table student1. SQL Drop Table is used to drop table from a database. It removes the table from the database schema, there is no way to recover the data after execution of this statement on the table.

SQL Truncate Table

shape Description

SQL is a non-procedural dialect. Client can compose SQL script to execute and SQL compiler consequently produces a method to get to database and convey the fancied output. SQL is a database coding, planned for the recuperation and organisation of data in social database. SQL Truncate command is utilized to erase all rows from the current table in the database."SQL Truncate Table" command shows how to truncate the rows in a table.

shape Syntax

Truncate table <table_name>; Table_name => Any accurate table.

shape Examples

By viewing the below example, the concept of truncate command can be understood easily. [c]sql> select * from employee20; +--------+--------+-----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+-----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | | 1005 | winni | marketing | 14500 | NULL | +--------+--------+-----------+-------+------------+ 5 rows in set (0.00 sec) sql> truncate table employee20; Query OK, 0 rows affected (0.37 sec)[/c] The above example tells that, how to delete all the existing data from the table employee20.

Rename

shape Description

Rename command of MySQL DDL Commands will rename the existing table name. i.e. rename old to new table name.

shape Syntax

Rename old <table_name> to new <table_name>;
table_name => Any accurate table.

shape Examples

By viewing the below example, the concept of rename command can be understood easily. [sql]sql> select * from employee22; +--------+--------+-----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+-----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | | 1005 | winni | marketing | 14500 | NULL | +--------+--------+-----------+-------+------------+ 5 rows in set (0.00 sec) sql> rename table employee22 to employee20; Query OK, 0 rows affected (0.24 sec) sql> select * from employee20; +--------+--------+-----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+-----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | | 1005 | winni | marketing | 14500 | NULL | +--------+--------+-----------+-------+------------+ 5 rows in set (0.00 sec)[/sql] The above example tells that, how to Rename previous table name to new table name. i.e, from employee22 to employee20. 

Truncate Table

shape Description

SQL is a non-procedural dialect. Client can compose SQL script to execute and SQL compiler consequently produces a method to get to database and convey the fancied output. SQL is a database coding, planned for the recuperation and organisation of data in social database. Truncate command is utilized to erase all rows from the current table in the database."SQL Truncate Table" command shows how to truncate the rows in a table.

shape Syntax

Truncate table <table_name>; Table_name => Any accurate table.

shape Examples

By viewing the below example, the concept of truncate command can be understood easily. [c]sql> select * from employee20; +--------+--------+-----------+-------+------------+ | emp_id | ename | job | sal | comisssion | +--------+--------+-----------+-------+------------+ | 1001 | kate | manager | 14000 | 10 | | 1002 | jack | scaleman | 13000 | 20 | | 1003 | maddie | business | 14000 | 30 | | 1004 | madd | business | 14000 | NULL | | 1005 | winni | marketing | 14500 | NULL | +--------+--------+-----------+-------+------------+ 5 rows in set (0.00 sec) [/c] The above example tells that, how to delete all the existing data from the table employee20.

Summary

shape Key Points

  • Create table - Is utilized to make a table in the SQL Database by indicating the datatype and size
  • Alter Table - Alter table will add or modify the structure of the table.
  • Drop Table - SQL Drop Table will drop a column from the table and drop table will drop the entire table.
  • Rename table - Will rename old table name to new table.
  • Truncate Table - Truncate table is utilized to erase all the lines from the table.