ALTER TABLE cust
ADD CONSTRAINT cust_id_pk PRIMARY KEY(cust_id) DEFERRABLE INITIALLY DEFERRED;
INSERT
INTO cust VALUES (1,'RAJ'); --row 1
INSERT INTO cust VALUES (3,'SAM'); --row 2
COMMIT;
SET CONSTRAINT cust_id_pk IMMEDIATE;
INSERT INTO cust VALUES (1,'LATA'); --row 3
INSERT INTO cust VALUES (2,'KING'); --row 4
COMMIT;
Which rows would be made permanent in the CUST table?
A. row 4 only
B. rows 2 and 4
C. rows 3 and 4
D. rows 1 and 4
答案是C
其中 alter table 语句的DEFERRABLE INITIALLY DEFERRED 的官方描述:
The DEFERRABLE
and NOT
DEFERRABLE
parameters indicate whether or not, in subsequent transactions, constraint checking can be deferred until the end of the transaction using the
SET
CONSTRAINT
(S
) statement. If you omit this clause, then the default is
NOT
DEFERRABLE
.
IMMEDIATE
Specify IMMEDIATE
to cause the specified constraints to be checked immediately on execution of each constrained DML statement. Oracle Database first checks any constraints that were deferred earlier in the transaction and then continues immediately
checking constraints of any further statements in that transaction, as long as all the checked constraints are consistent and no other
SET
CONSTRAINTS
statement is issued. If any constraint fails the check, then an error is signaled. At that point, a
COMMIT
statement causes the whole transaction to undo.
Making constraints immediate at the end of a transaction is a way of checking whether
COMMIT
can succeed. You can avoid unexpected rollbacks by setting constraints to
IMMEDIATE
as the last statement in a transaction. If any constraint fails the check, you can then correct the error before committing the transaction.
DEFERRED
Specify DEFERRED
to indicate that the conditions specified by the deferrable constraint are checked when the transaction is committed.
The
INITIALLY
clause establishes the default checking behavior for constraints that are
DEFERRABLE
. The
INITIALLY
setting can be overridden by a
SET
CONSTRAINT
(
S
) statement in a subsequent transaction.
-
Specify INITIALLY
IMMEDIATE
to indicate that Oracle should check this constraint at the end of each subsequent SQL statement. If you do not specify
INITIALLY
at all, then the default is INITIALLY
IMMEDIATE
.
If you declare a new constraint INITIALLY
IMMEDIATE
, then it must be valid at the time the
CREATE
TABLE
or ALTER
TABLE
statement is committed or the statement will fail.
-
Specify INITIALLY
DEFERRED
to indicate that Oracle should check this constraint at the end of subsequent transactions.
This clause is not valid if you have declared the constraint to be NOT
DEFERRABLE
, because a NOT
DEFERRABLE
constraint is automatically
INITIALLY
IMMEDIATE
and cannot ever be INITIALLY
DEFERRED
.
有此可以知道 由于在添加约束的时候指定了DEFERRABLE INITIALLY DEFERRED ,即初始延迟;每次只要不显示执行SET CONSTRAINT 。。。IMMEDIATE命令,在语句执行的时候不会执行约束,只有在commit时,即事务要提交时才会进行约束检查 ; 当commit 时,有失败的语句(不符合约束的行),那么会自动回滚整个事务的语句; 所以最好在commit之前执行SET CONSTRAINT 。。。IMMEDIATE语句,之后发出错误信息,不会回滚,这是可以修改不符合约束的行,然后再提交;
注:在事务结束后(提交或回滚)后,SET CONSTRAINT 。。。IMMEDIATE|DEFERRED 的作用也即结束,接着对应的约束还是会按照初始的设置;
如果是下面这种情况
SET CONSTRAINT cust_id_pk IMMEDIATE;
INSERT INTO cust VALUES (1,'LATA'); --row 3
INSERT INTO cust VALUES (1,'KING'); --row 4
COMMIT;
那么只有第三行会进入表中;
分享到:
相关推荐
数据库管理与应用 4.2.3在ALTER TABLE语句中创建索引 .pdf 学习资料 复习资料 教学资源
MySQL数据库中ALTER TABLE语句的用法研究.pdf
数据库管理与应用 3.4.2 使用ALTER TABLE语句修改表结构.pdf 学习资料 复习资料 教学资源
--删除表列alter table test modify address char(10) --修改表列类型||alter table test change
一个字段: ALTER TABLE table_name MODIFY(字段类型 数据类型)); 添加一个字段: ALTER TABLE table_name ADD(字段类型 数据类型);
SQL Server中alter table一些用法.doc
实例一:使用ALTER TABLE命令向表中添加字段、修改字段类型以及设置主键。 首先创建一个表,SQL语句如下: mysql> CREATE TABLE myTable( -> ID SMALLINT -> ); 使用desc命令查看表结构: mysql> desc ...
mysql alter table语句可以修改表的基本结构,例如添加字段、删除字段、添加主键、添加索引、修改字段数据类型、对表重命名等等操作,本文章通过两个简单的实例向大家介绍mysql alter table的使用方法。 实例一:...
MYSQL ALTER TABLE命令用于修改表结构,例如添加/修改/删除字段、索引、主键等等,本文章通过实例向大家介绍MYSQL ALTER TABLE语句的使用方法, MySQL ALTER语法如下: ALTER [IGNORE] TABLE tbl_name alter_spec...
alter table in Sql server
文章目录SQL CREATE INDEX 语句索引SQL CREATE INDEX 语法SQL CREATE UNIQUE INDEX 语法SQL 撤销索引、撤销表以及撤销数据库DROP INDEX 语句...语句SQL ALTER TABLE 语句SQL ALTER TABLE 语法要改变表中列的数据类型...
alter table emlog_user add sex varchar(2) NOT NULL DEFAULT ‘1’; alter table emlog_user add qq varchar(20) NOT NULL; alter table emlog_user add zhuye varchar(50) NOT NULL; alter table emlog_user add ...
ALTER TABLE `ecs_brand` ADD COLUMN `brand_banner` VARCHAR(80) NOT NULL COMMENT '商品品牌banner'; ALTER TABLE `ecs_goods` ADD COLUMN `sales_count` int(10) UNSIGNED NOT NULL DEFAULT 0 AFTER `click_...
NULL 博文链接:https://zotao.iteye.com/blog/691609
MYSQL ALTER TABLE命令用于修改表结构,例如添加/修改/删除字段、索引、主键等等,本文章通过实例向大家介绍MYSQL ALTER TABLE语句的使用方法,需要的朋友可以参考一下。
alter table tbl_name add col_name type 例如, 给pet的表增加一列 weight, mysql>alter table pet add weight int; 2. 删除列 alter table tbl_name drop col_name 例如, 删除pet表中的weight这一列 mysql>...
收藏资料: alter sql
ALTER TABLE (add column) ALTER TABLE table_name ADD column_name datatype ALTER TABLE (drop column) ALTER TABLE table_name DROP COLUMN column_name AS (alias for column) SELECT column_name AS column_...
删除索引 数据索引 课程目标 理解 —— 删除索引的语法; 掌握 —— 删除索引的2种不同方法。 删除索引 使用DROP INDEX语句删除索引 语法格式: DROP INDEX 索引名 ON 表名 ...使用ALTER TABLE语句删除索引
MySQL Alter Table 修改表信息.docx