/magstudentportal/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/magstudentportal/trunk

« back to all changes in this revision

Viewing changes to src/main/resources/create_tables.sql

  • Committer: Gustav Hartvigsson
  • Date: 2017-08-14 13:56:15 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20170814135615-0cg8hm4j9qywl4hg
* finished the current version of the DB SQL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
 
3
 
CREATE TABLE IF NOT EXISTS students (
 
1
/*
 
2
  This file should be executed to create the tables. It contains the constraints
 
3
  for foreign keys and composite keys.
 
4
 
 
5
  This file is going to be used to generate the JPA stuffs.
 
6
 
 
7
  Changes to this file can come at any time.
 
8
 */
 
9
 
 
10
CREATE TABLE IF NOT EXISTS student (
4
11
  id                      SERIAL PRIMARY KEY,
5
 
  user_name               VARCHAR (32) NOT NULL UNIQUE ,
 
12
  user_name               VARCHAR (32) NOT NULL UNIQUE,
6
13
  pwd                     VARCHAR (256) NOT NULL,
7
14
 
8
15
  /* name */
11
18
 
12
19
  class                   VARCHAR (32) /* This could be a foreign key :-) */
13
20
 
14
 
  /* address *//*
 
21
  /* address */
 
22
  /*
15
23
  address_country         VARCHAR (32) NOT NULL,
16
24
  address_city            VARCHAR (32) NOT NULL,
17
25
  address_street          VARCHAR (32) NOT NULL,
21
29
  */
22
30
);
23
31
 
 
32
CREATE TABLE IF NOT EXISTS staff_type (
 
33
  id                      SERIAL PRIMARY KEY,
 
34
  name                    VARCHAR (32) UNIQUE
 
35
);
 
36
 
 
37
 
 
38
/* Add staff types to database ************************************************/
 
39
INSERT INTO staff_type (id, name) VALUES (0, 'NONE'); /* We need to have the
 
40
                                                         zero:th value be none
 
41
                                                         so we don't get caught
 
42
                                                         in the 'null trap'.
 
43
                                                         */
 
44
INSERT INTO staff_type (name) VALUES ('Admin');
 
45
INSERT INTO staff_type (name) VALUES ('Teacher');
 
46
/******************************************************************************/
 
47
 
24
48
CREATE TABLE IF NOT EXISTS staff (
25
49
  id                      SERIAL PRIMARY KEY,
26
 
  user_name               VARCHAR(32)  NOT NULL UNIQUE,
27
 
  pwd                     VARCHAR(256) NOT NULL,
 
50
  user_name               VARCHAR (32)  NOT NULL UNIQUE,
 
51
  pwd                     VARCHAR (256) NOT NULL,
28
52
 
29
53
  staff_type              BIGINT,
30
54
 
34
58
  /*
35
59
  employee_nr             VARCHAR (32) NOT NULL UNIQUE,
36
60
  */
37
 
  FOREIGN KEY (staff_type) REFERENCES staff_type (id)
 
61
  CONSTRAINT staff_type_kf  FOREIGN KEY (staff_type)
 
62
                            REFERENCES staff_type (id)
38
63
);
39
64
 
40
 
CREATE TABLE IF NOT EXISTS staff_type (
 
65
 
 
66
 
 
67
CREATE TABLE IF NOT EXISTS course (
41
68
  id                      SERIAL PRIMARY KEY,
42
 
  name                    VARCHAR (32)
 
69
 
 
70
  name                    VARCHAR (30) UNIQUE,
 
71
  description             TEXT
 
72
);
 
73
 
 
74
CREATE TABLE IF NOT EXISTS lecture (
 
75
  course BIGINT           NOT NULL,
 
76
  start_time              TIME NOT NULL,
 
77
  end_time                TIME NOT NULL,
 
78
 
 
79
  CONSTRAINT course_fk    FOREIGN KEY (course)
 
80
                          REFERENCES course (id),
 
81
 
 
82
  CONSTRAINT lecture_pk   PRIMARY KEY (course, start_time, end_time)
 
83
);
 
84
 
 
85
CREATE TABLE IF NOT EXISTS teaches (
 
86
  course BIGINT           NOT NULL,
 
87
  staff BIGINT            NOT NULL,
 
88
 
 
89
  CONSTRAINT course_fk    FOREIGN KEY (course)
 
90
                          REFERENCES course (id),
 
91
 
 
92
  CONSTRAINT staff_fk     FOREIGN KEY (staff)
 
93
                          REFERENCES course (id),
 
94
 
 
95
  CONSTRAINT teaches_pk   PRIMARY KEY (course, staff)
 
96
);
 
97
 
 
98
CREATE TABLE IF NOT EXISTS student_attendance (
 
99
  lecture_course          BIGINT NOT NULL,
 
100
  lecture_start_time      TIME NOT NULL,
 
101
  lecture_end_time        TIME NOT NULL,
 
102
 
 
103
 
 
104
  student                 BIGINT NOT NULL,
 
105
 
 
106
  CONSTRAINT lecture_fk   FOREIGN KEY (lecture_course,
 
107
                                      lecture_start_time,
 
108
                                      lecture_end_time)
 
109
                          REFERENCES lecture (course,
 
110
                                              start_time,
 
111
                                              end_time),
 
112
 
 
113
  CONSTRAINT student_fk   FOREIGN KEY (student)
 
114
                          REFERENCES student (id)
43
115
);
44
116