Advertisement
GamerBhai02

DBMS EXP 6

May 15th, 2025
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.60 KB | Source Code | 0 0
  1. Create table author (
  2.     author_id int primary key,
  3.     name varchar(50),
  4.     city varchar(50),
  5.     country varchar(50)
  6. );
  7. Create table publisher (
  8.     publisher_id int primary key,
  9.     name varchar(50),
  10.     city varchar(50),
  11.     country varchar(50)
  12. );
  13. Create table category (
  14.     category_id int primary key,
  15.     description varchar(100)
  16. );
  17. Create table catalog (
  18.     book_id int primary key,
  19.     title varchar(100),
  20.     author_id int,
  21.     publisher_id int,
  22.     category_id int,
  23.     year int,
  24.     price int,
  25.     foreign key (author_id) references author(author_id),
  26.     foreign key (publisher_id) references publisher(publisher_id),
  27.     foreign key (category_id) references category(category_id)
  28. );
  29. Create table order_details (
  30.     order_no int,
  31.     book_id int,
  32.     quantity int,
  33.     primary key (order_no, book_id),
  34.     foreign key (book_id) references catalog(book_id)
  35. );
  36. insert into author (author_id, name, city, country) values
  37. (1, 'Author A', 'City 1', 'Country 1'),
  38. (2, 'Author B', 'City 2', 'Country 2'),
  39. (3, 'Author C', 'City 3', 'Country 3'),
  40. (4, 'Author D', 'City 4', 'Country 4'),
  41. (5, 'Author E', 'City 5', 'Country 5');
  42. insert into publisher (publisher_id, name, city, country) values
  43. (1, 'Publisher A', 'City 1', 'Country 1'),
  44. (2, 'Publisher B', 'City 2', 'Country 2'),
  45. (3, 'Publisher C', 'City 3', 'Country 3'),
  46. (4, 'Publisher D', 'City 4', 'Country 4'),
  47. (5, 'Publisher E', 'City 5', 'Country 5');
  48. insert into category (category_id, description) values
  49. (1, 'Fiction'),(2, 'Non-Fiction'),(3, 'Science Fiction'),
  50. (4, 'Mystery'),(5, 'Thriller');
  51. insert into catalog (book_id, title, author_id, publisher_id, category_id, year, price) values
  52. (1, 'Book 1', 1, 1, 1, 2005, 100),(2, 'Book 2', 2, 1, 2, 2010, 120),
  53. (3, 'Book 3', 3, 3, 3, 2015, 150),(4, 'Book 4', 4, 4, 4, 2020, 200),
  54. (5, 'Book 5', 5, 5, 5, 2025, 250);
  55. insert into order_details (order_no, book_id, quantity)
  56. values (101, 1, 2),(102, 2, 3),
  57. (103, 3, 1),(104, 4, 4),(105, 5, 2);
  58. select a.author_id, a.name, a.city, a.country, count(c.book_id) as book_count from author a
  59. join catalog c on a.author_id = c.author_id where c.year > 2000
  60. group by a.author_id, a.name, a.city, a.country having count(c.book_id) >= 2;
  61. select a.author_id, a.name, a.city, a.country, c.title, od.quantity from order_details od
  62. join catalog c on od.book_id = c.book_id join author a on c.author_id = a.author_id
  63. where od.quantity = (select max(quantity) from order_details);
  64. update catalog set price = price * 1.10 where
  65. publisher_id = (select publisher_id from publisher where name = 'Publisher E');
  66. select * from catalog;
  67.  
  68.  
  69. Output:
  70.  
  71. +-----------+----------+--------+-----------+--------+----------+
  72. | author_id | name     | city   | country   | title  | quantity |
  73. +-----------+----------+--------+-----------+--------+----------+
  74. |         4 | Author D | City 4 | Country 4 | Book 4 |        4 |
  75. +-----------+----------+--------+-----------+--------+----------+
  76. +---------+--------+-----------+--------------+-------------+------+-------+
  77. | book_id | title  | author_id | publisher_id | category_id | year | price |
  78. +---------+--------+-----------+--------------+-------------+------+-------+
  79. |       1 | Book 1 |         1 |            1 |           1 | 2005 |   100 |
  80. |       2 | Book 2 |         2 |            1 |           2 | 2010 |   120 |
  81. |       3 | Book 3 |         3 |            3 |           3 | 2015 |   150 |
  82. |       4 | Book 4 |         4 |            4 |           4 | 2020 |   200 |
  83. |       5 | Book 5 |         5 |            5 |           5 | 2025 |   275 |
  84. +---------+--------+-----------+--------------+-------------+------+-------+
Tags: Exp 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement