0% found this document useful (0 votes)
15 views

DB Lab Assign First

The document provides SQL queries to perform CRUD operations on a database with tables for Students, Courses, and Enrollments. It includes queries to: 1) Insert new records into the Students, Courses, and Enrollments tables 2) Read and select data from the tables by joining them 3) Update a record in the Enrollments table 4) Delete a Student record and its associated enrollments using a cascading delete It also defines several common SQL constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY and explains their purposes for enforcing data integrity and relationships between tables.

Uploaded by

I am ab Yes
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)
15 views

DB Lab Assign First

The document provides SQL queries to perform CRUD operations on a database with tables for Students, Courses, and Enrollments. It includes queries to: 1) Insert new records into the Students, Courses, and Enrollments tables 2) Read and select data from the tables by joining them 3) Update a record in the Enrollments table 4) Delete a Student record and its associated enrollments using a cascading delete It also defines several common SQL constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY and explains their purposes for enforcing data integrity and relationships between tables.

Uploaded by

I am ab Yes
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
You are on page 1/ 6

QUESTION 1:

Write SQL queries to perform the CRUD operations.

Solution
create database studies;

create table Students


(
StudentID int not null primary key,
FirstName varchar(255),
LastName varchar(255),
Email varchar(255),
Phone varchar(255),
);

create table Courses


(
CourseID varchar(255) not null primary key,
CourseName varchar(255),
Credits int,
);

create table Enrollments


(
EnrollmentID varchar(255),
StudentID int,
CourseID varchar(255), Grade varchar(255), foreign key
(StudentID) references Students(StudentID), foreign key
(CourseID) references Courses(CourseID)
);
--now insert a new student in the student table

PART(A)
insert into Students(StudentID , FirstName , LastName , Email , Phone) values(120
, 'Hashir' , 'Sarfaraz' , '[email protected]' , '0329-66613245')

--more entries insert into Courses(CourseID ,


CourseName , Credits) values('CS-222-001-2023-
Spring' , 'Data Structure' , 3)

insert into Courses(CourseID , CourseName , Credits) values('CS-333-001-2023-


Fall' , 'Data Base' , 3)

insert into Students(StudentID , FirstName , LastName , Email , Phone) values(125


, 'Saad' , 'Saleem' , '[email protected]' , '0301-5211324')

insert into Enrollments (EnrollmentID, StudentID, CourseID, Grade)


values('U202310003' , 125 , 'CS-333-001-2023-Fall' , 'C')

insert into Enrollments (EnrollmentID, StudentID, CourseID, Grade)


values('U202310004' , 120 , 'CS-222-001-2023-Spring' , 'A-')
PART(B)

--Read
select e.EnrollmentID, e.StudentID, s.FirstName, s.LastName, s.Email, s.Phone,
c.CourseName, c.CourseID, c.Credits, e.Grade from Enrollments e inner join Students s on
e.StudentID = s.StudentID inner join Courses c on e.CourseID = c.CourseID;

PART(C)
--update
update Enrollments set Grade = 'B' where CourseID = 'CS-333-001-2023-Fall' AND StudentID
= 125

PART(D)

--delete alter table Enrollments add


constraint fk_enrollments_students
FOREIGN KEY (StudentID) REFERENCES Students(StudentID) on delete cascade;--using
cascading delete

delete from Students where StudentID = 125;

QUESTION 2:
(a)Define the following SQL constraints and explain their purpose:
NOT NULL:
It ensures that a column cannot have a NULL value. It means that presence of value.
PURPOSE:
It enforces that every row in the table must have a value.
UNIQUE:
It ensures that each value in a column is different from every other value. It means no repetition of value.
PURPOSE:
Unlike PRIMARY KEY, it allows NULL values. It enforces that every value must be unique.

PRIMARY KEY:
Primary key is a combination of “NOT NULL” and “UNIQUE”.
PURPOSE:
It enforces the uniqueness of values in the specified column(s) and ensures that the column(s) cannot
contain NULL values.

FOREIGN KEY:
Foreign key uniquely identifies a row/record in another table.
PURPOSE:
It establishes a link between two tables by specifying that values in a column (or set of columns) in one
table must correspond to values in a primary key column in another table.

CHECK:
Check ensures that all values in a column satisfies a specific condition.
PURPOSE:
It enforces data only rows that satisfy the specified condition to be inserted or updated.

DEFAULT:
It sets a Default value for a column when no value is specified.

PURPOSE:
If no value is provided for the column, then it helps ensure consistency.

INDEX:
Index is used to create and retrieve data from databases very quickly.
PURPOSE:
It improves query performance by creating an index on one or more columns.

QUESTION 2(B):
Solution
create database emp;

create table Departments


(
DepartmentID int not null primary key,
DepartmentName varchar(255),
);
create table Employees
(
EmployeeID int not null, -- Unique identifier for each employee
FirstName varchar(255),
LastName varchar(255),
Email varchar(255) unique, -- Unique email address for each employee
DepartmentID int,

constraint pk_emp primary key(EmployeeID), -- Primary key constraint

-- Foreign key constraint on DepartmentID, referencing the Departments table constraint


fk_dep foreign key(DepartmentID) references Departments(DepartmentID),
constraint uq_email unique (Email) -- Unique email address for each employee

);

You might also like