事务处理
- 事务指一组SQL语句
- 回退指撤销指定SQL语句过程
- 提交指将未存储的SQL语句结果写入数据库表
- 保留点指事务处理中设置的临时占位符,可以对它发布回退。
控制事务处理
- 使用ROLLBACK
- 使用Commit语句
BEGIN TRANSACTION DELETE OrderItems WHERE order_num = 12345;
delete Orders where order_num = 12345 commit transaction;
- 使用保留点
使用savepoint
save point delete1;
游标
游标是一个存储在DBMS服务器上的数据库查询,它不是一条select语句,而是被该语句检索出来的结果集
- 创建游标
使用declare语句创建游标
declare CustCursor Cursor for select*from Customers where cust_email is null;
- 使用游标
open curdor CustCursor
SQL高级特性
高级数据处理特性: 约束、索引和触发器。
- 定义外键
外键有助于防止意外删除
create table Orders (
order_num integer not null primary key,
order_date datetime not null,
cust_id char(10) not null references Customers(cust_id);
);
- 唯一约束 用UNIQUE关键字,也可以用单独的CONSTRAINT定义
- 检查约束