|
87 | 87 | netstat -nap | grep mysql
|
88 | 88 | ```
|
89 | 89 |
|
| 90 | + 也可以使用下面的命令查找是否有名为mysqld的进程。 |
| 91 | + |
| 92 | + ```Shell |
| 93 | + pgrep mysqld |
| 94 | + ``` |
| 95 | + |
90 | 96 | - 使用MySQL客户端工具连接服务器。
|
91 | 97 |
|
92 | 98 | 命令行工具:
|
|
177 | 183 | -- 创建学院表
|
178 | 184 | create table tb_college
|
179 | 185 | (
|
180 |
| - collid int not null auto_increment comment '编号', |
181 |
| - collname varchar(50) not null comment '名称', |
182 |
| - collmaster varchar(20) not null comment '院长', |
183 |
| - collweb varchar(511) default '' comment '网站', |
| 186 | + collid int auto_increment comment '编号', |
| 187 | + collname varchar(50) not null comment '名称', |
| 188 | + collmaster varchar(20) not null comment '院长', |
184 | 189 | primary key (collid)
|
185 | 190 | );
|
186 | 191 |
|
187 | 192 | -- 创建学生表
|
188 | 193 | create table tb_student
|
189 | 194 | (
|
190 |
| - stuid int not null comment '学号', |
191 |
| - stuname varchar(20) not null comment '姓名', |
192 |
| - stusex bit default 1 comment '性别', |
193 |
| - stubirth date not null comment '出生日期', |
194 |
| - stuaddr varchar(255) default '' comment '籍贯', |
195 |
| - collid int not null comment '所属学院', |
| 195 | + stuid int not null comment '学号', |
| 196 | + stuname varchar(20) not null comment '姓名', |
| 197 | + stusex boolean default 1 comment '性别', |
| 198 | + stubirth date not null comment '出生日期', |
| 199 | + stuaddr varchar(255) default '' comment '籍贯', |
| 200 | + collid int not null comment '所属学院', |
196 | 201 | primary key (stuid),
|
197 | 202 | foreign key (collid) references tb_college (collid)
|
198 | 203 | );
|
199 | 204 |
|
200 |
| - -- alter table tb_student add constraint fk_student_collid foreign key (collid) references tb_college (collid); |
201 |
| - |
202 | 205 | -- 创建教师表
|
203 | 206 | create table tb_teacher
|
204 | 207 | (
|
205 |
| - teaid int not null comment '工号', |
206 |
| - teaname varchar(20) not null comment '姓名', |
207 |
| - teatitle varchar(10) default '助教' comment '职称', |
208 |
| - collid int not null comment '所属学院', |
| 208 | + teaid int not null comment '工号', |
| 209 | + teaname varchar(20) not null comment '姓名', |
| 210 | + teatitle varchar(10) default '助教' comment '职称', |
| 211 | + collid int not null comment '所属学院', |
209 | 212 | primary key (teaid),
|
210 | 213 | foreign key (collid) references tb_college (collid)
|
211 | 214 | );
|
212 | 215 |
|
213 | 216 | -- 创建课程表
|
214 | 217 | create table tb_course
|
215 | 218 | (
|
216 |
| - couid int not null comment '编号', |
217 |
| - couname varchar(50) not null comment '名称', |
218 |
| - coucredit int not null comment '学分', |
219 |
| - teaid int not null comment '授课老师', |
| 219 | + couid int not null comment '编号', |
| 220 | + couname varchar(50) not null comment '名称', |
| 221 | + coucredit int not null comment '学分', |
| 222 | + teaid int not null comment '授课老师', |
220 | 223 | primary key (couid),
|
221 | 224 | foreign key (teaid) references tb_teacher (teaid)
|
222 | 225 | );
|
223 | 226 |
|
224 | 227 | -- 创建选课记录表
|
225 |
| - create table tb_score |
| 228 | + create table tb_record |
226 | 229 | (
|
227 |
| - scid int auto_increment comment '选课记录编号', |
228 |
| - stuid int not null comment '选课学生', |
229 |
| - couid int not null comment '所选课程', |
230 |
| - scdate datetime comment '选课时间日期', |
231 |
| - scmark decimal(4,1) comment '考试成绩', |
232 |
| - primary key (scid), |
233 |
| - foreign key (stuid) references tb_student (stuid), |
234 |
| - foreign key (couid) references tb_course (couid) |
| 230 | + recid int auto_increment comment '选课记录编号', |
| 231 | + sid int not null comment '选课学生', |
| 232 | + cid int not null comment '所选课程', |
| 233 | + seldate datetime default now() comment '选课时间日期', |
| 234 | + score decimal(4,1) comment '考试成绩', |
| 235 | + primary key (recid), |
| 236 | + foreign key (sid) references tb_student (stuid), |
| 237 | + foreign key (cid) references tb_course (couid), |
| 238 | + unique (sid, cid) |
235 | 239 | );
|
236 |
| - |
237 |
| - -- 添加唯一性约束(一个学生选某个课程只能选一次) |
238 |
| - alter table tb_score add constraint uni_score_stuid_couid unique (stuid, couid); |
239 | 240 | ```
|
240 | 241 |
|
241 | 242 | 2. DML
|
242 | 243 |
|
243 | 244 | ```SQL
|
244 |
| - |
245 | 245 | -- 插入学院数据
|
246 |
| - insert into tb_college (collname, collmaster, collweb) values |
247 |
| - ('计算机学院', '左冷禅', 'http://www.abc.com'), |
248 |
| - ('外国语学院', '岳不群', 'http://www.xyz.com'), |
249 |
| - ('经济管理学院', '风清扬', 'http://www.foo.com'); |
| 246 | + insert into tb_college (collname, collmaster) values |
| 247 | + ('计算机学院', '左冷禅'), |
| 248 | + ('外国语学院', '岳不群'), |
| 249 | + ('经济管理学院', '风清扬'); |
250 | 250 |
|
251 | 251 | -- 插入学生数据
|
252 | 252 | insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid) values
|
|
289 | 289 | (9999, '审计学', 3, 3366);
|
290 | 290 |
|
291 | 291 | -- 插入选课数据
|
292 |
| - insert into tb_score (stuid, couid, scdate, scmark) values |
| 292 | + insert into tb_record (sid, cid, seldate, score) values |
293 | 293 | (1001, 1111, '2017-09-01', 95),
|
294 | 294 | (1001, 2222, '2017-09-01', 87.5),
|
295 | 295 | (1001, 3333, '2017-09-01', 100),
|
|
304 | 304 | (1378, 1111, '2017-09-05', 82),
|
305 | 305 | (1378, 7777, '2017-09-02', 65.5),
|
306 | 306 | (2035, 7777, '2018-09-03', 88),
|
307 |
| - (2035, 9999, curdate(), null), |
308 |
| - (3755, 1111, date(now()), null), |
309 |
| - (3755, 8888, date(now()), null), |
| 307 | + (2035, 9999, default, null), |
| 308 | + (3755, 1111, default, null), |
| 309 | + (3755, 8888, default, null), |
310 | 310 | (3755, 9999, '2017-09-01', 92);
|
311 | 311 | ```
|
312 | 312 |
|
|
460 | 460 | - 隔离性:多个事务并发执行时,一个事务的执行不应影响其他事务的执行
|
461 | 461 | - 持久性:已被提交的事务对数据库的修改应该永久保存在数据库中
|
462 | 462 |
|
| 463 | +3. MySQL中的事务操作 |
| 464 | +
|
| 465 | + - 开启事务环境 |
| 466 | +
|
| 467 | + ```SQL |
| 468 | + start transaction |
| 469 | + ``` |
| 470 | +
|
| 471 | + - 提交事务 |
| 472 | +
|
| 473 | + ```Shell |
| 474 | + commit |
| 475 | + ``` |
| 476 | +
|
| 477 | + - 回滚事务 |
| 478 | +
|
| 479 | + ```SQL |
| 480 | + rollback |
| 481 | + ``` |
| 482 | +
|
463 | 483 | ### Python数据库编程
|
464 | 484 |
|
465 | 485 | 我们用如下所示的数据库来演示在Python中如何访问MySQL数据库。
|
|
0 commit comments