-- 创建数据库chapter
create database if not exists chapter character set utf8;
-- 使用数据库
use chapter;
-- 创建两张表:部门表dept,员工表emp
-- 创建之前先删除
drop table if exists dept,emp;
部门表(主表)
create table dept(
dept_id int(10) primary key not null,
dept_name varchar(30) not null
)default charset=utf8;
员工表(从表)
create table emp(
emp_id int(10) primary key not null,
emp_name varchar(30) not null,
dept_id int(10) not null,
constraint fk_emp_dept_id foreign key(dept_id) references dept(dept_id)
)default charset=utf8;
为两张表添加数据
insert into dept values(1,'财务部'),
(2,'销售部');
insert into emp values(101,'财务小王',1),
(102,'会计小张',1),
(201,'销售小李',2);
分别查询每张表