0% found this document useful (0 votes)
88 views11 pages

Database Systems Lab Manual Guide

Uploaded by

fatimaajmal004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views11 pages

Database Systems Lab Manual Guide

Uploaded by

fatimaajmal004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

4. Create a Table in Database:


The CREATE TABLE statement is used to create a new table in a database.
Create Table table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

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:
[Link]

MYSQL Data Types:

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

NOT NULL - Ensures that a column cannot have a NULL value

UNIQUE - Ensures that all values in a column are different

PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table

FOREIGN KEY - Uniquely identifies a row/record in another table

CHECK - Ensures that all values in a column satisfies a specific

condition DEFAULT - Sets a default value for a column when no value

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.

a. NOT NULL Constraint


By default, a column can hold NULL values. The NOT NULL constraint enforces a column to
NOT accept NULL values. This enforces a field to always contain a value, which means that you
cannot insert a new record, or update a record without adding a value to this field.

Add NOT NULL Constraint Using Create Table:

Create Table
Student (
rollNumber int NOT NULL,
name nvarchar(50) NOT NULL,
cgpa float NULL
)

Add NOT NULL Constraint Using ALTER Table:


Alter Table Student Alter column rollNumber int NOT NULL
MYSQL:

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.

Add Unique Constraint Using Create Table


Create Table
Student (
rollNumber int NOT NULL,
name nvarchar(50) NOT
NULL,
cnic nvarchar(50) NOT NULL unique,
cgpa float NULL
)

Unique constraint can also be defined as follows:

Create Table
Student (
rollNumber int NOT NULL,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL
unique (cnic)
)

Add Unique Constraint Using Alter Table


Alter Table Student add constraint UNIQUE_CONSTRAINT_STUDENT_CNIC Unique (cnic)

Unique Constraint on a Combination of Columns


Unique constraint can also be applied on a combination of columns:

Create Table
Student (
rollNumber int NOT NULL,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL,
unique (cnic, name)
)

(OR)

Alter Table Student add constraint UNIQUE_CONSTRAINT_STD_CNIC_NAME Unique (cnic, name)


Lab Manual: Database Systems
c. Primary Key Constraint
The primary key constraint uniquely identifies each row in a table. Primary keys must contain
UNIQUE values, and cannot contain NULL values. A table can have only one primary key, which
may consist of single or multiple columns.

Add Primary Key Constraint Using Create Table


Create Table Student
(
rollNumber int primary key,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL
)

(OR)
Create Table Student
(
rollNumber int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL,
primary key (rollNumber)
)

Add Primary Key Constraint Using Alter Table


To add primary key using alter table statement, the column must have not null constraint, otherwise
the not null constraint must first be defined.
Alter Table Student alter column rollNumber int NOT NULL
Alter Table Student add constraint PK_STUDENT primary key (rollNumber)

Composite Primary Key


A primary key involving more than one column is called composite primary key.

Add composite Primary Key Using Alter Table


Create Table
Student (
rollNumber int,
name
nvarchar(50),
cnic
nvarchar(50),
cgpa float,

primary key (rollNumber, cnic)


)

Add Composite Primary Key Using Alter Table


Alter Table Student add constraint PK_STUDENT primary key (rollNumber, cnic)
Lab Manual: Database Systems
MYSQL:

d. Foreign Key Constraint


A foreign key is a column (or a collection of columns) in one table that refers to the primary key in
another table. The value of the foreign key comes from the table in which it is the primary key.

Add Foreign Key Constraint Using Create Table

CREATE TABLE Student (


rollNumber int PRIMARY
KEY,
deptId int FOREIGN KEY REFERENCES Department(departmentId)
);

(OR)

CREATE TABLE
Student( rollNum
ber int, deptId
int,
PRIMARY KEY (rollNumber),
FOREIGN KEY (deptId) REFERENCES Department(departmentId)
);

Add Foreign Key Constraint Using Alter Table:


Alter Table Student add constraint FK_STUDENT foreign key (deptId) references
Department (departmentId)

Cascade/No Action/Set NULL:


We can define some actions for a foreign key value whenever there is a change (update or delete) in
the primary key value.

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 (

departmentId int primary


key, departmentName
nvarchar(50)
)

alter table Student add constraint FK_STUDENT foreign key (deptId) references Department
(departmentId) on delete No Action on update Cascade

Composite Foreign key:


