DB Lab Assign First
DB Lab Assign First
Solution
create database studies;
PART(A)
insert into Students(StudentID , FirstName , LastName , Email , Phone) values(120
, 'Hashir' , 'Sarfaraz' , '[email protected]' , '0329-66613245')
--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)
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;
);