0% found this document useful (0 votes)
4 views5 pages

Department

The document contains SQL commands for creating tables related to a database schema, including Department, Employee, Project, and Works_On tables with appropriate foreign key relationships. It also includes various SELECT queries to retrieve data from Members, Books, Genres, and Loans, as well as a stored procedure for inserting members and a trigger for updating the Books table upon genre deletion. Overall, it outlines database operations for managing members, books, and their loans in a library system.

Uploaded by

tungbd2004
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)
4 views5 pages

Department

The document contains SQL commands for creating tables related to a database schema, including Department, Employee, Project, and Works_On tables with appropriate foreign key relationships. It also includes various SELECT queries to retrieve data from Members, Books, Genres, and Loans, as well as a stored procedure for inserting members and a trigger for updating the Books table upon genre deletion. Overall, it outlines database operations for managing members, books, and their loans in a library system.

Uploaded by

tungbd2004
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/ 5

C1

-- Create the Department table


CREATE TABLE Department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL
);

-- Create the Employee table


CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);

-- Create the Project table


CREATE TABLE Project (
proj_id INT PRIMARY KEY,
proj_name VARCHAR(50) NOT NULL,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);

-- Create the Works_On table


CREATE TABLE Works_On (
emp_id INT,
proj_id INT,
hours_worked INT,
PRIMARY KEY (emp_id, proj_id),
FOREIGN KEY (emp_id) REFERENCES Employee(emp_id),
FOREIGN KEY (proj_id) REFERENCES Project(proj_id)
);

C2

SELECT MemberID, MemberName, Address, Email, Sex, BirthDate

FROM Members

WHERE Sex = 'Male' AND BirthDate > '1990-12-31';

C3

SELECT b.BookID, b.Title, b.PublicationYear, g.GenreID, g.GenreName

FROM Books b
JOIN Genres g ON b.GenreID = g.GenreID

WHERE b.PublicationYear <= 1900 AND g.GenreName = 'Adventure';

C4

SELECT DISTINCT g.GenreID, g.GenreName

FROM Genres g

JOIN Books b ON g.GenreID = b.GenreID

JOIN Loans l ON b.BookID = l.BookID

WHERE b.PublicationYear > 1980

ORDER BY g.GenreName ASC;

C5

SELECT m.MemberID, m.MemberName, m.Sex,

COALESCE(COUNT(DISTINCT l.BookID), 0) AS NumberOfBookCopies

FROM Members m

LEFT JOIN Loans l ON m.MemberID = l.MemberID

AND YEAR(l.LoanDate) = 2020

WHERE m.Sex = 'Female'

GROUP BY m.MemberID, m.MemberName, m.Sex

ORDER BY NumberOfBookCopies DESC, m.MemberName ASC;

C6

WITH LoanCounts AS (

SELECT

YEAR(LoanDate) AS Year,

MONTH(LoanDate) AS Month,

COUNT(*) AS NumberOfLoans

FROM Loans
GROUP BY YEAR(LoanDate), MONTH(LoanDate)

),

MaxLoans AS (

SELECT

Year,

MAX(NumberOfLoans) AS MaxLoans

FROM LoanCounts

GROUP BY Year

SELECT

lc.Year,

lc.Month,

lc.NumberOfLoans

FROM LoanCounts lc

JOIN MaxLoans ml ON lc.Year = ml.Year AND lc.NumberOfLoans = ml.MaxLoans

ORDER BY lc.Year ASC, lc.Month ASC;

C7:

SELECT

a.AuthorID,

a.AuthorName,

COALESCE(female_loans.NumberOfLoansByFemaleMembers, 0) AS
NumberOfLoansByFemaleMembers,

COALESCE(male_loans.NumberOfLoansByMaleMembers, 0) AS
NumberOfLoansByMaleMembers

FROM Authors a

LEFT JOIN (
SELECT

b.AuthorID,

COUNT(*) AS NumberOfLoansByFemaleMembers

FROM Loans l

JOIN Members m ON l.MemberID = m.MemberID

JOIN Books b ON l.BookID = b.BookID

WHERE YEAR(l.LoanDate) = 2020 AND m.Gender = 'Female'

GROUP BY b.AuthorID

) AS female_loans ON a.AuthorID = female_loans.AuthorID

LEFT JOIN (

SELECT

b.AuthorID,

COUNT(*) AS NumberOfLoansByMaleMembers

FROM Loans l

JOIN Members m ON l.MemberID = m.MemberID

JOIN Books b ON l.BookID = b.BookID

WHERE YEAR(l.LoanDate) = 2020 AND m.Gender = 'Male'

GROUP BY b.AuthorID

) AS male_loans ON a.AuthorID = male_loans.AuthorID

ORDER BY a.AuthorID;

C8:

CREATE PROCEDURE insertMember (

@memberID INT,

@memberName NVARCHAR(100),

@address NVARCHAR(200),

@email NVARCHAR(100),
@sex NVARCHAR(20),

@birthDate DATE

AS

BEGIN

-- Kiểm tra xem memberID đã tồn tại hay chưa

IF NOT EXISTS (SELECT 1 FROM Members WHERE memberID = @memberID)

BEGIN

-- Nếu chưa tồn tại, thực hiện chèn dữ liệu mới vào bảng Members

INSERT INTO Members (memberID, memberName, address, email, sex, birthDate)

VALUES (@memberID, @memberName, @address, @email, @sex, @birthDate);

END

-- Nếu đã tồn tại, không thực hiện chèn dữ liệu

END;
C9:

CREATE TRIGGER deleteGenres

ON Genres

AFTER DELETE

AS

BEGIN

-- Cập nhật bảng Books, đặt GenreID thành NULL với các sách thuộc thể loại bị xóa

UPDATE Books

SET GenreID = NULL

WHERE GenreID IN (SELECT GenreID FROM deleted);

END;

You might also like