-- 创建表的时候 声明外键
create table dept(
id int primary key auto_increment,
name varchar(40)
);
insert into dept values (null,'财务部'),(null,'人事部'),(null,'行政部'),(null,'科技部'),(null,'销售部');
create table emp(
id int primary key auto_increment,
name varchar(40),
dept_id int,
foreign key(dept_id) references dept(id)
);
insert into emp values (null,'奥巴马',1),(null,'萨达姆',2),(null,'哈利波特',4),(null,'火车王',4),(null,'张三丰',6);
update dept set id=6 where name='财务部';
-- 已有表建立外键
create table dept(
id int primary key auto_increment,
name varchar(40)
);
create table emp(
id int primary key auto_increment,
name varchar(40),
dept_id int
);
ALTER TABLE emp add CONSTRAINT FK_ID FOREIGN KEY(dept_id) REFERENCES dept(id);
--
select * from dept,emp;
--
select * from dept,emp where dept.id=emp.dept_id;
--
select * from dept join emp on dept.id = emp.dept_id;
select * from dept inner join emp on dept.id = emp.dept_id;
--
select * from dept left join emp on dept.id = emp.dept_id;
--
select * from dept right join emp on dept.id = emp.dept_id;
--
-- 但是可以间接 利用合并查询
select * from dept left join emp on dept.id = emp.dept_id
union
select * from dept right join emp on dept.id =emp.dept_id;
--
select dept.name 部门名称,emp.name 员工名称 from dept inner join emp on dept.id = emp.dept_id
where dept.id = 4;
create table department(
did int(4) not null primary key,
dname VARCHAR(36)
);
create table employee(
id int(4) not null primary key,
name varchar(36),
age int(2),
did int(4) not null
);
insert into department values (1,'网络部'),(2,'媒体部'),(3,'研发部'),(4,'人事部');
insert into employee values (1,'王红',20,1),(2,'李强',22,1),(3,'赵四',20,2),(4,'何娟',20,4);
select did from employee where age = 20;
select dname from department where did in (select did from employee where age = 20);
select department.dname from department inner join employee on department.did = employee.did where employee.age = 20;
select dname from department where did not in (select did from employee where age = 20);
select * from department where exists (select * from employee where age > 21);
select * from department where did > any (select did from employee);
--
create table account (
id int not null primary key auto_increment,
name varchar(20),
money double
);
insert into account values (1,'a',1000.0),(2,'b',1000.0);
set [session/global] transaction isolation level ...;
select @@tx_isolation;
-- 存储过程
create table student (
id int(3) primary key auto_increment,
name varchar(20) not null,
grade float,
gender char(2)
);
insert into student (name,grade,gender) values ('tom',60,'男'),('jack',70,'男'),('rose',90,'女'),('lucy',100,'女');
delimiter
create procedure proc()
BEGIN
SELECT * from student;
END
delimiter ;
-- 定义变量
-- 变量的声明要在begin end 之间
declare myvariable int default 100;
set var_name = expr;
delimiter
create procedure proc3()
BEGIN
declare myvariable int default 100;
select myvariable;
-- 第一个种方式赋值
set myvariable = myvariable + 20;
SELECT myvariable;
END
delimiter ;
-- 第二种方式赋值
select column_name into var_name from table_name;
-- 光标 游标
declare cursor_name cursor for select_statement;
open cursor_name;
fetch cursor_name into var_name;
close cursor_name;
-- 流程控制
-- if
delimiter
create procedure proc5()
begin
declare v varchar(2);
set v = 'a';
if v is null then select 'is null';
else select 'not null';
end if;
end
delimiter ;
-- case
delimiter
create procedure proc6()
BEGIN
declare v int;
set v = 2;
case v
when 1 then select 'value is 1';
when 2 then select 'value is 2';
else select 'not 1 or 2';
end case;
END
-- loop
-- leave 跳出循环(类似break)
-- iterate 再次循环(类似continue)
delimiter
create procedure doiterate()
begin
declare p1 int default 0;
my_loop:loop
set p1 =p1+1;
if p1<10 then iterate my_loop;
elseif p1 > 10 then
end
--replace
declare id int default 0;
repeat
set id = id + 1;
until id>=10;
end repeat;
--while
declare i int default 0;
while i < 10 do
set i=i+1;
end while;
-- 查看存储过程
show procedure status;
show procedure status like "%xxx%";
show create procedure proc_name;
-- 修改存储过程的特性