1、连接查询

  • 交叉连接:返回所有数据行的笛卡尔积

方法一

  1. select * from department cross join employee;

方法二

  1. select employee,name,department.dname from department,employee where
  2. department.did = employee.did;
  • 内连接:使用比较运算符对两个表数据进行比较,包含符合条件的数据

方法一

  1. select employee,name,department.dname from department join employee on
  2. department.did = employee.did order by age desc;

方法二

  1. select employee,name,department.dname from department,employee where
  2. department.did = employee.did;

自连接(特殊的内连接)

  1. select p1.* from employee as pl join employee as p2 on p1.did = p2.did
  2. where pz.name = '王红';
  • 外连接:分为左连接和右连接,包括左表\右表\两个表中的所有数据

左连接(左表满足条件的连接右表的)

  1. select department.did,department.dname,employee.name from department
  2. left join employee on department.did = employee.did;

右连接(右表满足条件的连接左表的)

  1. select department.did,department.dname,employee.name from department
  2. right join employee on department.did = employee.did;

2、子查询

可使用in,exists,any,all,比较运算符,只返回true\false,返回true外层查询才执行
“带in”:

  1. select * from department where did in(select did from employee where age = 20);
  1. select * from department where did not in(select did from employee where age = 20);

“带exists”:

  1. select * from department where exists(select did from employee where age = 20);

“带any”:

  1. select * from department where did > any(select did from employee);

“带all”:

  1. select * from department where did > all(select did from employee );

“带运算符”:

  1. select * from department where did = (select did from employee );

3、事务及储存过程

1)开启事务 start transaction;
2)SQL语句 …………………………..;
3)提交事务 commit;
4)取消事务 rollback;
事务四个特性:
1)原子性
2)一致性
3)隔离性
4)持久性
问题:
脏读、不可重复读、幻读
解决:
设置隔离级别 1)读未提交 2)读提交”防止脏读” 3)可充分读 4)可串行化”防止不可重复读\幻读”

演示脏读:一事务读取另一事务未提交数据
b账户

  1. start transaction;
  2. select * from account;

a账户

  1. start transaction;
  2. update account set money=money-100 where name='a';
  3. update account set money=money+100 where name='b';

b账户

  1. select * from account;
  2. commit;

a账户

  1. rollback;

演示不可重复性:在一个事务内重复读取别的线程已提交的数据
b账户

  1. start transaction;
  2. select * from account;

a账户

  1. update account set money=money-100 where name='a';
  2. select * from account;

b账户

  1. select * from account;
  2. commit;

演示幻读:一个事物内两次查询的数据条数不一致(其他事务插入,记录数增加)
a账户

  1. set session transaction isolation level read committed;

b账户

  1. start transaction;
  2. select * from account;

a账户

  1. select * from account;
  2. insert into account(name,money) values('c',1000);

b账户

  1. select * from account;
  2. commit;

4、创建存储过程Proc()

  1. delimiter //
  2. creat procedure proc()
  3. begin
  4. select * from student;
  5. end //
  6. delimiter;

调用存储过程

  1. call proc;

删过程

  1. drop proc;

5、光标的使用
1)声明(光标名cursor_student,表名student)

  1. declare cursor_student cursor for select * from student;

2)打开光标

  1. open cursor_student;

3)操作光标

  1. fetch cursor_student into s_name,s_gender;
  2. select s_name,s_gender;

4)关闭光标

  1. close cursor_student;

5、视图

优点:简化查询语句;安全性;逻辑数据独立性
1)创建视图

  1. create view stu_view as select math,chinese,math+chinese from student;

2)在多个基本表上创建视图

  1. create view stu_class(id,name,class) as select student.sid,student.name,stuinfo.calss
  2. from studnet,stuinfo where student.sid = stuinfo.sid;

3)查看视图

  1. desc stu_view;

  1. show table status like stu_view\G;

  1. show create view stu_view\G;

4)修改视图

  1. create or replace view stu_view as select * from student;

  1. alter view my_view as select chinese from student;

5)更新视图

  1. update stu_view set chinese = 100;

  1. insert into student values(4,'Lily',100,100);

  1. delete from stu_view where math = 70;

6)删除视图

  1. drop view if exists my_view;

6、数据库备份

备份单个数据库:

  1. mysqldump -u root -p --database chapter08>C:/backup/chapter08.sql;

备份多个数据库:

  1. mysqldump -u root -p --databases chapter08 chapter09>C:/backup/file.sql;

备份所有数据库:

  1. mysqldump -u root -p --all-databases>C:/backup/pp.sql;

7、数据的还原

  1. drop database chapter08;
  2. show databases;
  3. create database chapter08;
  4. mysql -u root -p chapter08>C:/backup/chapter08.sql;
  5. select * from student;

8、创建用户

分为超级用户(root用户)和普通用户
eg:用grant创建用户,用户名user1,主机名localhost,密码123,授予用户对chapter08.student表的查询权限

  1. grant select on chapter08.student to 'user1'@'localhost' identified by '123';

验证是否创建成功

  1. use mysql;
  2. select host,user,password from user;

9、删除用户

  1. drop user 'user1'@'localhost';

10、修改用户密码

修改root用户密码

  1. set password = password('新密码');

roo用户修改普通用户密码

  1. set password for 'username'@'hostname'=password('新密码');

普通用户修改密码

  1. set password =password('新密码');

11、对新用户授予权限

eg:创建用户user4,密码123,对数据库有insert,select权限,使用with grant option

  1. grant insert ,select on *.* to 'user4'@'localhost' identified by '123'
  2. with grant option;

12、收回权限

  1. revoke insert on *.* from 'user4'@'localhost';

13、收回所有权限

  1. revoke all privileges, grant option from 'user4'@'localhost';