Lab Manual: Database Systems
Lab Manual: Database Systems
Lab- 04 Manual
Contents
1. Create a Database.................................................................................................................3
2. Use a database......................................................................................................................3
3. Delete a Database.................................................................................................................3
4. Create a Table in Database..................................................................................................3
5. Drop a Table........................................................................................................................4
6. Truncate a Table..................................................................................................................4
7. SQL Constraints...................................................................................................................5
a. NOT NULL Constraint............................................................................................5
b. UNIQUE Constraint................................................................................................6
c. Primary Key Constraint...........................................................................................7
d. Foreign Key Constraint............................................................................................8
e. Check Constraint......................................................................................................9
f. Default Constraint..................................................................................................11
g. Drop a Constraint..................................................................................................11
8. Add a New Column...........................................................................................................11
9. Drop a Column...................................................................................................................12
10. Modify a Column...............................................................................................................12
1. Create a Database:
In order add data in our database, we first need to create our database. The syntax for creating
database is as follows:
Create database databasename;
Example:
Create database myDB;
Lab Manual: Database Systems
2. Use a database:
To tell the DBMS about the database in which we want to create database tables or add data, we
need to use the following syntax after creating our database:
Use databasename;
3. Delete a Database:
We can delete a database by using the following syntax:
Drop database databasename
Example:
Drop database myDB;
The above statement will delete all tables of myDB and their data.
Example:
Create Table
Student (
rollNumber int,
name
nvarchar(100),
cnic
nvarchar(100),
cgpa float
)
Lab Manual: Database Systems
To learn about datatypes in SQL Server visit the following link, and see datatypes under SQL
SERVER Data types:
https://www.w3schools.com/sql/sql_datatypes.asp
5. Drop a Table:
A table can be drop using the following syntax: (Dropping a table will delete the table data as well as
table definition)
Drop Table tableName
Example:
Drop Table Student
6. Truncate a Table:
A table can be truncated using the following syntax: (Truncating a table will only delete the data in
the table and not table definition)
Truncate Table tableName
Example:
Truncate Table Student
Lab Manual: Database Systems
7. SQL Constraints
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
is specified
Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.
Create Table
Student (
rollNumber int NOT NULL,
name nvarchar(50) NOT NULL,
cgpa float NULL
)
In this case, the name of the old and new column names are the same except that the column must
have a NOT NULL constraint:
Lab Manual: Database Systems
Alter Table
Student CHANGE
rollNumber
rollNumber int NOT NULL;
b. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
Create Table
Student (
rollNumber int NOT NULL,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL
unique (cnic)
)
Create Table
Student (
rollNumber int NOT NULL,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL,
unique (cnic, name)
)
(OR)
(OR)
Create Table Student
(
rollNumber int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL,
primary key (rollNumber)
)
(OR)
CREATE TABLE
Student( rollNum
ber int, deptId
int,
PRIMARY KEY (rollNumber),
FOREIGN KEY (deptId) REFERENCES Department(departmentId)
);
If we want to update the foreign key value when the corresponding primary key value is updated, and
delete the foreign key value whenever the corresponding primary key value is deleted; we use
cascade option.
If we want to set the foreign key value to null whenever the corresponding primary key value is
updated or deleted, then we use set NULL option.
If we want to force the foreign key to remain unchanged, we will use No Action option. If No
Action is specified, then the primary key value will not be allowed to delete or update in the
referenced table if there is a corresponding value of foreign key in the referencing table.
Lab Manual: Database Systems
Example:
Create Table
Student (
rollNumber int primary key,
deptId int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
)
Create Table
Department (
alter table Student add constraint FK_STUDENT foreign key (deptId) references Department
(departmentId) on delete No Action on update Cascade
e. Check Constraint
The check constraint is used to limit the value range that can be placed in a column.
Two types of constraints:
1. MySQL check constraints –column exmaple
(OR)
Create Table Student
(
rollNumber int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL,
check (cgpa>=0 AND cgpa<=4)
)
Lab Manual: Database Systems
Use following command to view the table definition
Check Constraint can be applied to relate the value of a column with another column:
Create Table
StudentMarks (
rollNumber int,
obtainedMarks float,
maximumMarks float,
check (obtainedMarks >=0 AND obtainedMarks<= maximumMarks)
)
Lab Manual: Database Systems
f. Default Constraint
Default constraint is used to assign a default value to a column when no value is specified by the
user during data insertion.
g. Drop a Constraint
alter table Student drop constraint DEFAULT_SUDENT_CGPA
MySQL Syntax
Example
9. Drop a Column
In some situations, you want to remove one or more columns from a table. In such cases, you use
the following ALTER TABLE DROP COLUMN statement:
alter table Student drop column
Rename Column:
Syntax:
Example:
END