-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinitMySQL.sh
182 lines (150 loc) · 5.92 KB
/
initMySQL.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
DATABASE=classhelper
while getopts "u:p:d:P:" arg
do
case $arg in
u)
USERNAME=-u$OPTARG
;;
p)
PASSWORD=-p$OPTARG
;;
P)
PORT=-P$OPTARG
;;
d)
DATABASE=$OPTARG
;;
?)
echo "unknow argument!"
exit 1
;;
esac
done
mysql $USERNAME $PASSWORD $PORT << EOF
CREATE DATABASE $DATABASE ;
/*使用utf8mb4编码*/
ALTER DATABASE $DATABASE CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
use $DATABASE;
/*==============================================================*/
/* Table: answers */
/*==============================================================*/
create table answers
(
ans_ex_id int not null,
ans_stu_id int not null,
ans_stu_name char(40),
ans_score int,
ans_answer text,
ans_time datetime default CURRENT_TIMESTAMP,
primary key (ans_ex_id, ans_stu_id)
);
/*==============================================================*/
/* Table: courses */
/*==============================================================*/
create table courses
(
coz_account char(20),
course_id int not null auto_increment,
course_name char(20),
course_time text,
course_info text,
student_num int default 0,
primary key (course_id)
);
/*==============================================================*/
/* Table: coz_stu */
/*==============================================================*/
create table coz_stu
(
cs_coz_id int not null,
cs_stu_id bigint not null,
cs_stu_name char(40) not null,
primary key (cs_coz_id, cs_stu_id)
);
/*==============================================================*/
/* Table: exams */
/*==============================================================*/
create table exams
(
exam_id int not null auto_increment,
ex_coz_id int,
exam_name char(20),
exam_state tinyint default 0,
exam_time datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
exam_question text,
exam_statistics text,
exam_stu_num int default 0,
primary key (exam_id)
);
/*==============================================================*/
/* Table: signup */
/*==============================================================*/
create table signup
(
sign_id int not null auto_increment,
sign_time datetime default CURRENT_TIMESTAMP,
sg_coz_id int,
sg_stu_num int default 0,
primary key (sign_id)
);
/*==============================================================*/
/* Table: stu_sign */
/*==============================================================*/
create table stu_sign
(
ss_sign_id int not null,
ss_stu_id int not null,
ss_stu_name char(40),
stu_sign_time datetime not null default CURRENT_TIMESTAMP,
primary key (ss_stu_id, ss_sign_id)
);
/*==============================================================*/
/* Index: Index_sign_id */
/*==============================================================*/
create index Index_sign_id on stu_sign
(
ss_sign_id
);
/*==============================================================*/
/* Table: users */
/*==============================================================*/
create table users
(
account char(20) not null,
password char(32) not null,
username char(40) not null,
email char(40),
phone char(20),
admin tinyint default 0,
primary key (account)
);
alter table courses add constraint FK_user_course foreign key (coz_account)
references users (account) on delete cascade on update restrict;
alter table coz_stu add constraint FK_Reference_6 foreign key (cs_coz_id)
references courses (course_id) on delete cascade on update restrict;
alter table exams add constraint FK_course_exam foreign key (ex_coz_id)
references courses (course_id) on delete cascade on update restrict;
alter table signup add constraint FK_couser_sign foreign key (sg_coz_id)
references courses (course_id) on delete cascade on update restrict;
create trigger addans after insert
on answers for each row
update exams set exam_stu_num=exam_stu_num+1
where exam_id=new.ans_ex_id;
create trigger addstudent after insert
on coz_stu for each row
update courses set student_num=student_num+1 where course_id=new.cs_coz_id;
create trigger delstudent after delete
on coz_stu for each row
update courses set student_num=student_num-1 where course_id=old.cs_coz_id;
create trigger delete_exam after delete
on exams for each row
delete from answers where ans_ex_id=old.exam_id;
create trigger delete_sign after delete
on signup for each row
delete from stu_sign where ss_sign_id=old.sign_id;
create trigger stusign after insert
on stu_sign for each row
update signup set sg_stu_num=sg_stu_num+1
where sign_id=new.ss_sign_id;
EOF