0% found this document useful (0 votes)
33 views6 pages

332CIS Lab Activity Sheet

1. Two tables (Publisher and Book) are created in a library_db database to store publisher and book data. 2. Sample data is inserted into the tables, including 3 publishers and 3 books with publisher references. 3. A series of SQL queries are run to return the number of books published by each publisher, find the price of a specific book, find publisher information for USA publishers, and join the tables to find the publisher name for each book.

Uploaded by

azaldeenalglal
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)
33 views6 pages

332CIS Lab Activity Sheet

1. Two tables (Publisher and Book) are created in a library_db database to store publisher and book data. 2. Sample data is inserted into the tables, including 3 publishers and 3 books with publisher references. 3. A series of SQL queries are run to return the number of books published by each publisher, find the price of a specific book, find publisher information for USA publishers, and join the tables to find the publisher name for each book.

Uploaded by

azaldeenalglal
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/ 6

332CIS Lab Activity Sheet 1 ( 5 points )

Student Name:
Answer only

Step 1/7

Database creation:-

create database if not exists library_db;


use library_db;

 Explanationfor step 1

Note:- A demo database called "library_db" is created for this


assignment
Step 2/7

1. Table creation:-

Publisher table –

create table Publisher(


Publisher_ID int not null auto_increment,
Publisher_Name varchar(70) not null,
Country varchar(30) not null,
primary key(Publisher_ID)
)engine=innodb default charset=utf8;

Book table –

create table Book(


Book_ID int not null auto_increment,
Publisher_ID int not null,
Book_Name varchar(200) not null,
Price int not null,
primary key(Book_ID),
foreign key(Publisher_ID) references Publisher(Publisher_ID) on
update cascade on delete cascade
)engine=innodb default charset=utf8;

 Explanationfor step 2

Note:- Two tables i.e. "Publisher" and "Book" are created as per the
requirement of the question 1.
Step 3/7

Insert data into tables:-

In Publisher table –

insert into Publisher(Publisher_Name, Country) values


('Wiley', 'USA'),
('Pearson', 'USA'),
('Mcgraw hill', 'USA');

In Book table –

insert into Book(Publisher_ID, Book_Name, Price) values


(3, 'Accounting Principles', 125),
(1, 'Intro to DB Management', 90),
(2, 'Database Systems', 150);
 Explanationfor step 3

Note:- Required data are inserted into the above tables.


Step 4/7

2. Return the number of books published by each publisher

select Publisher_ID as "Publisher ID", count(Book_Name) as "No. of


Books published" from Book group by Publisher_ID order by
Publisher_ID;

Result -

Step 5/7

3. Find the price of the book "Database Systems"

select Book_Name as "Book Name", Price from Book where


Book_Name="Database Systems";

Result -
Step 6/7

4. Find all information of publishers located in USA

select * from Publisher where Country="USA";

Result -

Step 7/7

5. Find the publisher name for each book

select distinct b.Book_Name as "Book Name", p.Publisher_Name as


"Publisher Name" from Book b join Publisher p on
b.Publisher_ID=p.Publisher_ID order by b.Book_Name;

Result -

Final answer

Points to be noted:-
1) All Necessary primary key and foreign key constraints were added
during the creation of tables, please check them,

2) Results of each SQL queries were given with each query to help
you understand each of the queries.

3) JOIN statement is used to join two or more tables based on one or


more common

You might also like