目录
- in表示嵌套
- 比较符表示嵌套
- exists表示嵌套
- 简单统计
- 分组统计和汇总
- 排序
- 组合查询
- 存储过程定义和调用
- 函数的定义和使用
- 触发器的使用
- 事件的设计
- 游标的使用
- 用户的创建和使用
- 限权的授予于撤销
- 数据库的备份于恢复
- 表数据的导入与导出
in表示嵌套
select rno,rname,dept from reader where rno in (select rno from borrow)
select rno,rname,dept from reader where rno not in (SELECT rno from borrow where year(borrowtime)=2016)
select bno,bname,press from book where bno in (select bno from borrow where backtime is null)
select rno,rname,dept from reader where rno in (select rno from borrow where bno=(select bno from book where bname='红楼梦'))
select rno from borrow where bno='b11001' and rno in(select rno from borrow where bno='b11003')
select * from reader where rno in (SELECT rno from book natural join borrow where reader.rno=borrow.rno and (book.bname='数据库原理' or book.bname='图形图像处理'))
select reader.* from reader natural join borrow natural join book where book.bname='数据库原理' or book.bname='图形图像处理';
select * from reader where rno in(select rno from borrow where bno in(select bno from book where bname in('数据库原理','图形图像处理')));
select * from reader where rno in (select rno from borrow where latefee>0)
select reader.* from reader natural join borrow where latefee > 0
比较符表示嵌套
select * from reader where rno =(select rno from borrow natural join book where book.bname='红楼梦')
select zno,zname,age from employee where
zno>(select zno from employee where zname='张旗')
and age<(SELECT age from employee where zname='张旗')
select * from employee where age>all(select age from employee where sex='女') and sex = '男'
select * from employee where age <any(select age from employee where sex='女') and sex = '男'
exists表示嵌套
select rno,rname,dept from reader where not exists(select book.bno from book where press='机械工业出版社' and not exists(select * from borrow where book.bno=borrow.bno and reader.rno=borrow.rno))
简单统计
select count(*) as '总人数',avg(age) as '平均年龄'from employee where sex='女'
select count(*) as '借阅次数' from book natural join borrow where bname='红楼梦'
select zname,age from employee where age>(select avg(age) from employee where sex='女') and sex='男'
select DISTINCT rno,rname,age,A.dept from reader as A inner join (select dept,max(age) as MA from reader group by dept) as B on A.age=B.MA and A.dept=B.dept
select rno,rname,age,dept from reader as A where age=(select max(age) from reader as B where A.dept=B.dept);
分组统计和汇总
select dept,count(*) from reader group by dept having count(*) > 10
select year(backtime),sum(damagefee) from borrow group by year(backtime)
select reader.rno,rname,A.借阅次数 from(select rno,count(*) as '借阅次数' from borrow group by rno having count(*) >= 2) as A natural join reader;
select rno,rname,count(*) as '借阅次数' from borrow natural join reader group by rno having count(*)>=2;
select reader.rno,rname,A.借阅次数 from reader left join (select rno,count(*) as '借阅次数' from borrow group by rno ) as A on reader.rno=A.rno;
select reader.rno,rname,count(borrow.rno) from reader left join borrow on borrow.rno=reader.rno group by rno;
select rno,count(*) as 借阅次数 from borrow group by rno having count(*) >= all(select count(*) from borrow group by rno)
select dept,count(*) from reader group by dept with rollup
排序
select press as '出版社',avg(price) as '平均价' from book group by press order by avg(price) desc ,bname
select book.bno,bname,count(*) from book natural join borrow group by bname order by count(*) desc
组合查询
select * from book where press='机械工业出版社'
union
select * from book where press='电子工业出版社'
select * from employee limit 2,6
select rno,sum(latefee+damagefee) as '所交费用'from borrow group by rno order by 所交费用 desc limit 3
存储过程定义和调用
delimiter $$
create procedure seek_reader (in dept_ varchar(20) ,in rname_ varchar(20) )
begin
select * from reader where dept = dept_ and rname=rname_;
end$$
call seek_reader('文学院','XXX')
delimiter $$
create procedure count_rs(inout count_ varchar(10))
begin
select count(*) into count_ from reader group by dept having dept=count_;
end$$
delimiter ;
drop procedure count_rs
set@count_='信息工程学院';
call count_rs(@count_);
select @count_ as 总人数;
delimiter $$
create procedure update_rdage(in dept_ varchar(10),in rname_ varchar(10),in age_ int)
begin
update reader set age=age_ where rname=rname_ and dept=dept_;
end $$
delimiter ;
call update_rdage('文学院','XXX',22);
select * from reader where rname='XXX'
drop procedure update_rdage;
函数的定义和使用
delimiter $$
create function count_books(zz varchar(10))
returns int
READS SQL DATA
begin
return(select count(*) from book group by author having author=zz);
end $$
select count_books('XXX')
触发器的使用
create table tsglxt_log(id bigint auto_increment primary key,
user_name varchar(30) not null,
tb_name varchar(30) not null,
event_type enum('insert','update','delete') not null,
logtime timestamp default now() not null)
delimiter $$
create trigger del_bf_book before delete on book
for each row
begin
insert into book_bak1 values(old.bno,old.bname,old.press,old.price,old.author);
insert into tsglxt_log(user_name,tb_name,event_type,logtime) values(user(),'book','delete',now());
end$$
delete from book where bno='B81004'
delimiter $$
create trigger update_bf_borrow before update on borrow
for each row
begin
set @price=0;
select price into @price from book where bno=old.bno;
if new.damagefee>3*@price then set new.damagefee=3*@price;
end if;
insert into tsglxt_log (user_name,tb_name,event_type,logtime) values(user(),'borrow','update',now());
end$$
update borrow set damagefee=300 where id=22;
select * from borrow where id=22;
事件的设计
create table event_test(id int auto_increment primary key,
event_name varchar(30) not null,
event_time timestamp not null);
create event event1 on schedule every 10 second
ends CURRENT_TIMESTAMP + INTERVAL 1 minute
do insert into event_test(event_name,event_time) values('event1',now());
select * from event_test
游标的使用
delimiter $$
create function seek_reader_deptname()
returns varchar(30)
READS SQL DATA
begin
DECLARE _STOP int default 0;
declare co varchar(20);
declare res varchar(30);
declare cur cursor for select dept from reader group by dept;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _STOP=1;
open cur;
loop1:loop
fetch cur into co;
if _STOP=1 then
leave loop1;
else
set res = concat_ws('、',res,co);
end if;
end loop loop1;
close cur;
return res;
end$$
select seek_reader_deptname()
用户的创建和使用
create user 'stu1'@'localhost' identified by '123456','stu2'@'localhost' identified by '123456'
rename user 'stu2'@'localhost' to 'tea1'@'localhost'
select * from mysql.user
mysqladmin -ustu1 -p password
set password for 'tea1'@'localhost' =password('123')
限权的授予于撤销
grant select(rno,rname,dept) on reader to 'stu1'@'loaclhost'
select * from mysql.tables_priv
select * from mysql.columns_priv
grant all on tsglxt.* to 'tea1'@'loaclhost';
select * from mysql.db;
grant all on 服务器.* to 'tea2'@'localhost' identified by '123456';
create user 'tea2'@'localhost' identified by '123456'
grant all on 服务器.* to 'tea2'@'localhost';
revoke update on tsglxt.* from 'tea1'@'localhost'
select * from mysql.db;
数据库的备份于恢复
msyqldump -uroot -proot tsglxt borrow >D:\BACKUP\borrow_bak.sql
mysql -uroot -proot tsglxt .sql
mysqldump -uroo -proot --databses tsglxt >D:\BACKUP\tsglxt_bal.sql
mysql -uroot -proot .sql
表数据的导入与导出
select * from borrow into outfile 'D:\BACKUP\borrow_bak.txt'
fields
terminated BY '\,'
enclosed by '\"'
lines
terminated by '\r\n'
set global local_infile=1
load data local infile 'D:\BACKUP\borrow_bak.txt' replace into table borrow
fields
terminated by '\,'
enclosed by '\"'
lines
terminated by '\r\n'