Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. drop table Borrows;
  2. drop table Books;
  3. drop table Authors;
  4. drop table Types;
  5. drop table Students;
  6.  
  7.  
  8.  
  9.  
  10. CREATE TABLE Students(
  11. StudentID Number NOT NULL,
  12. Name Varchar2(30 ) NOT NULL,
  13. Surname Varchar2(30 ) NOT NULL,
  14. Email Varchar2(50) NOT NULL,
  15. Password Varchar2(30) NOT NULL,
  16. Birthdate Date NOT NULL,
  17. Gender Char(1) NOT NULL,
  18. Class Varchar2(10 ) NOT NULL
  19. )
  20. /
  21.  
  22. -- Add keys for table Students
  23.  
  24. ALTER TABLE Students ADD CONSTRAINT Key1 PRIMARY KEY (StudentID)
  25. /
  26.  
  27. -- Table Books
  28.  
  29. CREATE TABLE Books(
  30. BookID Number NOT NULL,
  31. ISBN Number NOT NULL,
  32. Name Varchar2(30 ) NOT NULL,
  33. Pagecount Number NOT NULL,
  34. AuthorID Number NOT NULL,
  35. TypeID Number NOT NULL
  36. )
  37. /
  38.  
  39. -- Add keys for table Books
  40.  
  41. ALTER TABLE Books ADD CONSTRAINT Key2 PRIMARY KEY (BookID)
  42. /
  43.  
  44. -- Table Authors
  45.  
  46. CREATE TABLE Authors(
  47. AuthorID Number NOT NULL,
  48. Name Varchar2(20 ) NOT NULL,
  49. Surname Varchar2(20 ) NOT NULL
  50. )
  51. /
  52.  
  53. -- Add keys for table Authors
  54.  
  55. ALTER TABLE Authors ADD CONSTRAINT Key3 PRIMARY KEY (AuthorID)
  56. /
  57.  
  58. -- Table Types
  59.  
  60. CREATE TABLE Types(
  61. TypeID Number NOT NULL,
  62. Name Varchar2(20) NOT NULL
  63. )
  64. /
  65.  
  66. -- Add keys for table Types
  67.  
  68. ALTER TABLE Types ADD CONSTRAINT Key4 PRIMARY KEY (TypeID)
  69. /
  70.  
  71. -- Table Borrows
  72.  
  73. CREATE TABLE Borrows(
  74. BorrowID Number NOT NULL,
  75. StudentID Number NOT NULL,
  76. BookID Number NOT NULL,
  77. TakenDate Date NOT NULL,
  78. BroughtDate Date
  79. )
  80. /
  81.  
  82.  
  83. ALTER TABLE Borrows ADD CONSTRAINT Key5 PRIMARY KEY (BorrowID)
  84. /
  85.  
  86.  
  87. ALTER TABLE Borrows ADD CONSTRAINT Relationship1 FOREIGN KEY (StudentID) REFERENCES Students (StudentID);
  88. ALTER TABLE Borrows ADD CONSTRAINT Relationship2 FOREIGN KEY (BookID) REFERENCES Books (BookID);
  89.  
  90. ALTER TABLE Books ADD CONSTRAINT Relationship3 FOREIGN KEY (AuthorID) REFERENCES Authors (AuthorID);
  91. ALTER TABLE Books ADD CONSTRAINT Relationship4 FOREIGN KEY (TypeID) REFERENCES Types (TypeID);
  92.  
  93.  
  94.  
  95. --AUTOINCREMENT STUDENTS
  96. drop sequence seq_auto_stud;
  97. CREATE SEQUENCE seq_auto_stud;
  98.  
  99. CREATE OR REPLACE TRIGGER auto_stud
  100. BEFORE INSERT ON Students
  101. FOR EACH ROW
  102. BEGIN
  103. SELECT seq_auto_stud.NEXTVAL
  104. INTO :new.StudentID
  105. FROM dual;
  106. END;
  107.  
  108. create or replace Trigger userInsertCheck
  109. before insert on STUDENTS
  110. is
  111. passwordNotComplex exception;
  112. emailInvalid exception;
  113. passchar char;
  114. emailchar char;
  115. checkupper byte := 0;
  116. checknumber byte := 0;
  117. BEGIN
  118. for i in 1..LENGTH(:new.Password)
  119. loop
  120. passchar:= substr(:new.Password,i,1)
  121. if passchar = UPPER(passchar) then
  122. checkupper := 1;
  123. end if;
  124.  
  125. if passchar like '%[^0-9]%'
  126. checknumber := 1;
  127. end if;
  128.  
  129. End Loop;
  130.  
  131. if checkupper = 0 and checknumber = 0 then
  132. raise passwordNotComplex;
  133. end if;
  134.  
  135. if :new.Email not like '%@%';
  136. raise emailInvalid;
  137. end if;
  138.  
  139.  
  140. exception
  141. when passwordNotComplex then raise_application_error(-21001,'Password muss Gross-/Kleinbuchstaben und eine Ziffer beinhalten!');
  142. when emailInvalid then raise_application_error(-21002,'Die Email ist fehlerhaft!');
  143. END;
  144.  
  145.  
  146. --AUTOINCREMENT BOOKS
  147. drop sequence seq_auto_book;
  148. CREATE SEQUENCE seq_auto_book;
  149.  
  150. CREATE OR REPLACE TRIGGER auto_book
  151. BEFORE INSERT ON Books
  152. FOR EACH ROW
  153.  
  154. BEGIN
  155. SELECT seq_auto_book.NEXTVAL
  156. INTO :new.BookID
  157. FROM dual;
  158. END;
  159.  
  160. -- AUTOINCREMENT AUTHORS
  161. drop sequence seq_auto_auth;
  162. CREATE SEQUENCE seq_auto_auth;
  163.  
  164. CREATE OR REPLACE TRIGGER auto_auth
  165. BEFORE INSERT ON Authors
  166. FOR EACH ROW
  167.  
  168. BEGIN
  169. SELECT seq_auto_auth.NEXTVAL
  170. INTO :new.AuthorID
  171. FROM dual;
  172. END;
  173.  
  174. --AUTOINCREMENT TYPES
  175. drop sequence seq_auto_type;
  176. CREATE SEQUENCE seq_auto_type;
  177.  
  178. CREATE OR REPLACE TRIGGER auto_type
  179. BEFORE INSERT ON Types
  180. FOR EACH ROW
  181.  
  182. BEGIN
  183. SELECT seq_auto_type.NEXTVAL
  184. INTO :new.TypeID
  185. FROM dual;
  186. END;
  187.  
  188. --AUTOINCREMENT BORROWS
  189. drop sequence seq_auto_borr;
  190. CREATE SEQUENCE seq_auto_borr;
  191.  
  192. CREATE OR REPLACE TRIGGER auto_borr
  193. BEFORE INSERT ON Borrows
  194. FOR EACH ROW
  195.  
  196. BEGIN
  197. SELECT seq_auto_borr.NEXTVAL
  198. INTO :new.BorrowID
  199. FROM dual;
  200. END;
  201. -- Add keys for table Borrows
  202.  
  203.  
  204. Insert into Students values(null,'Rogala','Kacper','kacper.rogala@gso.de','Kacper654', to_date('14.09.1999','dd.mm.yyyy'),'M','IAF62');
  205. Insert into Students values(null,'Dinges','Marvin','marvin.dinges@gso.de','Marvin1',to_date('02.08.1999','dd.mm.yyyy'),'M','IAF62');
  206. Insert into Students values(null,'Brandscheidt','Alexander','alexander.brandscheidt@gso.de','Alexander56',to_date('25.06.1999','dd.mm.yyyy'),'M','IAF62');
  207.  
  208. select* from students;
  209.  
  210. Insert into Authors values(null,'Ditgens','John');
  211. Insert into Authors values(null,'Koeksal','Safak');
  212. Insert into Authors values(null,'Yavuz','Eray');
  213.  
  214. select* from Authors;
  215.  
  216. Insert into Types values(null,'Drama');
  217. Insert into Types values(null,'Comedy');
  218. Insert into Types values(null,'Horror');
  219.  
  220. select* from Types;
  221.  
  222. Insert into Books values(null,1234567,'Er ist wieder da',340,1,1);
  223. Insert into Books values(null,092109876,'Pipi Langstrumpf',540,2,2);
  224. Insert into Books values(null,0909876,'Hansel und Gretel',120,3,3);
  225.  
  226. select * from Books;
  227.  
  228. insert into Borrows values(null,1,1,to_date('13.03.2019','dd.mm.yyyy'),null);
  229. insert into Borrows values(null,2,2,to_date('13.03.2019','dd.mm.yyyy'),to_date('23.03.2019','dd.mm.yyyy'));
  230. insert into Borrows values(null,3,3,to_date('13.03.2019','dd.mm.yyyy'),to_date('23.03.2019','dd.mm.yyyy'));
  231.  
  232. select * from Borrows;
  233.  
  234. drop view BucherAus;
  235.  
  236. create view BucherAus as
  237. select bk.Name, st.Surname
  238. from borrows br, students st, books bk
  239. where br.StudentID = st.StudentID AND br.BookID = bk.BookID;
  240.  
  241. Select * from BucherAus;
  242.  
  243. create view BucherInfo as
  244. select bk.Name, at.Name, tp.Name
  245. from Books bk, Authors at, Types tp
  246. where bk.AuthorID = at.AuthorID AND bk.TypeID = tp.TypeID;
  247.  
  248.  
  249. create or replace procedure BorrowBook(studentID number, bookID number)
  250. is
  251. alreadyBorrowedError exception;
  252. borrowid number := null;
  253. Begin
  254. select br.BorrowID into borrowid from borrows br where bookID = br.BookID AND br.BroughtID is NULL;
  255. if borrwoid is not null then
  256. raise alreadyBorrowedError;
  257. end if;
  258.  
  259. insert into borrows values(null,studentID, bookID, Sysdate(), null);
  260. COMMIT;
  261.  
  262. exception when alreadyBorrowedError then raise_application_error(-21000,'The book is already borrowed!');
  263. end;
  264.  
  265. create or replace procedure BroughtBack(BookID number)
  266. is
  267. begin
  268. Update BORROWS br
  269. set BroughtDate = Sysdate()
  270. where BookID = br.BookID AND br.BroughtDate is NULL;
  271. COMMIT;
  272. end;
  273.  
  274. Select st.surname, (
  275. Select tp.Name
  276. from Types tp
  277. where tp.TypeID = (
  278. Select TypeID from (
  279. Select bk.TypeID, count(bk.Name) c
  280. from Books bk, Borrows br
  281. where bk.BookID = br.BookID and br.StudentID = st.StudentID -- st.StudentID kann hier nicht genutzt werden, obwohl es außen deklariert ist?
  282. group by bk.TypeID
  283. order by c DESC
  284. ) where rownum <= 1
  285. )
  286. ) likes
  287. from Students st;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement