0% found this document useful (0 votes)
30 views13 pages

DBMS Week6

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

DBMS Week6

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

CS262

DATABASE MANAGEMENT
SYSTEM

LABORATORY
ASSIGNMENT

NAME :-

KANZARIYA
AKSHAY KANAIYALAL.

ROLL NO :- 202251062

SECTION 1

WEEK 6
Question 1

Design a MySQL database schema for bookstore inventory management. Create


tables and columns as needed, incorporating the following points. Feel free to
add appropriate data to your tables based on your understanding. Your
submission should include proper code for creation of necessary tables,
constraints, views, etc ensuring the fulfilment of all the specified requirements.

a. Ensure that the quantity of books in the inventory is always non-negative


using a check constraint.

b. Enforce a rule that the price of a book cannot be less than Rs. 99 utilizing the
assertion keyword.

c. Use the deferred keyword to defer the checking of foreign key constraints
until the end of the transaction.

d. Create a view that displays the books with their titles, authors, and prices for
easy reference.

➢ CODE FOR CREATE TABLE:-

CREATE TABLE Authors(


author_id INT PRIMARY KEY,
author_name VARCHAR(255) NOT NULL
);

CREATE TABLE Books (


book_id INT PRIMARY KEY,
Book_title VARCHAR(255) NOT NULL,
author_id INT,
Book_price DECIMAL(10, 2) NOT NULL,
Book_quantity INT NOT NULL CHECK (Book_quantity >= 0),
CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES
Authors(author_id) DEFERRABLE INITIALLY DEFERRED,
CHECK (Book_price >= 99)
);
➢ CODE FOR CREATE VIEWS:-

CREATE VIEW BookView AS


SELECT b.Book_title, a.author_name, b.Book_price
FROM Books b
JOIN Authors a ON b.author_id = a.author_id;

➢ CODE FOR INSERT DATA:-

