0% found this document useful (0 votes)
23 views9 pages

Database and Table Creation in Mysql-1

The document outlines the process of creating a Library Database in MySQL, including the creation of tables for books, authors, publishers, and lending records. It details the insertion of data into these tables and provides examples of various SQL queries to retrieve and manipulate this data. The execution of these queries has been confirmed as successful.

Uploaded by

subathra devi
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)
23 views9 pages

Database and Table Creation in Mysql-1

The document outlines the process of creating a Library Database in MySQL, including the creation of tables for books, authors, publishers, and lending records. It details the insertion of data into these tables and provides examples of various SQL queries to retrieve and manipulate this data. The execution of these queries has been confirmed as successful.

Uploaded by

subathra devi
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/ 9

DATABASE AND TABLE CREATION IN MYSQL

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));

Query OK, 0 rows affected (0.05 sec)


2. mysql> create table book (book_id integer primary key, title varchar (20),publisher_name
varchar(20), pub_year varchar (20), foreign key(publisher_name) references publisher (name) on
delete cascade);
Query OK, 0 rows affected (0.07 sec)
3. mysql> create table book_authors (book_id integer, author_name varchar (20),foreign
key(book_id) references book (book_id) on delete cascade, primary key (book_id,
author_name));
Query OK, 0 rows affected (0.05 sec)
4.mysql> create table library_branch (branch_id integer primary key, branch_name varchar (50),
address varchar (50));
Query OK, 0 rows affected (0.05 sec)
5. mysql> create table book_copies (no_of_copies integer, book_id integer, branch_id integer,
foreign key (book_id) references book (book_id) on delete cascade, foreign key(branch_id) references
library_branch(branch_id) on delete cascade, primary key (book_id, branch_id));

Query OK, 0 rows affected (0.07 sec)

6. mysql> create table card (card_no integer primary key);

Query OK, 0 rows affected (0.04 sec)

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));

Query OK, 0 rows affected (0.08 sec)

To show tables:

mysql> show tables;


Tables_in_library
Book
book_authors
book_copies
book_lending
card
library_branch
publisher
7 rows in set (0.00 sec)
3. Insertion of Values toTables

Publisher table:

insert into publisher values (‘MCGRAW-HILL‘, 9989076587,’BANGALORE‘);


