Oracle可延迟约束Deferable的使用
欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 标准规定,约束可以是deferrable或not deferrable(默认)。 not deferrable 约束在每一个DML语句后检查; deferrable 约束可以在每一个insert,delete,或update(即时模式)后立即检查,或者在事
欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入
标准规定,约束可以是deferrable或not deferrable(默认)。
not deferrable 约束在每一个DML语句后检查;
deferrable 约束可以在每一个insert,delete,或update(即时模式)后立即检查,或者在事务末尾检查(延迟模式)
当没有按特定顺序执行数据加载时,这项功能特别有用――它允许先把数据载入子表,然后再装入父表。
另一种用法是在加载不符合某个check约束的数据之后,对其进行适当的更新。
语法如下:
[ [not] deferrable [initially {immediate | deferred} ] ]
或
[ [initially {immediate | deferred} ] [not] deferrable ]
1 deferrable介绍
1.1 deferrable的两个选项区别
deferrable表示该约束是可延迟验证的。 它有两个选项:
Initially immediate(默认): 立即验证, 执行完一个sql后就进行验证;
Initially deferred: 延迟验证, 当事务提交时或调用set constraint[s] immediate语句时才验证。
区别是: 事务提交时验证不通过, 则立即回滚事务; set constraint[s] immediate时只验证, 不回滚事务。
1.2 not deferrable与deferrable区别
区别就在于: “立即验证的可延迟约束” 是可以根据需要设置成 “延迟验证的可延迟约束”的, 而“不可延迟验证”是不能改变的。
2 deferrable实例
2.1 建表
create table test1(a number(1) constraint check_a check(a > 0) deferrable
initially immediate,
b number(1) constraint check_b check(b > 0) deferrable
initially deferred);
2.2 正常插入,没问题
SQL> insert into test1 values(1, 1);
1 row inserted
2.3 检验立即验证:数据不能插入
SQL> insert into test1 values(-1, 1);
insert into test1 values(-1, 1)
ORA-02290: 违反检查约束条件 (MYHR.CHECK_A)
2.4 检验延迟验证:可以执行
SQL> insert into test1 values(1, -1);
1 row inserted
SQL> select * from test1;
A B
-- --
1 1
1 -1
2.5 提交延迟验证(commit):验证失败,自动回滚
SQL> commit;
commit
ORA-02091: 事务处理已回退
ORA-02290: 违反检查约束条件 (MYHR.CHECK_B)
2.6 提交延迟验证(set constraint immediate):验证失败,不回滚
SQL> insert into test1 values(1, -1);
1 row inserted
SQL> set constraint check_b immediate;
set constraint check_b immediate
ORA-02290: 违反检查约束条件 (MYHR.CHECK_B)
或者将所有的约束做修改: alter session set constraints = immediate;
或者:set constraints all immediate;
2.7 将延迟验证设置为立即验证:则在插入时出错
SQL> set constraint check_b immediate;
Constraints set
SQL> insert into test1 values(1,-1);
insert into test1 values(1,-1)
ORA-02290: 违反检查约束条件 (MYHR.CHECK_B)
[1] [2]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.
