Database and Table Creation in Mysql-1
Database and Table Creation in Mysql-1
AIM:
To execute the basic queries like database creation, table creation and perform basic queries on
tables in MYSQL.
PROCEDURE:
1. Create database.
2. Create the needed Tables
A. Consider the following schema for a Library Database:
1. BOOK (Book_id, Title, Publisher_Name,Pub_Year)
2. BOOK_AUTHORS (Book_id,Author_Name)
3. PUBLISHER (Name, Address,Phone)
4. BOOK_COPIES(Book_id, Branch_id,No-of_Copies)
5. BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out,Due_Date)
6. LIBRARY_BRANCH (Branch_id, Branch_Name,Address)
3. Insert needed number of values (tuples) into the tables
4. Perform the various queries given below:
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number
of copies in each branch,etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 toJun2017
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.
5. Create a view of all books and its number of copie
QUERIES:
Create DataBase:
mysql> CREATE DATABASE LIBRARY;
Query OK, 1 row affected (0.02 sec)
To show database:
mysql> show databases;
Database
information_schema
library
mysql
performance_schema
sys
5rows in set (0.00 sec)
To set the database:
mysql> use library;
Database changed
Create Table:
1.mysql> create table publisher (name varchar (20) primary key, phone bigint,address varchar (20));
7. mysql> create table book_lending (date_out date, due_date date, book_id integer, branch_id integer,
card_no integer, foreign key (book_id) references book (book_id) on delete cascade, foreign key
(branch_id) references library_branch (branch_id) on delete cascade, foreign key (card_no) references
card (card_no) on delete cascade, primary key(book_id, branch_id, card_no));
To show tables:
Publisher table:
Library_branch table:
insert into library_branch values (10,‘RR NAGAR‘,‘BANGALORE‘);
insert into library_branch values (11,‘RNSIT‘,‘BANGALORE‘);
insert into library_branch values (12,‘RAJAJI NAGAR‘, ‘BANGALORE‘);
insert into library_branch values (13,‘MANIPAL‘,‘UDUPI‘)
To show Library_branch table:
mysql> select * from library_branch;
branch_id branch_name address
10 RR NAGAR BANGALORE
11 RNSIT BANGALORE
12 RAJAJI NAGAR BANGALORE
13 MANIPAL UDUPI
4 rows in set (0.00 sec)
Book_copies:
insert into book_copies values (10, 1, 10);
insert into book_copies values (5,1,11);
insert into book_copies values (2,2,12);
insert into book_copies values (5,2,13);
insert into book_copies values (7,3,13);
insert into book_copies values (1,5,10);
insert into book_copies values (3,4,11);
To show Book_copies table:
mysql> select * from BOOK_COPIES;
no_of_copies book_id | branch_id
10 1 10
5 1 11
2 2 12
5 2 13
7 3 13
3 4 11
1 5 10
5 OS PEARSON | GALVIN 1 10
4 CG GRUPOPLANETA EDWARD 3 11
ANGEL
2 ADBM MCGRAW-HILL NAVATHE 2 12
S
2 ADBM MCGRAW-HILL | NAVATHE 5 13
S
3 CN | PEARSON TANENBAUM 7 13
SQL>SELECT CARD_NO FROM BOOK_LENDING WHERE DATE_OUT BETWEEN '2007-01-01' AND '2007-06-
30'GROUP BY CARD_NO HAVING COUNT(*) <3;
CARD_NO
101
1 row in set (0.00 sec)
3.Query to Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
SQL>create view v_books as select b.book_id, b.title, c.no_of_copies from book b, book_copies c,
library_branch l where b.book_id=c.book_id and c.branch_id=l.branch_id;
RESULT:
The queries for creating database, table and basic queries have been executed successfully.