事务、视图、索引、备份和恢复
CREATE TABLE `bank`
(
`customerName` CHAR(10), #用户名
`currentMoney` DECIMAL(10,2) #当前余额
);
/*插入数据*/
INSERT INTO `bank`(`customerName`,`currentMoney`)
VALUES('张三',1000);
INSERT INTO `bank`(`customerName`,`currentMoney`) VALUES('李四',1);
(
`customerName` CHAR(10), #用户名
`currentMoney` DECIMAL(10,2) #当前余额
);
/*插入数据*/
INSERT INTO `bank`(`customerName`,`currentMoney`)
VALUES('张三',1000);
INSERT INTO `bank`(`customerName`,`currentMoney`) VALUES('李四',1);
BEGIN;
#开始事物
UPDATE `bank` SET currentMoney=currentMoney-500 WHERE customerName='张三';
UPDATE `bank` SET currentMoney=currentMoney+500 WHERE customerName='李四';
UPDATE `bank` SET currentMoney=currentMoney+500 WHERE customerName='李四';
ROLLBACK;
#回滚(撤销)事物
COMMIT; #提交事物
#注意: 开始事物 后 ,但没有提交,中间可以用(ROLLBACK)撤回,如果提交之后再用撤回是无效的。
#注意: 开始事物 后 ,但没有提交,中间可以用(ROLLBACK)撤回,如果提交之后再用撤回是无效的。
/
*默认情况下,每条单独的SQL语句视为一个事务
关闭默认提交状态后,可手动开启、关闭事务
值为0:关闭自动提交
值为1:开启自动提交
注意:关闭自动提交后,从下一条SQL语句开始则开启新事务,需使用COMMIT或ROLLBACK语句结束该事务
*/
关闭默认提交状态后,可手动开启、关闭事务
值为0:关闭自动提交
值为1:开启自动提交
注意:关闭自动提交后,从下一条SQL语句开始则开启新事务,需使用COMMIT或ROLLBACK语句结束该事务
*/
#关闭自动提交
SET autocommit=0;
UPDATE `bank` SET currentMoney=currentMoney-500 WHERE customerName='张三';
SET autocommit=0;
UPDATE `bank` SET currentMoney=currentMoney-500 WHERE customerName='张三';
ROLLBACK;
SET autocommit=1;
#另一种改变数据的办法
UPDATE `bank` SET `currentMoney`=`currentMoney`-500
WHERE `customerName`='张三';
UPDATE `bank` SET `currentMoney`=`currentMoney`+500
WHERE `customerName`='李四';
WHERE `customerName`='张三';
UPDATE `bank` SET `currentMoney`=`currentMoney`+500
WHERE `customerName`='李四';
/**将毕业学生的基本信息和考试成绩分别保存到历史表中
使用事务完成以下操作
查询result表中所有Y2学员的考试成绩,保存到表historyResult中
删除result表中所有Y2学员的考试成绩
查询student表中所有Y2学员的记录,保存到表historyStudent中
删除student表中所有Y2学员的记录
提交事务,查看各表中数据的变化
回滚事务,查看各表中数据的变化
*/
BEGIN;`historyresult`
查询result表中所有Y2学员的考试成绩,保存到表historyResult中
删除result表中所有Y2学员的考试成绩
查询student表中所有Y2学员的记录,保存到表historyStudent中
删除student表中所有Y2学员的记录
提交事务,查看各表中数据的变化
回滚事务,查看各表中数据的变化
*/
BEGIN;`historyresult`
CREATE TABLE historyResult(
SELECT r.* FROM `result` AS r,grade AS g ,student AS s
WHERE s.`studentNo`=r.`studentNo` AND g.`gradeID`=s.`gradeId` AND g.`gradeName`='Y2'
);
DELETE FROM result WHERE studentNo IN (
SELECT studentNo FROM student WHERE gradeid=( SELECT gradeid FROM grade WHERE `gradeName`='Y2' )
);
CREATE TABLE historyStudent(
SELECT * FROM student WHERE gradeid=( SELECT gradeid FROM grade WHERE `gradeName`='Y2' )
);
DELETE FROM student WHERE gradeid=( SELECT gradeid FROM grade WHERE `gradeName`='Y2' ) ;
SELECT r.* FROM `result` AS r,grade AS g ,student AS s
WHERE s.`studentNo`=r.`studentNo` AND g.`gradeID`=s.`gradeId` AND g.`gradeName`='Y2'
);
DELETE FROM result WHERE studentNo IN (
SELECT studentNo FROM student WHERE gradeid=( SELECT gradeid FROM grade WHERE `gradeName`='Y2' )
);
CREATE TABLE historyStudent(
SELECT * FROM student WHERE gradeid=( SELECT gradeid FROM grade WHERE `gradeName`='Y2' )
);
DELETE FROM student WHERE gradeid=( SELECT gradeid FROM grade WHERE `gradeName`='Y2' ) ;
COMMIT;
ROLLBACK;
UPDATE student SET gradeid=3 WHERE studentNo IN (
SELECT studentNo FROM result WHERE examDate<'2018-01-01'
)
SELECT studentNo FROM result WHERE examDate<'2018-01-01'
)
完毕!