Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. create database BookShop;
  2. use BookShop;
  3.  
  4. create table countries (
  5. id int unsigned not null auto_increment primary key,
  6. name varchar(50) not null unique
  7. );
  8.  
  9. create table authors (
  10. id int unsigned not null auto_increment primary key,
  11. name varchar(255) not null,
  12. surname varchar(255) not null,
  13. countryId int unsigned not null,
  14. constraint foreign key authors_countryId (countryId)
  15. references countries (id)
  16. );
  17.  
  18. create table themes (
  19. id int unsigned not null auto_increment primary key,
  20. name varchar(100) not null unique
  21. );
  22.  
  23. create table books (
  24. id int unsigned not null auto_increment primary key,
  25. name varchar(255) not null,
  26. pages int not null default 0,
  27. price decimal(10, 2) not null,
  28. publishDate date not null,
  29. authorId int unsigned not null,
  30. themeId int unsigned not null,
  31. constraint foreign key books_authorId (authorId)
  32. references authors (id),
  33. constraint foreign key books_themeId (themeId)
  34. references themes (id)
  35. );
  36.  
  37. create table shops (
  38. id int unsigned not null auto_increment primary key,
  39. name varchar(255) not null,
  40. countryId int unsigned not null,
  41. constraint foreign key shops_countryId (countryId)
  42. references countries (id)
  43. );
  44.  
  45. create table sales (
  46. id int unsigned not null auto_increment primary key,
  47. price decimal(10, 2) not null,
  48. quantity int not null,
  49. saleDate datetime not null default current_timestamp,
  50. bookId int unsigned not null,
  51. shopId int unsigned not null,
  52. constraint foreign key sales_bookId (bookId)
  53. references books (id),
  54. constraint foreign key sales_shopId (shopId)
  55. references shops (id)
  56. );
  57.  
  58. delimiter //
  59. create procedure setCountries()
  60. begin
  61. insert into countries (name) values
  62. ('Россия'), ('Украина'), ('Беларусь');
  63. end//
  64. delimiter ;
  65.  
  66. delimiter //
  67. create procedure setShops()
  68. begin
  69. set @i = 1;
  70. set @maxId = (select max(id) from countries);
  71. loop1: loop
  72. insert into shops (name, countryId)
  73. values (concat('shop', @i), floor(rand() * @maxId) + 1);
  74. set @i = @i + 1;
  75. if @i > 10 then leave loop1; end if;
  76. end loop loop1;
  77. end//
  78. delimiter ;
  79.  
  80. delimiter //
  81. create procedure setAuthors()
  82. begin
  83. set @i = 1;
  84. set @maxId = (select max(id) from countries);
  85. loop1: loop
  86. insert into authors (name, surname, countryId)
  87. values (
  88. concat('author', @i),
  89. concat('surname', @i),
  90. floor(rand() * @maxId) + 1
  91. );
  92. set @i = @i + 1;
  93. if @i > 100 then leave loop1; end if;
  94. end loop loop1;
  95. end//
  96. delimiter ;
  97.  
  98. delimiter //
  99. create procedure setThemes()
  100. begin
  101. insert into themes (name)
  102. values ('Детектив'), ('Роман'), ('Фэнтези');
  103. end//
  104. delimiter ;
  105.  
  106. delimiter //
  107. create procedure setBooks()
  108. begin
  109. set @i = 1;
  110. loop1: loop
  111. set @authorId = (
  112. select id from authors order by rand() limit 1
  113. );
  114. set @themeId = (
  115. select id from themes order by rand() limit 1
  116. );
  117. insert into books (
  118. name, pages, price, publishDate, authorId, themeId
  119. ) values (
  120. concat('book', @i), floor(rand() * 500) + 20,
  121. rand() * 500000 / 100 + 100,
  122. current_timestamp(), @authorId, @themeId
  123. );
  124. set @i = @i + 1;
  125. if @i > 500 then leave loop1; end if;
  126. end loop loop1;
  127. end//
  128. delimiter ;
  129.  
  130. call setBooks();
  131.  
  132. delimiter //
  133. create procedure setSales()
  134. begin
  135. set @i = 1;
  136. loop1: loop
  137. set @bookId = (select id from books order by rand() limit 1);
  138. set @price = (select price from books where id = @bookId);
  139. set @shopId = (select id from shops order by rand() limit 1);
  140. set @quantity = (select floor(rand() * 100) + 1);
  141. insert into sales (price, quantity, saleDate, bookId, shopId)
  142. values (@price, @quantity, current_timestamp(), @bookId, @shopId);
  143. set @i = @i + 1;
  144. if @i > 1000 then leave loop1; end if;
  145. end loop loop1;
  146. end//
  147. delimiter ;
  148.  
  149. call setCountries();
  150. call setShops();
  151. call setAuthors();
  152. call setThemes();
  153. call setBooks();
  154. call setSales();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement