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

Create Database Library

The document details the creation of database tables for a library management system including book, staff, category, borrower, student and shelf details. Transactions are created at save points and different types of database backups are also demonstrated.

Uploaded by

Daniel Wasihun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Create Database Library

The document details the creation of database tables for a library management system including book, staff, category, borrower, student and shelf details. Transactions are created at save points and different types of database backups are also demonstrated.

Uploaded by

Daniel Wasihun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

create database library_managment

use library_managment;

/* Creating database tables */

CREATE TABLE Book_Details


(
ISBN_Code int PRIMARY KEY,
Book_Title varchar(100),
Language varchar(10),
No_Copies_Actual int,
No_Copies_Current int,
Category_id int,
Publication_year int,
Shelf_Id int,
FOREIGN KEY (Shelf_Id) REFERENCES Shelf_Details(Shelf_Id),
FOREIGN KEY (Category_id) REFERENCES Category_Details(Category_id)
)

CREATE TABLE Staff_Details


(
Staff_Id int PRIMARY KEY,
Staff_Name varchar(50),
Password varchar(16)
)

CREATE TABLE Category_Details


( Category_Id int PRIMARY KEY,
Category_Name varchar(50)
)

CREATE TABLE Borrower_Details


(
Borrower_Id int PRIMARY KEY,
Student_Id varchar(10),
Book_Id int,
Borrowed_From date,
Borrowed_TO date,
Actual_Return_Date date,
Issued_by int ,
FOREIGN KEY (Issued_by) REFERENCES Staff_Details(Staff_Id),
FOREIGN KEY (Book_Id) REFERENCES Book_Details(ISBN_Code),
FOREIGN KEY (Student_Id) REFERENCES Student_Details(Student_Id)
)
Create TABLE Student_Details
(
Student_Id varchar(10) PRIMARY KEY,
Student_Name varchar(50),
Gender Varchar(20),
Date_Of_Birth date,
Department varchar(10),
contact_Number varchar(15)
)

Create Table Shelf_Details


(
Shelf_id int PRIMARY KEY,
Shelf_No int,
Floor_No int
)

INSERT INTO Book_details (ISBN_Code,Book_Title , Language , No_Copies_Actual ,


No_Copies_Current , Category_id ,
Shelf_Id, Publication_year )
VALUES('1','Programming Concept','English', 2 , 2 , 15 , 3 ,2006),
('2','Database ','English', 3 , 3 , 15 , 2 ,2004),
('3','Data Structure','English', 5 , 5 , 15 , 1 , 2008),
('4','Web Programing','English', 8 , 4 , 14 , 3 ,2001),
('5','Java Fx','English', 8 , 8 , 14 , 2 ,2012),
('6','Git Hub','English', 9 , 6 , 13 , 3 , 2002),
('7','C#','English', 30 , 20 , 14 , 2 , 2003);

Insert into STAFF_DETAILS ( Staff_Id , Staff_Name , Password)


values (1,'Ahmed Jemal','123456'),
(2,'Esayas Abyu ','654321'),
(3,'Daniel Wasihun','107064'),
(4,'Bilal Ebro','121621');

/* table */
/*drop table CATEGORY_DETAILS*/

INSERT INTO CATEGORY_DETAILS


(Category_Id , Category_Name )
VALUES(15,'Programming'),
(14,'Website Book'),
(13,'repository');

Insert into Borrower_Details


