Advertisement
Mitsanski

Untitled

Feb 10th, 2022
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. /*
  2. Ex.1
  3.  
  4. create database testDB
  5.  
  6. */
  7.  
  8. /*
  9. Ex.2
  10.  
  11. create table readers (
  12. pk int primary key,
  13. name char(40) not null,
  14. fnum char(10) not null,
  15. spec char(30) not null
  16. );
  17. */
  18.  
  19. /*
  20. Ex.3
  21. create table books (
  22. pk int primary key,
  23. name char(60) not null,
  24. price money not null,
  25. isbn char(13),
  26. pyear int
  27. );
  28.  
  29. */
  30.  
  31.  
  32. /*
  33. Ex.4
  34. create table files (
  35. pk int primary key,
  36. daterent datetime not null,
  37. fk_r int not null foreign key references readers(pk) on delete cascade on update no action,
  38. fk_b int not null foreign key references books(pk) on delete cascade on update no action,
  39. tdelay int
  40. )
  41.  
  42. */
  43.  
  44. /*
  45. Ex.5
  46. insert into readers (pk, name, fnum, spec)
  47. values(1, 'Иван Димитров', 018010, 'ИНФ'),
  48. (2, 'Стела Илиева', 019010, 'КСТ'),
  49. (3, 'Димитър Стоев', 018011, 'ИНФ')
  50. */
  51. /*
  52. Ex.6
  53. update readers
  54. set fnum = 017010, spec = 'ФИЗ'
  55. where pk = 2;
  56. */
  57.  
  58. /*
  59. Ex.7
  60.  
  61. insert into books (pk, name, price, isbn, pyear)
  62. values(1, 'Delphi 6', 26, '0-14-020652-3', 2012),
  63. (2, 'The Road', 17, '1-4133-0454-0', 2013)
  64. */
  65. /*
  66. Ex.8
  67. update books
  68. set name = 'The words', pyear = 2014
  69. where pk = 2;
  70.  
  71. */
  72.  
  73. /*
  74. Ex.9
  75. insert into books (pk, name, price, isbn, pyear)
  76. values(3, 'DB Systems', 34, '0-13-187325-3', 2008),
  77. (4, 'Excel 2010', 29, '0-4704-7487-4', 2010),
  78. (5, 'SQL Server', 21, '0-2314-5515-X', 2012),
  79. (6, 'MS Excel XP', 18, '0-2314-8321-2', 2013)
  80. */
  81.  
  82. /*
  83. Ex.10
  84. delete from books where pk = 2;
  85. */
  86. /*
  87. Ex.11
  88. insert into files (pk, daterent, fk_r, fk_b, tdelay)
  89. values(1, '11-11-2013', 2, 3, null),
  90. (2, '11-11-2013', 2, 5, null),
  91. (3, '12-12-2013', 1, 4, null),
  92. (4, '12-12-2013', 1, 5, null),
  93. (5, '12-12-2013', 3, 3, null)
  94. */
  95.  
  96. /*
  97. Ex.12
  98. update files set tdelay = 14 where (fk_r = 2) and (fk_b = 3) and (daterent = '11-11-2013');
  99. update files set tdelay = 7 where (fk_r = 1) and (fk_b = 4) and (daterent = '12-12-2013');
  100. */
  101.  
  102. /*
  103. Ex. 13
  104. select name, isbn, price, pyear
  105. from books
  106. where(pyear >= 2009) and (pyear <= 2012)
  107. order by pyear desc, price asc;
  108. */
  109.  
  110. /*
  111. Ex.14
  112. select name, price, pyear
  113. from books
  114. order by pyear desc;
  115. */
  116.  
  117. /*
  118. Ex.15
  119. select name, price, pyear
  120. from books
  121. where name like '%Excel%'
  122. order by pyear desc;
  123. */
  124.  
  125. /*
  126. Ex.16
  127. select
  128. b.name as 'Book name',
  129. r.name as 'Reader name',
  130. f.daterent as 'Date Rent'
  131. from
  132. files as f join readers as r on f.fk_r = r.pk join books as b on f.fk_b = b.pk
  133. order by
  134. b.name desc
  135. */
  136.  
  137. /*
  138. Ex.17
  139. select
  140. f.daterent, r.name, b.name, b.isbn, f.tdelay
  141. from
  142. files as f join readers as r on f.fk_r = r.pk join books as b on f.fk_b = b.pk
  143. where
  144. f.tdelay >= 5
  145. order by
  146. r.name asc
  147. */
  148. /*
  149. Ex.18
  150. select
  151. b.name, count(f.fk_b) as 'Count rents'
  152. from
  153. books as b join files as f on f.fk_b = b.pk
  154. group by
  155. b.name
  156. */
  157.  
  158. /*
  159. Ex.19
  160. select
  161. r.name, count(f.fk_r) as 'Count rents'
  162. from
  163. readers as r join files as f on f.fk_r = r.pk
  164. group by
  165. r.name
  166.  
  167. */
  168.  
  169. /*
  170. Ex.20
  171. select
  172. r.spec, count(f.fk_b) as 'Count books', sum(b.price) as 'Sum price'
  173. from
  174. files as f join readers as r on f.fk_r = r.pk join books as b on f.fk_b = b.pk
  175. group by
  176. r.spec
  177. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement