【跟上黑马学sql】学习笔记-MySQL-多表关系

多表关系(一对一、1对多(多对一)、多对多)

目录

一对多

多对多

一对一


  • 一对多

##创建一个部门表
CREATE TABLE dept (
    id INT PRIMARY KEY NOT NULL auto_increment comment '主键',
    NAME VARCHAR ( 10 ) comment '部门名称'
) COMMENT '部门表';
##向部门表中插入数据
INSERT INTO dept ( NAME )
VALUES
	( '研发部' ),
	( '市场部' ),
	( '财务部' ),
	( '销售部' ),
	( '总经办' );
drop table dept;
##创建一个员工表
CREATE TABLE emp (
	id INT PRIMARY KEY NOT NULL auto_increment,
	NAME VARCHAR ( 10 ),
	age INT CHECK (- 1 < age < 120 ),
	job VARCHAR ( 10 ),
	salary INT,
	entry_time date COMMENT '入职时间',
	managerid INT COMMENT '上级领导id',
	department_id INT COMMENT '部门id',#外键
	constraint fk_emp_dept_id foreign key (department_id) references dept(id)
) COMMENT '员工表';
INSERT INTO emp ( NAME, age, job, salary, entry_time, managerid, department_id )
VALUES
('钢蛋', 55, '总裁', 20000, '2000-01-01', null, '5' ),
('威哥',35,'项目经理',12500,'2005-12-05',1,1),
('一掏',30,'开发',18000,'2022-11-03',2,1),
('张子语',20,'开发',11500,'2012-02-05',2,1),
('陆先生',30,'开发',10500,'2022-09-07',3,1),
('靳璐',30,'程序员鼓励师',9600,'2022-09-07',2,1);
  • 多对多

##创建一个学生表并插入数据
create table student(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    Sno varchar(10) comment '学号'
) comment '学生表';
insert into student
values (null, '靳璐', '2000100101'),(null, '陆先生', '2000100102'),
       (null, '肖子琪', '2000100103'),(null, '张大为', '2000100104');
##创建一个课程表并插入数据
create table course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
) comment '课程表';
insert into course
values (null, 'Java'), (null, 'web'), (null , 'MySQL') , (null, '框架');

##创建中间表来维护student和course两表间关系
create table student_course(
    id int auto_increment comment '主键' primary key,
    studentid int not null comment '学生ID',
    courseid  int not null comment '课程ID',
    constraint fk_courseid foreign key (courseid) references course (id),
    constraint fk_studentid foreign key (studentid) references student (id)
)comment '学生课程中间表';
insert into student_course values (null,1,1),(null,1,2),(null,1,3),(null,2,2),(null,2,3),(null,3,4);
  • 一对一

## 创建用户基本信息表
create table tb_user
(
    id     int auto_increment primary key comment '主键ID',
    name   varchar(10) comment '姓名',
    age    int comment '年龄',
    gender char(1) comment '1: 男 , 2: 女',
    phone  char(11) comment '手机号'
) comment '用户基本信息表';
## 往用户基本信息表插入初始数据
insert into tb_user(id, name, age, gender, phone)
values (null, '黄渤', 45, '1', '18800001111'),
       (null, '冰冰', 35, '2', '18800002222'),
       (null, '码云', 55, '1', '18800008888'),
       (null, '李彦宏', 50, '1', '18800009999');

##创建用户教育信息表
create table tb_user_edu
(
    id            int auto_increment primary key comment '主键ID',
    degree        varchar(20) comment '学历',
    major         varchar(50) comment '专业',
    primaryschool varchar(50) comment '小学',
    middleschool  varchar(50) comment '中学',
    university    varchar(50) comment '大学',
    userid        int unique comment '用户ID',
    constraint fk_userid foreign key (userid) references tb_user (id)
) comment '用户教育信息表';
##往用户教育信息表插入初始数据
insert into tb_user_edu(id, degree, major, primaryschool, middleschool, university, userid)
values (null, '本科', '舞蹈', '静安区第一小学', '静安区第一中学', '北京舞蹈学院', 1),
       (null, '硕士', '表演', '朝阳区第一小学', '朝阳区第一中学', '北京电影学院', 2),
       (null, '本科', '英语', '杭州市第一小学', '杭州市第一中学', '杭州师范大学', 3),
       (null, '本科', '应用数学', '阳泉第一小学', '阳泉区第一中学', '清华大学', 4);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值