外键 修改表 复制表

外键(Foreign Key)

把所有数据都存放于一张表的弊端
  1)组织结构不清晰
  2)浪费硬盘空间
  3)扩展性极差(修改某一个部门的信息的时候~)

 

foreign key会带来什么样的效果?

  1、在创建表时,先建被关联的表dep,才能建关联表emp

  2、在插入记录时,必须先插被关联的表dep,才能插关联表emp

  3、更新与删除都需要考虑到关联与被关联的关系:同步更新与同步删除(级联删除级联更新

    on update cascade
    on delete cascade

 

一对多

如何查找表与表之间的关系:

  需要做到换位思考(站在两边去找表关系),都分析过了才能下结论

以员工和部门表为例:
先站在员工表:
  找员工表的多条数据能否对应部门表的一条数据
  翻译:
    多个员工能否属于一个部门
    可以!之后不能直接下结论,还需要站在部门表的角度再确认关系
再站在部门表:
  找部门表的多条数据能否对应员工表的一条数据
  翻译:
    多个部门能否有同一个员工
    不可以!
只有站在两边表的角度都分析过了,才能够下结论
员工表单向多对一部门表
# 1、在创建表时,先建被关联的表dep,才能建关联表emp
create table dep(
    id int primary key auto_increment,
    dep_name char(10),
    dep_comment char(60)
);

create table emp(
    id int primary key auto_increment,
    name char(16),
    gender enum('male','female') not null default 'male',
    dep_id int,
    foreign key(dep_id) references dep(id)
);

# 2、在插入记录时,必须先插被关联的表dep,才能插关联表emp
insert into dep(dep_name,dep_comment) values
('sb教学部','sb辅导学生学习,python课程'),
('外交部','xx校区驻张江形象大使'),
('nb技术部','nb技术能力有限部门');

insert into emp(name,gender,dep_id)  values
('tom','male',1),
('bob','male',2),
('lxx','male',1),
('wxx','male',1),
('lili','female',3);

# 3.更新与删除都需要考虑到关联与被关联的关系>>>同步更新与同步删除
create table dep(
    id int primary key auto_increment,
    dep_name char(10),
    dep_comment char(60)
);

create table emp(
    id int primary key auto_increment,
    name char(16),
    gender enum('male','female') not null default 'male',
    dep_id int,
    foreign key(dep_id) references dep(id)
    on update cascade
    on delete cascade
);
insert into dep(dep_name,dep_comment) values
('教学部','辅导学生学习,教授python课程'),
('外交部','形象大使'),
('技术部','技术能力有限部门');

insert into emp(name,gender,dep_id)  values
('ax','male',1),
('en','male',2),
('lx','male',1),
('wx','male',1),
('wu','female',3);

 

多对多

站在两张表的角度:
  1.站在图书表:一本书可不可以有多个作者,可以!----> 书对作者:一对多
  2.站在作者表:一个作者可不可以写多本书,可以!----> 作者对书:一对多
  双方都能一条数据对应对方多条记录,这种关系就是多对多!

# 先来想如何创建表?图书表需要有一个外键关联作者,作者也需要有一个外键字段关联图书。问题来了,先创建谁都不合适!如何解决?
# 建立第三张表,该表中有一个字段fk左表的id,还有一个字段是fk右表的id
create table author(
    id int primary key auto_increment,
    name char(16)
);

create table book(
    id int primary key auto_increment,
    bname char(16),
    price int
);

insert into author(name) values
('ego'),
('lex'),
('wxx')
;
insert into book(bname,price) values
('西游记',310),
('红楼梦,360),
('水浒传',500),
('三国演义',300)
;

create table author2book(
    id int primary key auto_increment,
    author_id int,
    book_id int,
    foreign key(author_id) references author(id)
    on update cascade
    on delete cascade,
    foreign key(book_id) references book(id)
    on update cascade
    on delete cascade
);

insert into author2book(author_id,book_id) values
(1,3),
(1,4),
(2,2),
(2,4),
(3,1),
(3,2),
(3,3),
(3,4);

 

一对一

 

# 左表的一条记录唯一对应右表的一条记录,反之也一样

create table customer(
    id int primary key auto_increment,
    name char(20) not null,
    qq char(10) not null,l
    phone char(16) not null
);

create table student(
    id int primary key auto_increment,
    class_name char(20) not null,
    customer_id int unique, #该字段一定要是唯一的
    foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
    on delete cascade
    on update cascade
);

三种外键关系都是用foreign key,区别在于如何使用以及其他条件限制即可做出三种关系

 

 

修改表

语法:
  1) 修改表名
    ALTER TABLE 表名
                          RENAME 新表名;
  2) 增加字段
    ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…],
                          ADD 字段名  数据类型 [完整性约束条件…];
    ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…]  FIRST;
    ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;                      
  3) 删除字段
    ALTER TABLE 表名
                          DROP 字段名;
  4) 修改字段
    ALTER TABLE 表名
                          MODIFY  字段名 数据类型 [完整性约束条件…];
    ALTER TABLE 表名
                          CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
    ALTER TABLE 表名
                          CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];

复制表

 在mysql中不识别大小写的

# 复制表结构+记录 (key不会复制: 主键、外键和索引)
create table new_service select * from service;

# 只复制表结构
select * from service where 1=2;        //条件为假,查不到任何记录

create table new1_service select * from service where 1=2;  

create table t4 like employees;

 

posted @ 2019-05-14 19:12  zhoyong  阅读(127)  评论(0编辑  收藏  举报