码迷,mamicode.com
首页 > 数据库 > 详细

数据库之 表与表之间的关系

时间:2017-09-08 00:20:58      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:分享   ssid   custom   primary   管理员   ssi   arc   多对一   用户表   

表1 foreign key 表2
则表1的多条记录对应表2的一条记录,即多对一

利用foreign key的原理我们可以制作两张表的多对多,一对一关系
多对多:
    表1的多条记录可以对应表2的一条记录
    表2的多条记录也可以对应表1的一条记录

一对一:
    表1的一条记录唯一对应表2的一条记录,反之亦然

分析时,我们先从按照上面的基本原理去套,然后再翻译成真实的意义,就很好理解了

一、多对一或者一对多(左边表的多条记录对应右边表的唯一一条记录)

需要注意的:1.先建被关联的表,保证被关联表的字段必须唯一。

      2.在创建关联表,关联字段一定保证是要有重复的。

其实上一篇博客已经举了一个多对一关系的小例子了,那我们在用另一个小例子来回顾一下。

这是一个书和出版社的一个例子,书要关联出版社(多个书可以是一个出版社,一个出版社也可以有好多书)。

谁关联谁就是谁要按照谁的标准。

技术分享

书要关联出版社
被关联的表
create  table press(
id int primary key auto_increment,
name char(20)
);
关联的表
create table book(
book_id int primary key auto_increment,
book_name varchar(20),
book_price int,
press_id int,
constraint Fk_pressid_id foreign key(press_id) references press(id)
on delete cascade
on update cascade
);

插记录
insert into press(name) values(新华出版社),
                              (海燕出版社),
                              (摆渡出版社),
                              (大众出版社);
insert into book(book_name,book_price,press_id) values(Python爬虫,100,1),
                                                       (Linux,80,1),
                                                       (操作系统,70,2),
                                                       (数学,50,2),
                                                       (英语,103,3),
                                                       (网页设计,22,3);

运行结果截图:

技术分享

二、一对一

例子一:用户和管理员(只有管理员才可以登录,一个管理员对应一个用户)

管理员关联用户

 技术分享

===========例子一:用户表和管理员表=========
先建被关联的表
create table user(
id int primary key auto_increment, #主键自增
name char(10)
);
在建关联表
create table admin(
id int primary key auto_increment,
user_id int unique,
password varchar(16),
foreign key(user_id) references user(id)
on delete cascade
on update cascade
);
insert into user(name) values(susan1),
                             (susan2),
                             (susan3),
                             (susan4),
                             (susan5),
                             (susan6);
insert into admin(user_id,password) values(4,sds156),
                                          (2,531561),
                                          (6,f3swe);

 

运行结果截图:

技术分享

例子二:学生表和客户表

技术分享

 

========例子二:学生表和客户表=========
create table customer(
id int primary key auto_increment,
name varchar(10),
qq int unique,
phone int unique
);
create table student1(
sid int primary key auto_increment,
course char(20),
class_time time,
cid int unique,
foreign key(cid) references customer(id)
on delete cascade
on update cascade
);
insert into customer(name,qq,phone) values(小小,13564521,11111111),
                                          (嘻哈,14758254,22222222),
                                          (王维,44545522,33333333),
                                          (胡军,545875212,4444444),
                                          (李希,145578543,5555555),
                                          (李迪,754254653,8888888),
                                          (艾哈,74545145,8712547),
                                          (啧啧,11147752,7777777);
insert into student1(course,class_time,cid) values(python,08:30:00,3),
                                                 (python,08:30:00,4),
                                                 (linux,08:30:00,1),
                                                 (linux,08:30:00,7);

 

运行结果截图:

技术分享

三、多对多(多条记录对应多条记录)

书和作者(我们可以再创建一张表,用来存book和author两张表的关系)

要把book_id和author_id设置成联合唯一

联合唯一:unique(book_id,author_id)

联合主键:alter table t1 add primary  key(id,avg)

多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
  
关联方式:foreign key+一张新的表

技术分享

技术分享技术分享

========书和作者,另外在建一张表来存书和作者的关系
#被关联的
create table book1(
id int primary key auto_increment,
name varchar(10),
price float(3,2)
);
#========被关联的
create table author(
id int primary key auto_increment,
name char(5)
);
#========关联的
create table author2book(
id int primary key auto_increment,
book_id int not null,
author_id int not null,
unique(book_id,author_id),
foreign key(book_id) references book1(id)
on delete cascade
on update cascade,
foreign key(author_id) references author(id)
on delete cascade
on update cascade
);
#========插入记录
insert into book1(name,price) values(九阳神功,9.9),
                                    (葵花宝典,9.5),
                                    (辟邪剑谱,5),
                                    (降龙十巴掌,7.3);
insert into author(name) values(egon),(e1),(e2),(e3),(e4);
insert into author2book(book_id,author_id) values(1,1),
                                                 (1,4),
                                                 (2,1),
                                                 (2,5),
                                                 (3,2),
                                                 (3,3),
                                                 (3,4),
                                                 (4,5);

 

数据库之 表与表之间的关系

标签:分享   ssid   custom   primary   管理员   ssi   arc   多对一   用户表   

原文地址:http://www.cnblogs.com/haiyan123/p/7489743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!