1 | --name: Rick van der Zwet
|
---|
2 | --sid: 0433373
|
---|
3 | --file: generate.sql
|
---|
4 | --vak/opdracht: Databases/ opdracht 1
|
---|
5 |
|
---|
6 | CREATE TABLE Coach(
|
---|
7 | ID integer,
|
---|
8 | Age integer,
|
---|
9 | Name char(20),
|
---|
10 | PRIMARY KEY( ID )
|
---|
11 | );
|
---|
12 |
|
---|
13 |
|
---|
14 | CREATE TABLE Sportsman(
|
---|
15 | ID integer,
|
---|
16 | Age integer,
|
---|
17 | Name char(20),
|
---|
18 | Country char(20),
|
---|
19 | Coach_id integer NOT NULL,
|
---|
20 | PRIMARY KEY( ID ),
|
---|
21 | FOREIGN KEY( Coach_id ) REFERENCES Coach( ID )
|
---|
22 | );
|
---|
23 |
|
---|
24 |
|
---|
25 | CREATE TABLE Sponsor(
|
---|
26 | ID integer,
|
---|
27 | Name char(20),
|
---|
28 | SupportSportsman_id integer,
|
---|
29 | Budget integer,
|
---|
30 | PRIMARY KEY( ID ),
|
---|
31 | FOREIGN KEY( SupportSportsman_id ) REFERENCES Sportsman( ID )
|
---|
32 | );
|
---|
33 |
|
---|
34 |
|
---|
35 | CREATE TABLE Sports_Branch(
|
---|
36 | Name char(20),
|
---|
37 | PRIMARY KEY( Name )
|
---|
38 | );
|
---|
39 |
|
---|
40 |
|
---|
41 | CREATE TABLE plays(
|
---|
42 | Sportsman_id integer,
|
---|
43 | Sports_Branch char(20),
|
---|
44 | PRIMARY KEY( Sportsman_id, Sports_Branch ),
|
---|
45 | FOREIGN KEY( Sportsman_id ) REFERENCES Sportsman( ID ) ON DELETE CASCADE,
|
---|
46 | FOREIGN KEY( Sports_Branch ) REFERENCES Sports_Branch( Name ) ON DELETE CASCADE
|
---|
47 | );
|
---|
48 |
|
---|
49 |
|
---|
50 | CREATE TABLE Sports_event(
|
---|
51 | ID integer,
|
---|
52 | Datum date,
|
---|
53 | Name char(20),
|
---|
54 | Location char(20),
|
---|
55 | PRIMARY KEY( ID )
|
---|
56 | );
|
---|
57 |
|
---|
58 |
|
---|
59 | CREATE TABLE participate(
|
---|
60 | Sportsman_id integer,
|
---|
61 | SportsEvent_id integer,
|
---|
62 | PRIMARY KEY( Sportsman_id, SportsEvent_id ),
|
---|
63 | FOREIGN KEY( Sportsman_id ) REFERENCES Sportsman( ID ) ON DELETE CASCADE,
|
---|
64 | FOREIGN KEY( SportsEvent_id ) REFERENCES Sports_event ( ID ) ON DELETE CASCADE
|
---|
65 | );
|
---|
66 |
|
---|
67 | CREATE TABLE won(
|
---|
68 | Sportsman_id integer,
|
---|
69 | Sports_event integer,
|
---|
70 | Sports_Branch char(20),
|
---|
71 | Metal char(20),
|
---|
72 | PRIMARY KEY( Sportsman_id, Sports_event, Sports_Branch ),
|
---|
73 | FOREIGN KEY( Sportsman_id ) REFERENCES Sportsman( ID ) ON DELETE CASCADE,
|
---|
74 | FOREIGN KEY( Sports_event ) REFERENCES Sports_event( ID ) ON DELETE CASCADE,
|
---|
75 | FOREIGN KEY( Sports_Branch ) REFERENCES Sports_Branch( Name ) ON DELETE CASCADE
|
---|
76 | );
|
---|
77 |
|
---|
78 |
|
---|
79 | --
|
---|
80 | --budget VIEW is een beetje in elkaar gehacked
|
---|
81 | --niet geheel duidelijk wat bedoeld werd.
|
---|
82 | --de view lijkt me wat overbodig hier,
|
---|
83 | --dit kan beter extern geregeld worden.
|
---|
84 | --
|
---|
85 |
|
---|
86 | CREATE TABLE Sponsor_login(
|
---|
87 | Name char(20),
|
---|
88 | Passwd char(20),
|
---|
89 | Sponsor_id integer,
|
---|
90 | PRIMARY KEY ( Name ),
|
---|
91 | FOREIGN KEY( Sponsor_id ) REFERENCES Sponsor( ID ) ON DELETE CASCADE
|
---|
92 | );
|
---|
93 |
|
---|
94 | CREATE VIEW budget( Name, Support_Sportsman_id, Budget )
|
---|
95 | AS SELECT S.Name, S.SupportSportsman_id, S.Budget
|
---|
96 | FROM Sponsor S, Sponsor_login L
|
---|
97 | WHERE L.sponsor_id = S.ID AND L.Passwd = 'externe_variable'
|
---|
98 | ;
|
---|
99 |
|
---|
100 | DROP VIEW budget;
|
---|
101 | DROP TABLE sponsor_login;
|
---|
102 |
|
---|
103 | --
|
---|
104 | --END of HACK
|
---|
105 | --
|
---|
106 |
|
---|
107 |
|
---|
108 | DROP TABLE won;
|
---|
109 | DROP TABLE participate;
|
---|
110 | DROP TABLE Sports_event;
|
---|
111 | DROP TABLE plays;
|
---|
112 | DROP TABLE Sports_Branch;
|
---|
113 | DROP TABLE Sponsor;
|
---|
114 | DROP TABLE Sportsman;
|
---|
115 | DROP TABLE Coach;
|
---|
116 |
|
---|