博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库学习
阅读量:4321 次
发布时间:2019-06-06

本文共 3196 字,大约阅读时间需要 10 分钟。

  今天学习了多表关联、修改表、复制表和蠕虫复制

  一、多表关联:

    如何找出两张表之间的关系

    1、先站在左表的角度去找

    是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)

    2、再站在右表的角度去找

    是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段 foreign key 左表一个字段(通常是id)

    3、总结:

    多对一:

    如果只有步骤1成立,则是左表多对一右表

    如果只有步骤2成立,则是右表多对一左表

    多对多:

    如果步骤1和2同时成立,则证明这两张表是一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系

    一对一:

    如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可

    建立表之间的关系

    #一对多或称为多对一

    三张表:出版社,作者信息,书

    一对多(或多对一):一个出版社可以出版多本书

    关联方式:foreign key

    

create table press(id int primary key auto_increment,name varchar(20));create table book(id int primary key auto_increment,name varchar(20),press_id int not null,foreign key(press_id) references press(id)on delete cascadeon update cascade);insert into press(name)  values('北京工业地雷出版社'),('人民音乐不好听出版社'),('知识产权没有用出版社');insert into book(name,press_id) values('九阳神功',1),('九阴真经',2),('九阴白骨爪',2),('独孤九剑',3),('降龙十巴掌'),('葵花宝典',3);

  多对多

  三张表:出版社,作者信息,书

  多对多:一个作者可以写多本书,一本书也可以用多个作者,双向的一对多,即多对多

  关联方式:foreign key +一张新的表

create table auther(id int primary key auto_increment,name varchar(20));create table book(id int primary key auto_increment,name varchar(20));#这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了create table author2book(id int not null unique auto_increment,author_id int not null,book_id int not null,constraint fk_author foreign key(author_id) references author(id) on delete cascadeon update cascade,constraint fk_book foreign key(book_id) references book(id)on delete cascadeon update cascade,primary key(author_id,book_id));#插入四个作者,id依次排开insert into author(name) values('egon'),('alex'),('yuanhao'),('wpq');#插入六本书,id依次排开insert into book(name) values('九阳神功'),('九阴真经'),('九阴白骨爪'),('独孤九剑'),('降龙十巴掌'),('葵花宝典');insert into author2book(author_id,book_id) values(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(2,1),(2,6),(3,4),(3,5),(3,6),(4,1);

  一对一:

  两张表:学生表和客户表

  一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系

  关联方式:foreign key +unique

#一定是student来foreign key表customer,这样就保证了:#1 学生一定是一个客户,#2 客户不一定是学生,但有可能成为一个学生create table customer(id int primary key auto_increment,name varchar(20) not null,qq varchar(10) not null,phone char(16) not null);create table student(id int primary key auto_increment,class_name varchar(20) not null,customer_id int unique, #该字段一定要是唯一的foreign key(customer_id) references customer(id)#外键的字段一定要保证uniqueon delete cascadeon update cascade);#增加客户insert into customer(name,qq,phone) values('李飞机','1224542',1234567891),('王大炮','1224542',1234567891),('守榴弹','1224542',1234567891),('吴坦克','1224542',1234567891),('战地雷','1224542',1234567891);#增加学生insert into student(class_name,customer_id) values('脱产3期',3),('周末19期',4),('周末19期',5);

  二、修改表、复制表和蠕虫复制:

    1.修改表

      add 添加字段

      modify 修改字段类型

      change 修改字段名称 或 类型

      drop 删除字段

      rename 改表名

 

    2.复制表

      create table 新的表名 select *from 源表名;

        数据 结构  约束不能复制

      当条件不成立时 只复制表结构

      create table 新的表名 select * from 源表名 where 1 = 2;

      create table stu_copy2 select * from student1 where 1 = 2;

    3.蠕虫复制

     即自我复制

     insert into 表名称 select *from 表名;

     如果有主键 避开主键字段

     insert into 表名称(其他字段) select 其他字段 from 表名;

     

转载于:https://www.cnblogs.com/xiaocaiyang/p/9997405.html

你可能感兴趣的文章
第2天线性表链式存储
查看>>
python自动化测试-D11-学习笔记之一(yaml文件,ddt)
查看>>
mysql存储过程使用游标循环插入数据
查看>>
Ubuntu 12.04 添加新用户并启用root登录
查看>>
shell中$0,$?,$!等的特殊用法
查看>>
20145309信息安全系统设计基础第9周学习总结上
查看>>
c# 字段、属性get set
查看>>
td内容超出隐藏
查看>>
Spring CommonsMultipartResolver 上传文件
查看>>
Settings app简单学习记录
查看>>
SQLAlchemy
查看>>
多线程
查看>>
使用缓存的9大误区(下)转载
查看>>
appium键值对的应用
查看>>
MyEclipse 8.X 通用算法
查看>>
selenium.Phantomjs设置浏览器请求头
查看>>
分布式数据库如何选择,几种分布式数据库优缺点一览
查看>>
BZOJ 4443: 小凸玩矩阵【二分图】
查看>>
苹果 OS X制作u盘启动盘
查看>>
Jquery便利对象
查看>>