人力资源管理系统——hrs
判断是否存在hrs的库,如果存在的话就进行删除
drop database if exists hrs;
- 删库一定要谨慎再谨慎,应该说不论是删什么东西之前都要先进行灵魂拷问,能不能删除
创建hrs的库
create database hrs default charset utf8mb4;
- MySQL里面本身是不区分大小写的,字符串的比较区不区分大小写这些是看你设置的校对规则
- 假如后面添加校对规则collate utf8_bin则后面的查询就会区分大小写
- 假如后面添加校对规则collate utf8_general_ci 则后面的查询就不会区分大小写
- 在建表的时候还可以重新修改编码,校对规则
切换到hrs库
use hrs;
创建表
drop table if exists tb_emp;
drop table if exists tb_dept;
create table tb_dept
(
dno int not null comment '编号',
dname varchar(10) not null comment '名称',
dloc varchar(20) not null comment '所在地',
primary key (dno)
);
create table tb_emp
(
eno int not null comment '员工编号',
ename varchar(20) not null comment '员工姓名',
job varchar(20) not null comment '员工职位',
mgr int comment '主管编号',
sal int not null comment '员工月薪',
comm int comment '每月补贴',
dno int comment '所在部门编号',
primary key (eno),
foreign key (dno) references tb_dept(dno),
foreign key (mgr) references tb_emp(eno)
);
- 可以通过修改表添加主键
- alter table tb_dept add constraint pk_dept_dno primary key(dno);
- 通过修改表添加外键约束
- 自参照
- alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
- 添加外键约束
- alter table tb_emp add constraint fk_emp_dno fo