search
HomeDatabaseMysql TutorialOracle可延迟约束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] 

Oracle可延迟约束Deferable的使用

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.