INSERT INTO Authors (author_id, author_name) VALUES (100, 'Akshay


Kanzariya');
INSERT INTO Authors (author_id, author_name) VALUES (101, 'Divya
Kanzariya');
INSERT INTO Authors (author_id, author_name) VALUES (102, 'Songara
Pankaj');
INSERT INTO Authors (author_id, author_name) VALUES (103, 'Songara
Ravi');

INSERT INTO Books (book_id, Book_title, author_id, Book_price,


Book_quantity) VALUES (001, 'Treasure Island', 100, 1500.00, 9);
INSERT INTO Books (book_id, Book_title, author_id, Book_price,
Book_quantity) VALUES (002, 'Time Machine', 101, 1999.99, 20);
INSERT INTO Books (book_id, Book_title, author_id, Book_price,
Book_quantity) VALUES (003, 'The Tempest', 102, 1299.50, 10);
INSERT INTO Books (book_id, Book_title, author_id, Book_price,
Book_quantity) VALUES (004, 'Romeo and Juliet', 103, 1499.50, 15);
➢ PRINT TABLE:-

SELECT * FROM Authors;

SELECT * FROM Books;

SELECT * FROM BookView;

1.

2.
3.
Question 2

Design a MySQL database schema for a university to maintain


student records with the following constraints. Create tables and
columns as needed and free to add appropriate data to your tables
based on your understanding. Your submission should include
proper code for creation of necessary tables, constraints, views,
etc ensuring the fulfilment of all the specified requirements.

a. Every student must belong to at least one department.


b. The GPA of a student must be between 0 and 4.
c. Each department must have a department head.
d. The maximum number of students in a department cannot
exceed 100.

Note: Make sure to use check constraints, deferred keyword,


assertion statements, etc. You would be evaluated upon how well
you utilize these things in your database design!
➢ CODE FOR CREATE TABLE:-

CREATE TABLE Employees (


Employee_id INT PRIMARY KEY,
Employee_name VARCHAR(50) NOT NULL
);

CREATE TABLE Departments (


Department_id INT PRIMARY KEY,
Department_name VARCHAR(50) NOT NULL,
Department_head INT,
CONSTRAINT fk_department_head
FOREIGN KEY (Department_head)
REFERENCES Employees(Employee_id) DEFERRABLE INITIALLY
DEFERRED
);

CREATE TABLE Students (


Student_id INT PRIMARY KEY,
Student_name VARCHAR(50) NOT NULL,
Department_id INT,
GPA DECIMAL(3,2) NOT NULL,
CONSTRAINT chk_gpa_range CHECK (GPA >= 0 AND GPA <= 4),
CONSTRAINT fk_department
FOREIGN KEY (Department_id)
REFERENCES Departments(Department_id) DEFERRABLE
INITIALLY DEFERRED
);
CREATE TABLE Enrollment (
Enrollment_id INT PRIMARY KEY,
Student_id INT,
Department_id INT,
CONSTRAINT fk_enrollment_student
FOREIGN KEY (Student_id)
REFERENCES Students(Student_id) DEFERRABLE INITIALLY
DEFERRED,
CONSTRAINT fk_enrollment_department
FOREIGN KEY (Department_id)
REFERENCES Departments(Department_id) DEFERRABLE
INITIALLY DEFERRED
);

➢ CODE FOR TRIGGER :-

CREATE OR REPLACE TRIGGER max_students_per_department


BEFORE INSERT OR UPDATE ON Enrollment
FOR EACH ROW
DECLARE
Department_count INT;
BEGIN
SELECT COUNT(*) INTO Department_count
FROM Enrollment
WHERE Department_id = :NEW.Department_id;

IF Department_count >= 100 THEN


RAISE_APPLICATION_ERROR(-20001, 'Maximum number of
students in the department exceeded');
END IF;
END;
/
➢ CODE FOR INSERT DATA:-

INSERT INTO Employees (Employee_id, Employee_name) VALUES (1,


'Akshay Kanzariya');
INSERT INTO Employees (Employee_id, Employee_name) VALUES (2,
'Divya Kanzariya');
INSERT INTO Employees (Employee_id, Employee_name) VALUES (3,
'Songara Pankaj');
INSERT INTO Employees (Employee_id, Employee_name) VALUES (4,
'Songara Ravi');

INSERT INTO Departments (Department_id, Department_name,


Department_head) VALUES (101, 'Computer Science', 1);
INSERT INTO Departments (Department_id, Department_name,
Department_head) VALUES (102, 'Mathematics', 2);
INSERT INTO Departments (Department_id, Department_name,
Department_head) VALUES (103, 'Physics', 1);
INSERT INTO Departments (Department_id, Department_name,
Department_head) VALUES (104, 'Electrical', 2);

INSERT INTO Students (Student_id, Student_name, Department_id, GPA)


VALUES (20225101, 'Jeet Jani', 101, 3.5);
INSERT INTO Students (Student_id, Student_name, Department_id, GPA)
VALUES (20225102, 'Ajay Jain', 101, 3.8);
INSERT INTO Students (Student_id, Student_name, Department_id, GPA)
VALUES (20225103, 'Himay Patel', 102, 2.9);
INSERT INTO Students (Student_id, Student_name, Department_id, GPA)
VALUES (20225104, 'Pulkit Gupta', 102, 4.0);
INSERT INTO Students (Student_id, Student_name, Department_id, GPA)
VALUES (20225105, 'Kalpeet Kapse', 103, 3.2);
INSERT INTO Students (Student_id, Student_name, Department_id, GPA)
VALUES (20225106, 'Rajpal Chaudhry', 103, 3.9);
INSERT INTO Students (Student_id, Student_name, Department_id, GPA)
VALUES (20225107, 'Songara Jaydeep', 104, 3.7);
INSERT INTO Students (Student_id, Student_name, Department_id, GPA)
VALUES (20225108, 'Mayur Kanzariya', 104, 3.6);

INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)


VALUES (2001, 20225101, 101);
INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)
VALUES (2002, 20225102, 101);
INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)
VALUES (2003, 20225103, 102);
INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)
VALUES (2004, 20225104, 102);
INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)
VALUES (2005, 20225105, 103);
INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)
VALUES (2006, 20225106, 103);
INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)
VALUES (2007, 20225107, 104);
INSERT INTO Enrollment (Enrollment_id, Student_id, Department_id)
VALUES (2008, 20225108, 104);

➢ CODE FOR TRIGGER QUERY EXAMPLE :-

BEGIN
INSERT INTO Enrollment (enrollment_id, student_id, department_id)
VALUES (2005, 1005, 102);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/
➢ PRINT TABLE:-

SELECT * FROM Employees;

SELECT * FROM Departments;

SELECT * FROM Students;

SELECT * FROM Enrollment;

1.

2.
3.
4.

END

You might also like