You are on page 1of 6

Experiment 2

Aim: To study  implementation of DDL commands of SQL with suitable examples

1) Create  table 2) Describe table 3) Alter table 4) Drop table 5) Rename  table

Software Required: Microsoft SQL server, Virtual SQL Server Lab

Theory:

                DDL (Data Definition Language ) Commands:-

Data definition language is used to define the basic structure of database. With this
definition user can define the integrity constraints, assertions etc. Execution of the
DDL statements result into the structure definition and other related operations. There
are different commands of DDL. Those are:-

1)Create Table:-  

The create table command is used to define the table with its name. Also the list of
fields, their data types as well size are necessary. There are certain rules while naming
the table. One important note is that, at the end of SQL statement ‘;’( semicolon) is
given to terminate it. The rules while naming the table and the attribute are as follows:

* A name can be of maximum up to 30 characters.

* Alphabets (A-Z and a-z) as well as number (0-9) are allowed.

* The name should begin with an alphabet and not the number.

* The special symbol generally allowed is ‘_’ i.e. underscore.

* Some reserve words of SQL like insert, update, create etc are not allowed for
renaming.

    Syntax to create the table is as follows:

   SQL> create table <table name> (<columnName1> <data type> (<size>),


(<columnName2> <data type> (<size>)…) ;

E.g. Student table is created with the following definition where studname, rollno ,
marks, addr are defined as an attributes.

SQL> create table student

create table student1(studname varchar(30), rollno int primary key, marks int, addr
varchar(25));
2)Describe table:-

In the last command we have just create table student. The table does not contain any
kind of records but the basic structure is ready. To check the basic structure of table
we can execute describe command.

Syntax to describe the table is as follows:

select* from <Table Name>

Ex:

select* from student1;

3) Alter table:-

Alter table command is used to make necessary changes or modification in the table
structure. With alter table the adding and deleting of the column is possible.

e.g suppose the structure of the table is:

A. Lets add column address1  to student1. It can be done as follows:


Syntax:-

           Alter table <tablename> add (<columnname datatype size>, <columnname


datatype size>);

Ex:
ALTER TABLE student1 add Address1 varchar(50);

B. It is possible to even drop the column from an existing table using alter table with
drop column clause.

Syntax:-

                    Alter table <tablename> drop column (<columnname >,


<columnname>);

Ex:

ALTER TABLE student1

DROP COLUMN studname;

C. Third option with alter table is modifying existing column.


 Syntax:-

                      Alter table <tablename> modify (<columnname Newdatatype


Newsize>);

Ex:
ALTER TABLE student1
ALTER COLUMN marks varchar(50);

There are certain limitations of alter table:-

1)      It is not possible to change the name of the table

2)      Change in the column name is also not possible.

3)      If column contains some data then its length cannot be reduced.

4)Drop table:-

The table is deleted with the help of drop column command. It deletes the table along
with its data. Once the table is dropped the records or data within it cannot be
recovered. So drop table should be executed with caution.

Syntax:-

       Drop table <tablename>;

e.g.

Drop table student1;

5)Rename table:-

                       Rename command gives new name to the table. It has been seen that it
is not possible to give new name to the table using alter table. So user can use rename
command to specify the new name to the table.

Syntax:-

     Sp_Rename<table name> to <Newtablename>;

e.g.

Sp_Rename student1,student2;

Conclusion:
Output:

1)Create Table:-  

2)Describe table:-
3) Alter table:-

A.

B.

C.
4)Drop table:-

5)Rename table:

You might also like