标签:name gem png ges not arc 18C alt ima
代码块
use company;
#创建部门表
CREATE TABLE department (
id int PRIMARY key auto_increment,
dep_name varchar(10) not null
);
#创建员工表
CREATE TABLE employee(
id int PRIMARY key auto_increment,
emp_name varchar(10) not null,
dep_id int
-- CONSTRAINT dep_id_fk FOREIGN key (dep_id) REFERENCES company(id) on DELETE CASCADE on UPDATE CASCADE
#上面的外键约束没有也可以,外键约束不影响表与表之间关联,影响的是表插入数据的约束
);
#向部门表中插入数据
insert into department(dep_name) VALUES ('网盟营销'),('大数据中心')
#向员工表中插入数据
insert into employee(emp_name,dep_id) VALUES('wangsiyu',1),('alex',2)
代码块
select employee.id,emp_name,dep_name from employee,department WHERE department.id=employee.dep_id;
注意:对于两个表都同名的字段,应该在查询该字段的前面加上对应的表,否则电脑不知道你这个字段是哪个表的
select * from employee inner join department on employee.dep_id=department.id;
select * from employee left join department on employee.dep_id=department.id;
select * from employee right join department on employee.dep_id=department.id;
select * from employee full join department on employee.dep_id=department.id;
标签:name gem png ges not arc 18C alt ima
原文地址:https://www.cnblogs.com/hellosiyu/p/12501659.html