Red123456789

PaP final

Dec 18th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. CREATE DATABASE Books14114034
  2.  
  3. USE Books14114034
  4. GO
  5. CREATE TABLE TheAuthors4
  6. (Id int NOT NULL IDENTITY,
  7. Name nvarchar(20) NOT NULL
  8. Primary key(Id));
  9. GO
  10.  
  11. CREATE TABLE TheBooks4
  12. (Id int IDENTITY,
  13. Name nvarchar(150) NOT NULL,
  14. Description varchar(MAX),
  15. AuthorId int NOT NULL FOREIGN KEY REFERENCES TheAuthors4(Id) ON UPDATE CASCADE,
  16. Primary key(Id));
  17. GO
  18.  
  19. CREATE TABLE TheBorrowers4(
  20. Id int IDENTITY,
  21. Name nvarchar(30) NOT NULL,
  22. Phone nvarchar(30) NOT NULL,
  23. Email nvarchar(30) NOT NULL,
  24. Occupation nvarchar(30),
  25. Age int,
  26. Primary key(Id));
  27. GO
  28.  
  29. Alter TABLE TheBorrowers4
  30. ADD CHECK(Age >18)
  31. GO
  32.  
  33. CREATE TABLE TheBorrowedBooks4(
  34. Id int IDENTITY,
  35. BookId int NOT NULL FOREIGN KEY REFERENCES TheBooks4(Id),
  36. BorrowerId int NOT NULL FOREIGN KEY REFERENCES TheBorrowers4(Id),
  37. DateOfBorrowing DATE DEFAULT GETDATE(),
  38. Returned tinyInt NOT NULL DEFAULT 0,
  39. DateOfReturning DATE,
  40. Primary key(Id));
  41. GO
  42.  
  43. INSERT INTO TheAuthors4(name) VALUES ('Ivan Vazov'),('Isaac Asimov'),('Terry Pratchet'),('Howard Lovecraft'),('Harley Stevens'),('Franz Kafka'),('Geo Milev');
  44. GO
  45.  
  46. INSERT INTO TheBooks4(Name,AuthorId,Description) VALUES
  47. ('Under the yoke',1,'The very first Bulgarian novel')
  48. ,('The Solar System and Back ',2,'A collection of science essays')
  49. ,('The Stars in their Courses',2,'A collection of seventeen scientific essays')
  50. ,('Spell it out',5, 'A dark comedy involving little wizard girls, psychic warfare, insanity and nihilism'),
  51. ('Halberts and katanas',5,'Collection of fun little short stories'),
  52. ('The Castle',6,'Alienation, unresponsive bureaucracy, the frustration of trying to conduct business with non-transparent, seemingly arbitrary controlling systems, and the futile pursuit of an unobtainable goal')
  53. ,('A Little Expressionist Calendar for the Year 1921',7,'Every month has its poem')
  54. ,('At the mountains of madness',4, 'The forzen deserts have never been so horyfing');
  55. GO
  56.  
  57. UPDATE TheBooks4 SET Description = 'The colour of magic' Where id = 8;
  58. GO
  59.  
  60. SELECT TheBooks4.Name,TheAuthors4.Name FROM TheBooks4 LEFT JOIN TheAuthors4 ON TheAuthors4.Id = TheBooks4.AuthorId
  61. GO
  62.  
  63. SELECT Name FROM TheAuthors4
  64. GO
  65.  
  66. INSERT INTO TheBorrowers4(Name,Phone,Email,Occupation,Age)
  67. VALUES ('John Lennon','0895666131','j_lennon.vr@gmail.com','musician',21),
  68. ('Wolfgang Mozart','0895535525','gang_of_wolves@gmail.com','composer',25),
  69. ('Leslie Claypool','0895999622','primussucks@gmail.com','wine entrepenuer',35),
  70. ('Jack White','0895656131','jackieIIIwhite@gmail.com','professional willie wonka',40),
  71. ('Ian Kilmister','0895199622','lemmy_killmaster@abv.com','rockstar',19)
  72. GO
  73. UPDATE TheBooks4 SET AuthorId = 3 Where id = 5
  74. GO
  75. SELECT Name,Phone FROM TheBorrowers4
  76. GO
  77. SELECT Phone FROM TheBorrowers4 WHERE Name = 'Wolfgang Mozart';
  78. GO
  79. INSERT INTO TheBorrowedBooks4(BookId, BorrowerId) VALUES
  80. (1,2),(2,3),(3,3),(4,4),(5,5),(7,5)
  81. GO
  82. UPDATE TheBorrowedBooks4 SET Returned = 1 WHERE BookId = 4
  83. GO
  84. UPDATE TheBorrowedBooks4 SET DateOfReturning = '2016-12-05' WHERE BookId =4
  85. GO
  86. UPDATE TheBorrowedBooks4 SET Returned = 1 WHERE BookId = 5
  87. GO
  88. UPDATE TheBorrowedBooks4 SET DateOfReturning = '2016-12-18' WHERE BookId =5
  89. GO
  90.  
  91. SELECT * FROM TheBorrowedBooks4
  92.  
  93. SELECT TheBooks4.Name,TheBorrowedBooks4.DateOfReturning FROM TheBooks4 LEFT JOIN TheBorrowedBooks4 ON TheBooks4.Id = TheBorrowedBooks4.BookId
  94. WHERE Returned = 1
  95. GO
  96.  
  97. DELETE FROM TheBooks4 WHERE Name = 'The colour of magic'
  98. GO
  99. SELECT COUNT(*) FROM TheBorrowedBooks4 Where Returned = 0
  100. GO
  101.  
  102.  
  103.  
  104. SELECT B.Name,BOR.Name, BOR.Phone, BB.DateOfBorrowing, BB.DateOfReturning, DateDiff(DAY,BB.DateOfBorrowing,BB.DateOfReturning) AS NumberOfDays FROM TheBorrowedBooks4 as BB
  105. INNER JOIN TheBooks4 as B ON B.Id = BB.BookId
  106. INNER JOIN TheBorrowers4 as Bor ON Bor.Id = BB.BorrowerId
  107. WHERE DateOfReturning IS NOT NULL
  108.  
  109. SELECT AVG(DateDiff(DAY,BB.DateOfBorrowing,BB.DateOfReturning)) AS NumberOfDays FROM TheBorrowedBooks4 as BB
  110. WHERE DateOfReturning IS NOT NULL
  111.  
  112.  
  113. SELECT TheBooks4.Name,TheAuthors4.Name FROM TheBooks4 LEFT JOIN TheAuthors4 ON TheAuthors4.Id=TheBooks4.AuthorId
  114. GROUP BY TheAuthors4.Name, TheBooks4.Name
  115. GO
Add Comment
Please, Sign In to add comment