( Borrower_Id ,Student_Id, Book_Id , Borrowed_From , Borrowed_TO ,
Actual_Return_Date , Issued_by )
VALUES(1,'WDU145670',6,'01-Aug-2024','7-Aug-2024','7-Aug-
2024',2),
(2,'WDU145673',4,'02-Aug-2024','8-Aug-
2024',NULL,4),
(3,'WDU145674',5,'01-Aug-2024','7-Aug-2024','7-
Aug-2024',1),
(4,'WDU145672',2,'02-Aug-2024','8-Aug-
2024',NULL,2) ,
(5,'WDU145675',4,'01-Aug-2024','7-Aug-2024','7-
Aug-2024',4),
(6,'WDU145666',1,'02-Aug-2024','8-Aug-
2024',NULL,3) ;

Insert into STUDENT_DETAILS


( Student_Id , Student_Name , Gender , Date_Of_Birth , Department ,
contact_Number )
values ('WDU145670','Ahmed Ali','Male','04-Oct-
1995','SE','09681849871'),
('WDU145672','Daniel Wasihun','Male','23-Feb-
1994','CSE','09723476554'),
('WDU145673','Abebe Belete','Male','13-Jan-
1990','IT','0974566554'),
('WDU145674','Awol Dawit','Male','3-Oct-
1993','SE','09723476544'),
('WDU145675','Daniel Wasihun','Male','6-Aug-
1995','IT','09723476567'),
('WDU145666','Bilal Ebro','Male','9-Aug-
1991','IT','09723476523');

Insert into Shelf_Details


( Shelf_id , Shelf_No , Floor_No )
Values(1, 1, 1),
(2, 2, 1),
(3, 1, 2);

checkpoint;

select Borrower_Details.Borrower_id,Book_Details.Book_title
from Borrower_Details,Book_Details
where Borrower_Details.book_id=Book_Details.ISBN_Code

select * from staff_details


select *from student_details

use library_managment
select student.student_id, student.student_name, book.Book_Title,
staff.staff_name, b.Borrowed_To
from student_Details student, Staff_Details staff, Borrower_Details b,
book_details book
where book.ISBN_Code = b.book_id and b.Issued_by = staff.Staff_id;

select*from Shelf_Details;
--creating view

create view StudentView as select s.Student_Id, s.Student_Name


FROM Student_Details as s
INNER JOIN Borrower_Details as a ON s.Student_Id = a.Student_Id;

select * from StudentView

create view StudentBorrowedView as select Student_Details.Student_Id,


Student_Details.Student_Name ,Borrower_Details.Book_Id
FROM Student_Details
INNER JOIN Borrower_Details ON Student_Details.Student_Id =
Borrower_Details.Student_Id;

select * from StudentBorrowedView

create view StudentServerView as select Student_Details.Student_Id,


Student_Details.Student_Name ,Borrower_Details.Book_Id,Borrower_Details.Issued_by
FROM Student_Details
INNER JOIN Borrower_Details ON Student_Details.Student_Id =
Borrower_Details.Student_Id;
select * from StudentServerView

--3 Create roles and assign them to different users

-- Create login

create login Adminstrator with password = '123456';


create login Officer with password = '121621';
create login Manager with password = '107064';
create login Assistant with password = '654321';
create login Student with password='111111';

-- Create roles

create role Adminstrator;


create role Officer;
create role Manager;
create role Assistant;
create role Student;

-- Grant permissions to roles

grant select ,update ,insert ,delete on Borrower_Details to Adminstrator;


grant select ,update ,insert ,delete on book_details to Adminstrator;
grant select ,update ,insert ,delete on Shelf_Details to Adminstrator;
grant select ,update ,insert ,delete on staff_details to Adminstrator;
grant select ,update ,insert ,delete on student_Details to Adminstrator;

grant select ,update ,insert ,delete on Borrower_Details to Manager;


grant select ,update ,insert on book_details to Manager;
grant select ,update ,insert on student_Details to Manager;

grant select , update ,delete ,insert on Borrower_Details to Assistant ;


grant select on book_details to Assistant;

grant select ,update ,delete ,insert on Borrower_Details to Officer;


grant select ,update ,delete ,insert on book_details to Officer;

revoke delete ,update on Borrower_Details from Assistant;


revoke delete on student_details from Adminstrator;

grant select on book_details to Student

-- Create users for each customer and assign them to the respective roles

create user Ahmed for login Adminstrator;


exec sp_addrolemember 'Adminstrator','Ahmed';

create user Bilal for login Officer;


exec sp_addrolemember'Officer','Bilal';

create user Esayas for login Assistant;


exec sp_addrolemember'Assistant','Esayas';

create user Daniel for login Manager;


exec sp_addrolemember'Manager','Daniel'

-- 4 Create transaction at different save points and restore

BEGIN TRANSACTION;

UPDATE Borrower_Details SET Actual_Return_Date = '11-Aug-


2024' WHERE Student_Id = 'WDU145673';

SAVE TRANSACTION SavePoint1;

UPDATE STUDENT_DETAILS SET Student_Name = 'Dawit


Endashaw' WHERE Student_Id = 'WDU145673';

SAVE TRANSACTION SavePoint2;

UPDATE Book_Details SET Shelf_Id =1 WHERE ISBN_Code=


6;

-- Rollback to SavePoint1
ROLLBACK TRANSACTION SavePoint1;
-- view the result
SELECT * FROM Book_Details;
SELECT * FROM Borrower_Details;

COMMIT TRANSACTION;

--5 creating different types of backup

-- Full Backup
backup database library_managment
to disk = 'C:\Backup\library_managment_full.bak'
with format,
medianame = 'Library_backup',
Name = 'Full Backup of Library Managment System';

-- Differential Backup
backup database library_managment
to disk = 'C:\Backup\library_managment_diff.bak'
with differential,
medianame = 'Library_backup',
Name = 'Differential Backup of Library Managment System';
-- Transaction Log Backup
backup database library_managment
to disk = 'C:\Backup\library_managment_log.bak'
with format,
medianame = 'Library_backup',
Name = 'Transaction Log Backup of Library Managment System';

--7 Applying encryption and Stored procedure

-- Create a symmetric key


CREATE SYMMETRIC KEY staffkey
WITH ALGORITHM = AES_256
ENCRYPTION BY PASSWORD = 'encrypt12';

-- Open the symmetric key


OPEN SYMMETRIC KEY staffkey
DECRYPTION BY PASSWORD = 'encrypt12';

-- Encrypt a column
UPDATE STAFF_DETAILS
SET Password = EncryptByKey(Key_GUID('staffkey'), Password);
-- Decrypt a column
SELECT Password,
CONVERT(int, DecryptByKey(Password)) AS Password
FROM STAFF_DETAILS;

-- Close the symmetric key


CLOSE SYMMETRIC KEY staffkey;

-- create procedure
create procedure AddNewCustomer
@ISBN_Code int ,
@Book_Title varchar(100),
@Language varchar(10),
@No_Copies_Actual int,
@No_Copies_Current int,
@Category_id int,
@Publication_year int,
@Shelf_Id int
AS
BEGIN

INSERT INTO Book_details (ISBN_Code,Book_Title , Language ,


No_Copies_Actual , No_Copies_Current , Category_id , Shelf_Id, Publication_year )
VALUES
(@ISBN_Code,@Book_Title ,@Language,@No_Copies_Actual,@No_Copies_Current,@Categ
ory_id , @Publication_year, @Shelf_Id);
END;
/*

-- Apply encryption, stored procedure techniques Encryption Example

-- Create a symmetric key


CREATE SYMMETRIC KEY CustomerKey
WITH ALGORITHM = AES_256
ENCRYPTION BY PASSWORD = 'strongpassword123';

-- Open the symmetric key


OPEN SYMMETRIC KEY CustomerKey
DECRYPTION BY PASSWORD = 'strongpassword123';

-- Encrypt a column
UPDATE Customer
SET Email = EncryptByKey(Key_GUID('CustomerKey'), Email);

-- Decrypt a column
SELECT CustomerID,
CONVERT(VARCHAR(100), DecryptByKey(Email)) AS Email
FROM Customer;
-- Close the symmetric key
CLOSE SYMMETRIC KEY CustomerKey;
/*
-- commited transactions
begin transaction;
UPDATE Book_Details set Shelf_Id = 1
where ISBN_CODE = 2;
commit;
rollback;

-- uncommited transactions
begin transaction;
UPDATE Book_Details set Shelf_Id = 2
where ISBN_CODE = 6;
rollback;

-- create trigger
create trigger trigger_1 on database for drop_table
as print"you cann't drop table"
rollback;

drop trigger trigger_1 on database

create trigger Staff on database for drop_table, delete_table


as print"you are not to drop and delete tables"
rollback;
drop table student_Details ;

drop trigger staff on database */

You might also like