Data Definition Language:-


These commands are used for creating ,modifying and dropping the structure of Database objects.
This language contains five commands
1.     Create
2.     Alter
3.     Sp_Rename
4.     Truncate
5.     Drop
      
1.Create: 
  • This command is used to create the database objects within the database
  • SY: create table <table name>
         (col 1 data type (size),
         col2 data type (size),
                     :
                     :
                     :
                     :
         coln data type (size));
Ex: create table Employee(EmpId int,EName varchar(20))  --Creating A table

2. ALTER:

  • This command is used to modify the structure of a table using this command, we can perform four different operations
  • Using this command we can increase (or) decrease the size of the data type & also we can change the data type from old data type to new data type
  • We can add a new column to the existing table
  • We can change the column name from old column name to new column name
  • We can remove the column from the existing table
This command contains 4 sub commands
1.     Alter- Alter column
2.     Alter- Add
3.     Sp_Rename
4.     Alter- Drop
SP_help: This command is used to see the structure of table
SY: sp_help <table name>
Ex: sp_help Employee

Syntax for Alter-Alter Column
Sy: Alter Table <Table Name> Alter Column Column Name Data Type (Size)
Ex: alter table Employee Alter column EName varchar(22)—-Altering the coloumn
  
i]Alter-Add: This command is used to add a new column to the existing table
Sy: ALTER TABLE <TABLE NAME> ADD COLUMNNAME DATA TYPE(size);
Ex:- alter table Employee add  Salary money –-ading the coloum to the existing table

ii]Alter-Drop:
Alter Table <Table Name> Drop Column <Column Name>;
alter table employee drop column salary—to delete a perticular coloumn

iii]Sp_Rename:
SY: Sp_Rename ‘Tablename.Oldcolumn’,’New Column Name’,’Column’;
EX: sp_rename 'Employee.EName','Name','column' –changing the column Name of the table.

3. Sp_Rename:
This command is used to change the table name from old table name to new table name
SY: sp_rename ‘old table name’,’New table name’
EX: sp_rename empdetails,Employee—changing the Name of the table
4. Truncate:
This command is used for to delete all the records from existing table permanently
Sy: Truncate Table <Table Name>
EX: truncate table employee –-to delete the records permanently from the database
5. Drop:
This command is used to remove the table permanently from the database
Sy: Drop Table <Table Name>
Ex: drop table employee –to drop the table permanently from the database

No comments:

Post a Comment