The document describes the design of a database for a library management system. It includes the SQL queries used to:
1. Create tables for user login details, book details, book ratings, and user privileges.
2. Define primary keys, foreign keys, indexes and other attributes of the tables.
3. Insert sample data into the tables.
4. Provide examples of optimized queries on the login and book details tables.
2. SYNTAX CREATING DATABASE
QUERY:
CREATE DATABASE `LIBRARY`;
SQL SYNTAX FOR CREATING FIRST TABLE: LOGIN
QUERY:
CREATE TABLE `LIBRARY`.`LOGIN` (`ID` INT NOT NULL ,`USER_NAME` VARCHAR(
30 ) NOT NULL ,`PASSWORD` VARCHAR( 30 ) NOT NULL ,`PRIVILEGE` VARCHAR( 10
) NOT NULL) ENGINE = MYISAM ;
SETTING PRIMARY KEY FOR ID
QUERY:
ALTER TABLE `LOGIN` ADD PRIMARY KEY (`ID` );
SETTING AUTO INCREMENTINFG FOR ID
QUERY:
ALTER TABLE `login` CHANGE `ID` `ID` INT (11) NOT NULL AUTO_INCREMENT;
SETTING ENGINE AS INNODB
QUERY:
ALTER TABLE `login` ENGINE = INNODB;
INDEXING
QUERY:
ALTER TABLE `login` ADD INDEX (`USER_NAME` );
ALTER TABLE `login` ADD INDEX(`PRIVILEGE`)
SQL FOR INSERTING VALUES IN TO TABLE
QUERY:
INSERT INTO `LIBRARY`.`login` (`ID`, `USER_NAME`, PASSWORD`, `PRIVILEGE`)
VALUES ('1', 'ADMINISTRATOR', 'ADMINISTRATOR', 'ADMIN');
INSERT INTO `LIBRARY`.`login` (`ID`, `USER_NAME`,`PASSWORD`, `PRIVILEGE`)
VALUES ('2', 'FACULTY', 'FACULTY', 'FACULTY');
3. INSERT INTO `LIBRARY`.`login` (`ID`, `USER_NAME`, `PASSWORD`,
`PRIVILEGE`) VALUES ('3', 'RESHMA', 'RESHMA', 'STUDENT');
INSERT INTO `LIBRARY`.`LOGIN` (`ID`, `USER_NAME`, `PASSWORD`,
`PRIVILEGE`) VALUES ('4', 'RESKHA', 'REKHA', 'STUDENT');
SQL SYNTAX FOR CREATING TABLE: PRIVILEGE
QUERY:
CREATE TABLE `LIBRARY`.`PRIVILEGE` (`ID` INT NOT NULL, `PRIVILEGE`
VARCHAR(10) NOT NULL) ENGINE = MYISAM;
SETTING ENGINE =INNODB
QUERY:
ALTER TABLE `PRIVILEGE`ENGINE = INNODB;
SETTING PRIVILEGE UNIQUE VALUE
QUERY:
ALTER TABLE `PRIVILEGE` ADD UNIQUE(`PRIVILEGE`)
Query took 0.2112 sec
INSERTING
QUERY:
INSERT INTO `LIBRARY`.`PRIVILEGE` (`ID`, `PRIVILEGE`) VALUES ('1',
'ADMIN'), ('2', 'FACULTY');
INSERT INTO `LIBRARY`.`PRIVILEGE` (`ID`, `PRIVILEGE`) VALUES ('3',
'STUDENT');
SQL SYNTAX FOR CREATING TABLE: BOOK_DETAILS
QUERY:
CREATE TABLE `LIBRARY`.`BOOK_DETAILS` (`ID` INT NOT NULL ,
`TITLE` VARCHAR( 30 ) NOT NULL ,`AUTHOR` TEXT NOT NULL ,`PUBLISHER` VARC
HAR( 30 ) NOT NULL ,`KEYWORD` VARCHAR( 60 ) NOT NULL ,`ISBN` INT NOT NUL
L ,`ABSTRACT` VARCHAR( 60 ) NOT NULL ,`YEAR_OF_PU
BLISHING` DATE NOT NULL);
4. SETTING ENGINE AS INNODB
QUERY:
ALTER TABLE `book_details` ENGINE = INNODB;
SETTING PRIMARY KEY
QUERY:
ALTER TABLE `BOOK_DETAILS` ADD PRIMARY KEY (`ID` );
SETTING ISBN NO: UNIQUE
ALTER TABLE ` BOOK_DETAILS ` ADD UNIQUE (`ISBN`);
INDEXING
QUERY:
ALTER TABLE ` BOOK_DETAILS ` ADD INDEX (`PRIVILEGE` );
ALTER TABLE ` BOOK_DETAILS ` ADD INDEX (`YEAR_OF_PUBLISHING` );
ALTER TABLE ` BOOK_DETAILS ` ADD INDEX (`ABSTRACT` );
ALTER TABLE ` BOOK_DETAILS ` ADD INDEX (`ISBN` );
ALTER TABLE ` BOOK_DETAILS ` ADD INDEX (`PUBLISHER` );
ALTER TABLE ` BOOK_DETAILS ` ADD INDEX (`TITLE` );
INSERTING VALUES
QUERY:
INSERT INTO `LIBRARY`.`book_details` (`ID`, `TITLE`, `AUTHOR`,
`PUBLISHER`, `KEYWORD`, `ISBN`, `ABSTRACT`, `YEAR_OF_PU BLISHING`,
`PATH`, `PRIVILEGE`) VALUES ('1001', ' Harry Potter', ' J. K.
Rowling', ' cityhills', ' wizard,adventures', ' 978', ' a series of
seven fantasy novels', ' 2000-03-08', ' F:S 4DATABASE
ADMINISTRATION', ' STUDENT')
INSERT INTO `LIBRARY`.`book_details` (`ID`, `TITLE`, `AUTHOR`,
`PUBLISHER`, `KEYWORD`, `ISBN`, `ABSTRACT`, `YEAR_OF_PU BLISHING`,
5. `PATH`, `PRIVILEGE`) VALUES ('1002', 'A Tale of Two Cities',
'Charles Dickens', 'macgrew', 'two cities', '787', 'wonderful
novel', '2011-03-21', 'F:S 4DATABASE ADMINISTRATION',
'STUDENT');
CREATING TABLE: BOOK_RATINGS
QUERY:
CREATE TABLE `LIBRARY`.`BOOK_RATINGS` (`ID` INT NOT NULL, `BID` INT NOT
NULL, `Ratings` INT NOT NULL, `Note` VARCHAR(50) NOT NULL, `U_ID` INT
NOT NULL, PRIMARY KEY (`ID`)) ENGINE = MyISAM;
SETTING ENGINE AS InnoDB
QUERY:
ALTER TABLE `book_ratings`ENGINE = InnoDB;
ADDING ONE MORE FIELD NAMED PRIVILEGE TO THE TABLE BOOK_RATINGS TO SET THE PRIVILEGE OF
EACH USER
QUERY:
ALTER TABLE ` BOOK_RATINGS ` ADD `PRIVILEGE` VARCHAR
(10) NOT NULL AFTER `UNAME;
SETTING FIELD PRIVILEGE A DEFAULT VALUE: STUDENT
QUERY:
ALTER TABLE `book_details` CHANGE `PRIVILEGE` `PRIVILEGE` VARCHAR(10)
CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT
'STUDENT'
SETTING AUTO INCREMENT FOR ID
QUERY:
ALTER TABLE ` BOOK_RATINGS ` CHANGE `ID` `ID` INT (11) NOT NULL
AUTO_INCREMENT;
INDEXING
QUERY:
ALTER TABLE `BOOK_RATINGS` ADD INDEX (`BID`);
6. ALTER TABLE `BOOK_RATINGS` ADD INDEX (`Ratings`);
ALTER TABLE `BOOK_RATINGS` ADD INDEX (`Note`);
ALTER TABLE `BOOK_RATINGS` ADD INDEX (`U_ID`);
ALTER TABLE ` BOOK_RATINGS ` ADD INDEX (`PRIVILEGE` );
ALTER TABLE `Book_Ratings` ADD UNIQUE (`U_ID`);
(Query took 0.2212 sec)
INSERTING VALUES
QUERY:
INSERT INTO `LIBRARY`.`BOOK_RATINGS` (`ID`, `BID`, `RATINGS`, `NOTE`,
`U_ID`) VALUES ('1', '1002', '4', 'GREAT STORY', '2', 'FACULTY');
INSERT INTO `LIBRARY`.`BOOK_RATINGS` (`ID`, `BID`, `RATINGS`, `NOTE`,
`U_ID`) VALUES ('2', '1001', '3', 'SEVEN FANTASY NOVELS', '2',
'FACULTY');
INSERT INTO `LIBRARY`.`BOOK_RATINGS` (`ID`, `BID`, `RATINGS`, `NOTE`,
`U_ID`) VALUES ('3', '1002', '3', 'BED TIME STORIES', '3', 'STUDENT');
INSERT INTO `LIBRARY`.`BOOK_RATINGS` (`ID`, `BID`, `RATINGS`, `NOTE`,
`U_ID`, `PRIVILEGE`) VALUES ('4', '1001', '1', 'NOT ACCEPTABLE
CONCEPT', '1', 'ADMIN');
INSERT INTO `LIBRARY`.`BOOK_RATINGS` (`ID`, `BID`, `RATINGS`, `NOTE`,
`U_ID`, `PRIVILEGE`) VALUES ('5', '1001', '2', 'NICE STORY', '4',
'STUDENT');
DATABASE : LIBRARY
7. TABLE: LOGIN
Query took 0.0005 sec
TABLE: BOOK_DETAILS
Query took 0.0006 sec
TABLE: BOOK_RATING
Query took 0.0005 sec
9. DATABASE AFTER CREATING TABLE PRIVILEG
DATABASE AFTER SETTING RELATIONSHIPS
1. U_ID (FK) IN TABLE BOOK_RATING REFERENCING TO ID(PK) OF TABLE
LOGIN
2. FINAL DATABASE
12. QUERY OPTIMIZATION
TABLE: LOGIN
Q: GET ALL THE RESULTS CONTAINING ID AS 3 USERNAME AS RESHMA AND
PRIVILEGE AS STUDENT
QUERY:
SEARCH QUERY TIME
NO
1 SELECT * FROM `login` WHERE `ID` = 3; Query took 0.0005 sec
2 SELECT * FROM `LOGIN` WHERE Query took 0.0007 sec
`USER_NAME` LIKE 'RESHMA'
3 SELECT * FROM `LOGIN` WHERE Query took 0.0005 sec
`PRIVILEGE` LIKE 'STUDENT'
4 SELECT * FROM `LOGIN` WHERE `ID` = 1 Query took 0.0005 sec
AND `USER_NAME` LIKE 'RESHMA'
5 SELECT * FROM `LOGIN` WHERE `ID` = 3 Query took 0.0005 sec
AND `PRIVILEGE` LIKE 'STUDENT'
6 SELECT * FROM `LOGIN` WHERE `ID` = 3 Query took 0.0005 sec
AND `USER_NAME` LIKE 'RESHMA' AND
`PRIVILEGE` LIKE 'STUDENT'
IN THE ABOVE CASE ALL QUERIES EXCEPT THE SECOND QUERY CAN BE
CONSIDERED AS THE OPTIMISED QUERY. HOWEVER THE BEST OPTIMISED QUERY
WILL BE THE 6TH ONE.
TABLE: BOOK_DETAILS
Q: GETTING ALL RESULTS FOR WHICH ID AS 1001, TITLE AS HARRY POTTER,
AUTHOR AS J.K ROWLING, KEYWORD AS WIZARD, ADVENTURES, ISBN AS 978,
PUBLISHER AS CITYHILLS, AND YEAR OF PUBLISHING AS 2000-03-08.
13. SEARCH QUERY TIME
NO
1 SELECT * FROM `book_details` WHERE 0.0006 sec
`ID` = 1001
2 SELECT * FROM `BOOK_DETAILS` WHERE 0.0006 sec
`TITLE` LIKE 'HARRY POTTER'
3 SELECT * FROM `BOOK_DETAILS` WHERE 0.0005 sec
`AUTHOR` LIKE 'J. K. ROWLING'
4 SELECT * FROM `BOOK_DETAILS` WHERE 0.0006 sec
`PUBLISHER` LIKE 'CITYHILLS'
5 SELECT * FROM `BOOK_DETAILS` WHERE 0.0005 sec
`KEYWORD` LIKE 'WIZARD,ADVENTURES'
6 SELECT * FROM `BOOK_DETAILS` WHERE 0.0008 sec
`ISBN` = 978
7 SELECT * FROM `BOOK_DETAILS` WHERE 0.0006 sec
`ABSTRACT` LIKE 'A SERIES OF SEVEN
FANTASY NOVELS'
8 SELECT * FROM `BOOK_DETAILS` WHERE 0.0006 sec
`YEAR_OF_PU BLISHING` = '2000-03-08'
9 SELECT * FROM `BOOK_DETAILS` WHERE 0.0005 sec
`ID` = 1001 AND `TITLE` LIKE 'HARRY
POTTER'
10 SELECT * FROM `BOOK_DETAILS` WHERE 0.0005 sec
`ID` = 1001 AND `TITLE` LIKE 'HARRY
POTTER' AND `AUTHOR` LIKE 'J. K.
ROWLING'
11 SELECT * FROM `BOOK_DETAILS` WHERE 0.0008 sec
`ID` = 1001 AND `TITLE` LIKE 'HARRY
POTTER' AND `AUTHOR` LIKE 'J. K.
ROWLING' AND `PUBLISHER` LIKE
'CITYHILLS'
12 SELECT * FROM `BOOK_DETAILS` WHERE 0.0006 sec
`ID` = 1001 AND `TITLE` LIKE 'HARRY
POTTER' AND `AUTHOR` LIKE 'J. K.
ROWLING' AND `PUBLISHER` LIKE
'CITYHILLS' AND `KEYWORD` LIKE
'WIZARD,ADVENTURES' AND `ISBN` = 978
13 SELECT * FROM `BOOK_DETAILS` WHERE 0.0006 sec
`ID` = 1001 AND `TITLE` LIKE 'HARRY
POTTER' AND `AUTHOR` LIKE 'J. K.
ROWLING' AND `PUBLISHER` LIKE
'CITYHILLS' AND `KEYWORD` LIKE
'WIZARD,ADVENTURES' AND `ISBN` = 978
14. AND `YEAR_OF_PU BLISHING` = '2000-03-
08'
14 SELECT * FROM `BOOK_DETAILS` WHERE 0.0006 sec
`ID` = 1001 AND `TITLE` LIKE 'HARRY
POTTER' AND `AUTHOR` LIKE 'J. K.
ROWLING' AND `ISBN` = 978
AND`PUBLISHER` LIKE 'CITYHILLS' AND
`KEYWORD` LIKE 'WIZARD,ADVENTURES' AND
`YEAR_OF_PU BLISHING` = '2000-03-08'
OPTIMISED QUERY ARE 3, 5,9,10.
TABLE: RATINGS
Q: GET ALL RATING DETAILS OF BOOK ID=1002
SEARCH QUERY TIME
NO
1 SELECT * FROM `book_ratings` WHERE Query took 0.0007 sec
`BID` = 1002;
SUBMITTED BY
RESHMA JOHNEY
ROLL NO: 24
reshmajohney@sify.com