mysql随手笔记1


```sql
create database SpringBoot;
use SpringBoot;



create table student(
    id int auto_increment primary key comment '主键',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'

)comment '学生表';

insert into student(name, no) VALUES ('解雨臣',1202),('张启山',1203)

select *
from student;

create table course(
    id int auto_increment primary key comment  '主键id',
    name varchar(10) comment '课程名称'
)comment '课程表';
insert into course(name) values ('java'),('前端'),('后端')

select *
from course;

select *from student

# 建立中间表来维护两个表之间的关系
# 维护的规则 就是 当两个实体是多对多之间的关系的时候 把两个实体之间的 主键 两个加过来 作为新表的两个外键

;
create table studentAndCourse(
#     第一个 关联表的 主键
id int auto_increment primary key comment '课程和学生的维护表的主键' ,
# 第一个 学生表中的主键作为 这个的外键
studentId int  not null comment '学生表的主键',
courseId int  not null comment '课程表的主键',
#                              把外键添加过来
                             constraint fk_courseId foreign key (courseId) references course(id),
                             constraint fk_studentId foreign key (studentId) references student(id)
);
# 创建完成中间表
insert into studentAndCourse(id, studentId, courseId) VALUES (null,1,2);#读作向关联表中 插入 第一个学生 选择了 id为2的课程的数据信息




select *from studentAndCourse;









# 一对一关系:
#  随意把一方实体的主键 加入另一方的作为另一方的主键即可
# 比如: AB 表是 一对一关系  把b表中的主键加入到A表中作为 A表的外键  并且这个外键 要设置为unique 唯一的

# 基本信息表
create table user(
    id int auto_increment primary key comment '主键',
    name varchar(10) comment '姓名',
    age int comment '年龄',
    gender char(1) comment '1 :男 ,2:女',
    phone char(11) comment '手机号'
)comment '用户信息表';


# 教育详情表
create table userEdu(
    id int auto_increment primary key  comment '主键',
    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 userEdu(id)
#
)comment '用户信息表';

# 吴邪这个学生
insert into user(name, age, gender, phone) VALUES ('吴邪',19,'男','15243244556');
insert into userEdu (degree, major, primaryschool, middleschool, university, userid)
values ('本科','计算机','吴山居小学','吴山居高中','浙江大学',1);



select name,age,gender,phone,degree,major,primaryschool,middleschool,university from userEdu,user;



















#   一对多: 把一方实体的主键加入到 多方实体中  这个主键作为这个表中的外键


# 创建不,部门表
create  table dept(
    id int auto_increment primary key comment '部门id  主键',
    name varchar(10) comment '部门名称'
)comment '部门表';

create table emp(
    id int auto_increment primary key  comment ' 员工id 主键',
    name varchar(10) comment '姓名',
    age int comment '年龄',
#     添加外键
    deptId int unique  comment '部门id 非空',
    constraint fk_deptId foreign key (deptId) references dept(id)#这个语句 读作 把 自定义了一个名字 fk_deptId 把dept表中的主键与 emp表中的主键映射起来
#     foreign key dept(id) references emp(deptId) 中 前面的表A(主键)是要添加进来的主键 也就是本表中的外键 属于一的那一方 后面  reference emp(deptId)
# 表示 emp表中的那个字段和 一方表中的主键完成映射 也就是外键
)comment '员工表';
# 员工和部门的关系: 员工员工属于一个部门  一个部门有多个员工  因此把部门表中的主键加入到员工表中

drop table dept,emp;




# 向 员工表中插入对应的员工信息 和员工对应的部门信息’  吴邪 19岁 部门为 测试部
insert into emp(id,name, age, deptId) VALUES (1,'吴邪',19,3);

# 向员工表中插入 解雨臣的个人信息
insert into emp(id, name, age, deptId) VALUES (2,'解雨臣',20,2);

# 创建表时 先创建一方实体所在的表
# 进行数据的插入操作时候也是先插入一方实体所在的表
insert into dept(id, name) VALUES (1,'测试部'),(2,'研发部'),(3,'后勤部')
select *
from dept;
# 查询 员工的信息 即部门
select e.name,e.age,e.deptId,d.id,d.name from emp e ,dept d where d.id=e.deptId;





# 内连接:
#           查询的是 两个表的交集部分
select  e.name,d.id,d.name,e.id,e.age,e.deptId from dept d ,emp e where e.deptId=d.id;



# 外连接:
#   左外连接: 查询出来的数据为 左表的所有数据加上 两个表的交集
#   例如:查询emp表的所有信息和dept表的部门信息(左外连接)
select e.*,d.name from emp e left outer join dept d on e.deptId = d.id;
#    e.*代表的就是emp表中的所有数据
#   d.name 为dept表的部门信息


#  右外连接:查询出来的数据为 右表的所有数据加上两个表的交集
#       例如:查询dept表的所有数据 和对应的员工信息
select  d.*,e.*  from dept d right join emp e on d.id = e.deptId;





/*
 自连接:操作一张表的时候  比如员工表中 有的字段是员工和员工的领导也是在员工表中 但是我们要查询的员工信息和员工的领导 这种操作就需要使用 自连接了

 */


# 查询 StudentInfo表中员工和员工领导的信息
select a.name,a.age,a.manageId,b.manageId ,b.name  from studentinfo a ,StudentInfo b where a.id=b.manageId;


create table StudentInfo(
    id int primary key auto_increment comment  '员工id',
    name varchar(10) comment '姓名',
    age int comment '年龄',
    manageId int comment '员工的领导id',
    DeptName varchar(10) comment '部门名称'
)comment '信息表';


insert into StudentInfo(name, age, manageId, DeptName) values
                                                              ('金庸',90,1,null);

select *from StudentInfo;



insert into StudentInfo(name, age, manageId, DeptName) values

                                                              ('张无忌',19,1,'明教'),
                                                              ('张三丰',86,1,'无党派'),
                                                              ('杨逍',40,2,'明教'),
                                                              ('韦一笑',67,2,'明教')
                                                              ;
select *from StudentInfo;





























drop table  A,B;


# 部门表:
create table B(
#     部门表
        id int primary key comment '部门id 主键',
        deptName varchar(10) comment '部门名称'

)comment '部门表';


# 把部门表的主键 加入到 员工表中做为外键作为两个表中的关联关系

# 员工表
create table A(
    id int primary key comment '员工id 主键',
    name varchar(10) comment '姓名',
    age  int comment '年龄',

    DeptId int not null comment '部门id',

    constraint fk_AID foreign key (DeptId) references B(id)
)comment '员工表';


insert into B(id, deptName)
values (1,'研发部'),(2,'测试部'),(3,'运维部')

insert into A(id, name, age, DeptId)
values (1,'吴邪',19,2);



select a.id as '员工id',a.name as '员工姓名',a.age '员工年龄',a.DeptId '员工部门id' ,b.id '部门id',b.deptName '部门名称' from A a ,B b
where a.DeptId=b.id;






# ---------------------------------------------------------------------------------------------


# 前端项目组
create table Front(

    id int auto_increment primary key comment '前端组id ',
    name varchar(10) comment '前端组名',
    count int comment '前端组人数'
);

create table Person(
    id int auto_increment primary key comment '工作人员id',
    name varchar(10) comment '姓名',
    salary int comment '工作人员工资',
    Address varchar(10) comment '住址',

#     工作人员所在的项目组
    FrontId int not null comment '工作人员所在的小组',

    constraint foreign key (FrontId) references Front(id)
)comment '项目组人员';


select *from Person;

select *from Front
# 添加项目组信息
insert into Front(name, count) VALUES ('A组',30)


                                        ,('B组',30),('C组',30)

insert into Person (name, salary, Address, FrontId)
values ('吴邪',28773,'吴山居',2);
select p.id,p.name,p.salary,p.Address,p.FrontId,f.id,f.name,f.count from Person p,Front f where p.FrontId=f.id;





# 查询出: 员工表所在的部门信息
select p.*,f.count,f.name from Front f,Person p where f.id=p.FrontId;

select p.id,p.name,p.salary,p.Address,f.id,f.name,f.count from Person p ,Front f where p.FrontId=f.id;




  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值