You are on page 1of 2

Exercise 4:

a)
● create database book_store;
● use book_store
● Create table AUTHOR (author_id int primary key, name varchar(20), city varchar(20),
country varchar(20));
● Create table PUBLISHER (publisher_id int primary key, name varchar(20), city
varchar(20), country varchar(20));
● Create table CATEGORY (category_id int primary key, description varchar(40));
● Create table CATALOG (book_id int primary key, title varchar(20), author_id int,
publisher_id int, category_id int, year int, price int, foreign key(author_id) references
AUTHOR(author_id) on delete cascade,foreign key(publisher_id) references
PUBLISHER(publisher_id) on delete cascade,foreign key(category_id) references
CATEGORY(category_id) on delete cascade);
● Create table ORDER_DETAILS(order_no int primary key, book_id int, quantity int,foreign
key(book_id) references CATALOG(book_id) on delete cascade);

Output: Query OK, 0 rows affected (1.24 sec)

b)
● Insert into AUTHOR (author_id,name,city,country) values
(01,'abc','xyz','india'),(02,'bcd','wxy','china'),(03,'cde','vwx','USA'),(04,'def','uvw','japan'),(0
5,'efg','tuv','korea');
● Insert into PUBLISHER values
(01,'fgh','ree','india'),(02,'hff','tes','china'),(03,'llm','plj','USA'),(04,'def','dgv','japan'),(05,'trw'
,'tuv','korea');
● Insert into CATEGORY values (01,'fgh sdcs'),(02,'hff scs'),(03,'llm svsv'),(04,'def
svsdv'),(05,'trw sdvs');
● Insert into CATALOG values (01,'sfs scs',01,02,03,1999,200),(02,'sdvs
scs',02,03,04,1997,250),(03,'fbd scs',03,04,05,1969,280),(04,'hfffg
scs',04,05,01,1869,800),(05,'hhger scs',05,01,02,2005,699);
● Insert into ORDER_DETAILS values (01,03,2),(02,05,9),(03,04,2),(04,01,5),(05,02,1);

Output: Query OK, 5 rows affected (0.14 sec)


Records: 5 Duplicates: 0 Warnings: 0

c)
● SELECT * FROM AUTHOR A WHERE EXISTS (SELECT A1.author_id,
COUNT(A1.author_id) FROM AUTHOR A1, CATALOG C WHERE A1.author_id =
C.author_id AND A.author_id = A1.author_id AND C.year > 2000 AND C.price > (
SELECT AVG(price) FROM CATALOG) GROUP BY A1.author_id HAVING
COUNT(A1.author_id) >= 2);
Output: Empty set (0.00 sec)

d)
● Select distinct a.name from AUTHOR a, CATALOG c, ORDER_DETAILS o where
a.author_id=c.author_id and o.book_id = c.book_id and exists(select o.book_id,
SUM(o.quantity)from ORDER_DETAILS o1 where o1.book_id = o.book_id group by
book_id having SUM(o1.quantity) >= ALL (select SUM(quantity) from ORDER_DETAILS
group by book_id));

Output:
+------+
| name |
+------+
| efg |
+------+
1 row in set, 1 warning (0.01 sec)

e)
●UPDATE CATALOG SET price = (1.1) * price WHERE author_id = (SELECT author_id
FROM AUTHOR WHERE name = 'abc');
Output: Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0

You might also like