Lab Manual Week03 DBS
Lab Manual Week03 DBS
Learning Objectives:
• Learning Data Definition Language
• Implementing DDL commands on our database
DDL commands:
The queries to manage the database, tables and views come under the umbrella of Data Definition Language.
1- Create:
If no, then right click on Databases and Refresh the services again
By expanding the Tables, you can see there is currently no table in our database, so we have to create one in
order to put our data into it.
For this task, write the following into the Query editor.
Remember: Once a query is executed, you should remove or hide it to avoid re-execution.
To comment the previous queries, use the button and to uncomment use this
CREATE table TableName -- Creates a database with your registration number
(
ID int,
FirstName varchar(255),
LastName varchar(255),
Age int,
Gender varchar(255),
)
By executing the above query, you can now see a table in your database with the given table name. Right click
on the table name and click “Select Top 1000 Rows”.
2- Drop:
This query is used to remove a table from the database.
Syntax:
3- Alter:
For any change in the tables, we use Alter command.
Description Query Syntax Example
Add a column in table ALTER Table tableName ADD ALTER Table Student ADD GPA
columnName datatype varchar(255)
Change datatype of column ALTER Table tableName ALTER ALTER Table Students ALTER
column columnName datatype column GPA int
Remove column(s) from the ALTER Table tableName DROP ALTER Table Students DROP
table column columnName column GPA
4- Truncate:
To remove the data from the tables we use Truncate command.
Syntax:
TRUNCATE Table tableName --All the entries from your table will be removed
5- Rename
To change the name of a table we use sp_rename command, which is a Stored Procedure. Stored procedures
are the functions of SQL server. And to execute the stored procedures we use exec before the command.
Syntax:
exec sp_rename tableName, new_tableName --Table will be renamed to the new name
2. Lab Tasks
1. Create database of a company having following tables.