insert into publisher values (‘PEARSON’, 9889076565,’NEWDELHI‘);
insert into publisher values(‘RANDOMHOUSE‘,7455679345,‘HYDERABAD‘);
insert into publisher values (‘HACHETTE LIVRE‘, 8970862340,‘CHENNAI‘);
insert into publisher values (‘GRUPOPLANETA‘,7756120238,’BANGALORE‘);
To show publisher table:
mysql> select *from publisher;
NAME PHONE ADDRESS
GRUPOPLANETA 7756120238 BANGALORE
HACHETTE LIVRE 8970862340 CHENNAI
MCGRAW-HILL 9989076587 BANGALORE
PEARSON 9889076565 NEWDELHI
RANDOMHOUSE 7455679345 HYDERABAD
Book table:
insert into book values (1,‘DBMS‘ , ‘MCGRAW-HILL‘ ,‘JAN-2017’);
insert into book values (2,‘ADBMS‘, ‘MCGRAW-HILL‘,‘JUN-2016‘);
insert into book values (3,‘CN‘, ‘PEARSON‘,‘SEP-2016‘);
insert into book values (4,‘CG‘, ‘GRUPO PLANETA‘,‘SEP-2015‘);
insert into book values (5,‘OS‘, ‘PEARSON‘,‘MAY-2016‘);
To show book table
mysql> select *from book;
book_id title publisher_name |pub_year
1 DBMS MCGRAW-HILL JAN-2017
2 ADBMS MCGRAW-HILL JUN-2016
3 CN PEARSON SEP-2016
4 CG GRUPOPLANETA SEP-2015
5 OS PEARSON MAY-2016
5 rows in set (0.00 sec
Book_authors table:
insert into book_authors values (1 ,‘NAVATHE‘);
insert into book_authors values (2,‘NAVATHE‘);
insert into book_authors values (3,‘TANENBAUM‘);
insert into book_authors values (4,‘EDWARD ANGEL‘);
insert into book_authors values (5,‘GALVIN‘);
To show Book_authors table
mysql> select *from book_authors;
book_id author_name
1 NAVATHE
2 NAVATHE
3 TANENBAUM
4 EDWARD ANGEL
5 GALVIN
5 rows in set (0.00 sec)

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

7 rows in set (0.00 sec)


Card table:
INSERT INTO CARD VALUES(100);
INSERT INTO CARD VALUES (101);
INSERT INTO CARD VALUES (102);
To show card table
mysql> select * from card;
card_no
100
101
102
3 rows in set (0.00 sec)
Book_Lending:
INSERT INTO BOOK_LENDING VALUES ('2007-01-17', '2007-06-17', 1, 10, 101);
INSERT INTO BOOK_LENDING VALUES ('2011-01-17', '2011-03-17', 3, 13, 101);
INSERT INTO BOOK_LENDING VALUES ('2021-02-17', '2021-04-17', 2, 13, 101);
INSERT INTO BOOK_LENDING VALUES ('2015-03-17', '2015-07-17', 4, 11, 101);
INSERT INTO BOOK_LENDING VALUES ('2012-04-17', '2012-05-17', 1, 11, 102);
To show Book_Lending :
mysql> select * from book_lending;
date_out due_date book_id branch_id card_no
2007-01-17 2007-06-17 1 10 101
2012-04-17 2012-05-17 1 11 102
2021-02-17 2021-04-17 2 13 101
2011-01-17 2011-03-17 3 13 101
2015-03-17 2015-07-17 4 11 101
5 rows in set (0.00 sec)
4. Basic Queries
1. Query to Retrieve details of all books in the library – id, title, name of publisher, authors,
number of copies in each branch,etc.
SQL>SELECT B.BOOK_ID, B.TITLE, B.PUBLISHER_NAME, A.AUTHOR_NAME, C.NO_OF_COPIES,
L.BRANCH_ID FROM BOOK B, BOOK_AUTHORS A, BOOK_COPIES C, LIBRARY_BRANCH l WHERE
B.BOOK_ID=A.BOOK_ID AND B.BOOK_ID=C.BOOK_ID AND L.BRANCH_ID=C.BRANCH_ID;

BOOK_I TITLE PUBLISHER_NAM AUTHOR_NAM NO_OF_COPIE BRANCH_I


D E E S D

1 DBMS MCGRAW-HILL NAVATHE 10 10

5 OS PEARSON | GALVIN 1 10

1 DBMS MCGRAW-HILL NAVATHE 5 11

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

7 rows in set (0.00 sec)


2. Query to Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 toJun2017

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.

mysql> delete from book where book_id=3;


Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM BOOK;
book_id title publisher_name |pub_year
1 DBMS MCGRAW-HILL JAN-2017
2 ADBMS MCGRAW-HILL JUN-2016
4 CG GRUPOPLANETA SEP-2015
5 OS PEARSON MAY-2016
4 rows in set (0.00 sec)
4. Query to Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.

SQL>CREATE VIEW V_PUBLICATION AS SELECT PUB_YEAR FROM BOOK;

Query OK, 0 rows affected (0.02 sec)


mysql> select * from v_publication;
PUB_YEAR
JAN-2017
JUN-2016
SEP-2015
MAY-2016
4 rows in set (0.00 sec)
5.Query to Create a view of all books and its number of copies that are currently available in the
Library.

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;

Query OK, 0 rows affected (0.01 sec)


mysql> select * from v_books;
book_id | title no_of_copies
1 DBMS 10
1 DBMS 5
2 ADBMS 2
2 ADBMS 5
4 CG 3
5 OS 1
6 rows in set (0.00 sec)

RESULT:

The queries for creating database, table and basic queries have been executed successfully.

You might also like