A primary key which is composite in the referenced table will form a composite foreign key in the
referencing table (all attributes of the composite primary key must be part of the foreign key in such a
case).

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

Add Check Constraint Using Create Table


Create Table
Student (
rollNumber int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL check (cgpa>=0 AND cgpa<=4)
)

(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

2. MySQL check constraints- Table constraints example

Add Check Constraint Using Alter Table


alter table Student add Constraint STUDENT_CHECK_CGPA check (cgpa>=0 AND cgpa<=4)

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.

Add Default Constraint Using Create Table


Create Table Student
(
rollNumber int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float default 0,
check (cgpa>=0 AND cgpa<=4)
)
SQL:
Add Default Constraint Using Create Table
alter table Student add constraint DEFAULT_SUDENT_CGPA default 0 for CGPA
MYSQL:
Alter table Student alter cgpa set default 0.5 ;

g. Drop a Constraint
alter table Student drop constraint DEFAULT_SUDENT_CGPA

8. Add a New Column


Create Table
Student (
rollNumber int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL
)

alter table Student add cgpa float NOT NULL

MySQL Syntax

Example

ALTER TABLE Student

ADD COLUMN phone VARCHAR(15) name;


AFTER

We can add multiple columns


Lab Manual: Object Oriented Programming

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

cnic to drop multiple columns

10. Modify a Column


Create Table
Student (
rollNumber int,
name nvarchar(50) NOT
NULL, cnic
nvarchar(50) NOT NULL,
)
alter table Student modify rollNumber varchar(100) NOT NULL

Rename Column:
Syntax:

Example:

END

Common questions

Powered by AI

Constraints like CHECK and NOT NULL play critical roles in ensuring data integrity. The CHECK constraint restricts the values that can be stored in a field based on a specified condition, reducing errors due to invalid data. Conversely, NOT NULL constraints prevent null values in specified columns, thus ensuring that critical data fields are always populated .

A composite primary key is used when a unique identifier for a table record requires a combination of multiple columns. It differs from a single column primary key, which relies on the uniqueness of a single column to define each record. Composite primary keys are particularly useful in many-to-many relationship tables, where a single column cannot provide uniqueness .

A DEFAULT constraint is useful as it allows a default value to be automatically assigned to an optional field when an explicit value is not provided during data insertion. This can help ensure that a column always contains a meaningful value, which is especially useful for timestamp fields that need to default to the current date-time, or numerical fields that need a default base value for calculations .

The main advantage of applying UNIQUE constraints across multiple columns is that it allows each combination of specified columns to be unique, which is beneficial for ensuring data sets with composite attributes like email plus phone number. However, limitations include increased complexity in maintaining and querying data, and potential performance impacts in databases with large volumes due to the need for additional indexing .

Adding a new column to an existing table involves using the ALTER TABLE statement with the ADD COLUMN clause. Considerations include defining the appropriate datatype, specifying constraints like NOT NULL if the column should not allow null values, and determining whether a default value should be applied to pre-existing rows .

Altering an existing column's datatype may be necessary when new business requirements demand that a column in a database accommodate larger numbers, different types of values (e.g., changing from INT to VARCHAR), or new formats. While addressing functional requirements, implications include potential data loss if existing data is incompatible with the new type, and updating applications that interact with the column to handle its new type correctly .

The CASCADE action is beneficial because it automatically updates or deletes records in a referencing table when the linked record in the primary table is updated or deleted, ensuring data consistency. However, it can be problematic because inadvertent deletions or updates in the primary table can propagate through related tables, potentially leading to unintended data loss or changes across the database .

Dropping a table removes the table definition along with its data, completely deleting the table from the database. In contrast, truncating a table deletes only the data in the table while preserving the table definition, meaning the structure of the table remains for future use .

Dropping a constraint completely removes it from the table, freeing the column from previously enforced rules. This might be ideal during a schema redesign where certain constraints are no longer needed. Altering a constraint, on the other hand, modifies it to potentially tighten or relax the rules it enforces, ideal for adjusting data validation needs in response to changing requirements without entirely removing the constraint .

A foreign key constraint ensures that the value in a column (or a group of columns) matches a value in another table's primary key column. This linkage enforces referential integrity by ensuring that data across tables remains consistent: the foreign key values in the referencing table must correspond to the primary key values in the referenced table, thereby preventing orphaned records and maintaining data integrity .

You might also like