Lab Manual
Lab Manual
Lab Manual 1
“Introduction to MS SQL Server , DML and DDL”
Database Systems
Fall 2019
Table of Contents
1. Objective 2
2. Task Distribution 2
3. Getting Started with Microsoft SQL server 3
Step 1: Open SQL server management Studio 3
Step 2: Connect to Server 3
Step 3: Create your own Database 4
>>If you want to delete your database write the following query and Execute, make sure that the
command is executed successfully. 5
You can also create new database by Left click on Database from object Explorer and select new
Database, give it a name and press ok (as shown in following figures). 6
Step 4: View and use you database 7
4. Data Definition Language: 7
Step 1: Create table: 8
Step 2: Altering table Schema 8
Adding new column to existing table 8
Drop existing Column from existing table 8
Step 3: Add Foreign key Constraint. 9
One way to create FK is to add it whilst creating table using the following syntax 9
Other way is to add FK constraint after creating the table, by using ALTER query, 9
Step 4: Add foreign key constraint to tables 10
Add FK whilst creating table. 10
FK can also be added after the referencing and referred tables have been created. 11
5. Data Modification Language 11
Step 1: Insert rows into table 11
Step 2: To see the data from you table use the following table 12
Step 3: Delete rows from the table 12
Step 4: Delete all the data from the table 12
Exercise 14
Appendix 15
Comments is SQL 15
Data Types 15
Where Conditions 17
Page 2
CUI, Lahore Campus Database Systems
1. Objective
The purpose of this lab is to introduce students to Microsoft SQL server environment and getting started
with Data definition queries and Data modification queries.
2. Prerequisites
You should know the concepts of Primary key, foreign key and how to identify these constraints from the
given schema. You have to read the manual before coming to lab.
3. Task Distribution
4.
Page 3
CUI, Lahore Campus Database Systems
Page 4
CUI, Lahore Campus Database Systems
>>Click on New Query, a new blank page will open (as shown in following figure)
Page 5
CUI, Lahore Campus Database Systems
>>If you want to delete your database write the following query and Execute, make sure that the
command is executed successfully.
You can also create new database by Left click on Database from object Explorer and select new
Database, give it a name and press ok (as shown in following figures).
Page 6
CUI, Lahore Campus Database Systems
Page 7
CUI, Lahore Campus Database Systems
If you expand the Database from object explorer, you can see all of the databases that exist of the server,
including the newly created ones (as shown in following figure).
To use your database, make sure to select your database from drop down menu (as shown in following
figure).
Use <databaseName>
Page 8
CUI, Lahore Campus Database Systems
Create the table Students using the query as shown in following figure, Execute, make sure the
message shows that the command is successful.
Go to object explorer and find you table.
Page 9
CUI, Lahore Campus Database Systems
Other way is to add FK constraint after creating the table, by using ALTER query,
alter table [tableName] add constraint [keyContraintName] Primary key
(Column1, column2, column3)
Page 10
CUI, Lahore Campus Database Systems
sp_help <tableName>
This will give you information about colums, their datatype and all the
constraints on the table.
Page 11
CUI, Lahore Campus Database Systems
FK can also be added after the referencing and referred tables have been created.
Try the following and see in object explorer
Or
INSERT INTO <tableName> (ColumnX,ColumnY) -- List of Columns
values
(ColumnXValue,ColumnYValue), --Row 1 list of Corrrespong column values
(ColumnXValue,ColumnYValue), --Row 2
(ColumnXValue,ColumnYValue) –Row 3
Page 12
CUI, Lahore Campus Database Systems
Page 13
CUI, Lahore Campus Database Systems
Step 2: To see the data from you table use the following table
Select * from tableName
Try the following and see the message and data in table using select query
Try these two commands after adding some rows to the table and see the
results.
See the data in table before and after trying the following query
Page 14
CUI, Lahore Campus Database Systems
Exercise
Will be given in lab
Appendix
Comments is SQL
Comments in SQL server start with two dashes as shown below, in green color
Data Types
Page 15
CUI, Lahore Campus Database Systems
Page 16
CUI, Lahore Campus Database Systems
Where Conditions
Or
And
Not
In
Sub queries
In
All
Any
Some
Exists
Page 17