1. 程式人生 > >mysql 建立表之間關系 練習 2

mysql 建立表之間關系 練習 2

data gen prim size clas sca reference utf ima

創建數據庫db6

create database db6 charset=utf8;

user db6;

技術分享圖片

# 創建班級表

mysql> create table class(cid int primary key auto_increment,caption varchar(50) not null unique);
Query OK, 0 rows affected (0.15 sec)

mysql> select * from class;
Empty set (0.00 sec)

mysql> insert into class(caption) values
(三年二班),(一年三班),(三年一班); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from class; +-----+--------------+ | cid | caption | +-----+--------------+ | 2 | 一年三班 | | 3 | 三年一班 | | 1 | 三年二班 | +-----+--------------+ 3 rows in set (0.00 sec)

# 創建老師表

mysql> create table teacher(tid int primary key auto_increment,tname varchar(50) not null);
Query OK, 0 rows affected (0.11 sec)


mysql> insert into teacher(tname) values(李老師),(吳老師),(陳老師);
Query OK, 3 rows affected (0.12 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select
* from teacher; +-----+-----------+ | tid | tname | +-----+-----------+ | 1 | 李老師 | | 2 | 吳老師 | | 3 | 陳老師 | +-----+-----------+ 3 rows in set (0.00 sec)

多對一關系建表

# 學生表與班級表建立關系

創建學生表

mysql> create table student(
sid int primary key auto_increment,
sname varchar(20) not null,
gender enum(,) not null,
class_id int not null,
foreign key(class_id) references class(cid) 
on delete cascade 
on update cascade);
Query OK, 0 rows affected (0.06 sec)

mysql> insert into student(sname,gender,class_id) values(小紅,,1),(小李,,1),(小剛,,2);
Query OK, 3 rows affected (0.36 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from student;
+-----+--------+--------+----------+
| sid | sname  | gender | class_id |
+-----+--------+--------+----------+
|   1 | 小紅   ||        1 |
|   2 | 小李   ||        1 |
|   3 | 小剛   ||        2 |
+-----+--------+--------+----------+
3 rows in set (0.00 sec)

# 老師表和課程表建立關系

創建課程表

mysql> create table course(
cid int primary key auto_increment,
cname varchar(20) not null,
tearch_id int not null,
foreign key(tearch_id) references teacher(tid)
on delete cascade on update cascade); Query OK, 0 rows affected (0.12 sec

插入記錄 並查詢

mysql> insert into course(cname,tearch_id) values(生物,1),(體育,1),(物理,2);
Query OK, 3 rows affected (0.11 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from course;
+-----+--------+-----------+
| cid | cname  | tearch_id |
+-----+--------+-----------+
|   1 | 生物   |         1 |
|   2 | 體育   |         1 |
|   3 | 物理   |         2 |
+-----+--------+-----------+
3 rows in set (0.00 sec)

建立多對多關系

# 建立一張score表 與 學生表 課程表建立關系

mysql> create table score(
sid int not null unique auto_increment,
student_id int not null,
corse_id int not null,
number char(16) not null,
foreign key(student_id) references student(sid) 
on delete cascade 
on update cascade,
foreign key(corse_id) references course(cid) 
on delete cascade 
on update cascade,
primary key(student_id,corse_id)
);

插入記錄

mysql> insert into score(student_id,corse_id,number) values(1,1,60),(1,2,59),(2,2,100);
Query OK, 3 rows affected (0.70 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
|   1 |          1 |        1 | 60     |
|   2 |          1 |        2 | 59     |
|   3 |          2 |        2 | 100    |
+-----+------------+----------+--------+
3 rows in set (0.00 sec)

mysql 建立表之間關系 練習 2