一.SQL特性——事务管理
COMMIT 提交
将SQL语句的执行结果写入数据库表中
ROLLBACK 回退
撤销SQL语句的执行结果
SAVEPOINT 保留点
时间点的备份,可以对其发布回退
MySQL 代码
D:\mysql\mysql80\mysql -uroot -p
Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> USE sqllearning
Database changed
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
+--------+---------+---------+------+------------+------------+
6 rows in set (0.01 sec)
mysql> INSERT INTO pet2 VALUES('test1', 'test', 'test', 'f', '20130901', NULL);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
7 rows in set (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
7 rows in set (0.00 sec)
mysql> SET AUTOCOMMIT = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO pet2 VALUES('test2', 'test', 'test', 'f', '20130901', NULL);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test2 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
8 rows in set (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
7 rows in set (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
7 rows in set (0.00 sec)
mysql> SET AUTOCOMMIT = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
7 rows in set (0.00 sec)
mysql> INSERT INTO pet2 VALUES('test3', 'test', 'test', 'f', '20130901',NULL);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test3 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
8 rows in set (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test3 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
8 rows in set (0.00 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test3 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
8 rows in set (0.00 sec)
mysql> DELETE FROM pet2 WHERE name = 'test3';
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
7 rows in set (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test3 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
8 rows in set (0.00 sec)
mysql> SAVEPOINT sp1;
Query OK, 0 rows affected (0.00 sec)
mysql> DELETE FROM pet2 WHERE name = 'test3';
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
7 rows in set (0.00 sec)
mysql> SAVEPOINT sp2;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO pet2 VALUES('test3','test','test','m','20180101',NULL);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test3 | test | test | m | 2018-01-01 | NULL |
+--------+---------+---------+------+------------+------------+
8 rows in set (0.00 sec)
mysql> ROLLBACK TO sp1;
ERROR 1305 (42000): SAVEPOINT sp1 does not exist
mysql> INSERT INTO pet2 VALUES('test2', 'test', 'test', 'f', '20130901', NULL);
Query OK, 1 row affected (0.00 sec)
mysql> ROLLBACK TO sp1;
ERROR 1305 (42000): SAVEPOINT sp1 does not exist
mysql> ROLLBACK TO sp2;
ERROR 1305 (42000): SAVEPOINT sp2 does not exist
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test3 | test | test | m | 2018-01-01 | NULL |
| test2 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
9 rows in set (0.00 sec)
mysql> DELETE FROM pet2 WHERE name = 'test3';
Query OK, 1 row affected (0.00 sec)
mysql> SET AUTOCOMMIT = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SAVEPOINT sp1;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO pet2 VALUES('test3','test','test','m','20190101',NULL);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test2 | test | test | f | 2013-09-01 | NULL |
| test3 | test | test | m | 2019-01-01 | NULL |
+--------+---------+---------+------+------------+------------+
9 rows in set (0.00 sec)
mysql> ROLLBACK TO sp1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM pet2;
+--------+---------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+---------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Har_old | dog | f | 1989-05-13 | 0000-00-00 |
| Chirpy | Gw%en | bird | f | 1998-09-11 | NULL |
| Claws | Gw%%en | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Fluffy | Har_old | cat | f | 1993-02-04 | NULL |
| test1 | test | test | f | 2013-09-01 | NULL |
| test2 | test | test | f | 2013-09-01 | NULL |
+--------+---------+---------+------+------------+------------+
8 rows in set (0.00 sec)
mysql> RELEASE SAVEPOINT sp1;
Query OK, 0 rows affected (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
二.SQL高级特性 —— 对字段的约束
三.SQL高级特性 —— 索引
四.数据库安全 —— DBA
五.课后作业
Q1:什么是事务?SQL 中为什么要使用事务管理?
答:成批执行的SQL语句叫作事务。
使用事务管理系统,可以确保成批执行的语句完全执行或完全不执行,并且可以通过点,控制语句执行进度。
使用事务管理,可以确保数据库信息的完整性。
Q2:请总结 SAVAPOINT 的用法。
答:
创建保留点:SAVEPOINT<保留点名>;
回退到保留点:ROLLBACK TO <保留点名>;
释放保留点:RELEASE SAVEPOINT<保留点名>;
Q3:请写出以下语句(Navicat 中操作)
1、从 milk_tea 表复制出 milk_tea2 表。
2、对 milk_tea 表的 in_price 列增加 UNIQUE 属性
3、开启一个事务管理
4、插入值 prod_id – ‘T1’, prod_name – ‘测试商品 1’,in_price – 88
5、插入值 prod_id – ‘T2’, prod_name – ‘测试商品 2’,in_price – 88
6、回退
7、重复 5 的操作
CREATE TABLE milk_tea2
AS
SELECT * FROM milk_tea;
SELECT * FROM milk_tea2;
ALTER TABLE milk_tea ADD CONSTRAINT UNIQUE(in_price);
BEGIN;
INSERT INTO milk_tea(prod_id, prod_name, in_price) VALUES('T1', '测试商品', 88);
SELECT * FROM milk_tea;
INSERT INTO milk_tea(prod_id, prod_name, in_price) VALUES('T2', '测试商品2', 88);
ROLLBACK;
SELECT * FROM milk_tea;