Oracle foreign key related constraints intermediate join deletion scheme

Source: Internet
Author: User

Cascading deletion in the constraints related to SQL and Oracle Foreign keys. If you want to delete related records in the relevant system, we need to join multiple tables to delete N, and there are also constraints between them. therefore, when creating a table, add constraints.

The details are as follows:

Oracle foreign key constraints of SQL can achieve cascading deletion and cascading update; Oracle only supports cascading deletion.

Format of SQL cascading deletion and cascading update:

 
 
  1. CREATE TABLE A001ID INT PRIMARY KEY,NAME VARCHAR20))  
  2. CREATE TABLE A002ID INT REFERENCES A001ID)ON DELETE CASCADE ON UPDATE CASCADE,AGE TINYINT) 

Format of Oracle cascading deletion:

 
 
  1. CREATE TABLE A001ID INT PRIMAY KEY,NAME VARCHAR220))  
  2. CREATE TABLE A002ID INT REFERENCES A001ID)ON DELETE CASCADE,AGE NUMBER2,0))  
  3. CREATE TABLE groups  
  4. (  
  5. id VARCHAR2(16) CONSTRAINT pk_groupid PRIMARY KEY,  
  6. name VARCHAR2(32),  
  7. description VARCHAR2(50)  
  8. )  
  9. TABLESPACE userspace;  
  10. CREATE TABLE usringrp  
  11. (  
  12. group_id VARCHAR2(16) CONSTRAINT fk_uing_grpid  
  13. REFERENCES groups(id)  
  14. ON DELETE CASCADE,  
  15. user_id VARCHAR2(16)  
  16. )  
  17. TABLESPACE userspace;  
  18. PowerDesigner  

Integrity constraints

Restrict ). Modification or deletion operations are not allowed. If the primary key of the primary table is modified or deleted, if the Sub-table contains a sub-record, an error message is displayed. This is the default integrity settings.

Set Null ). If the Oracle foreign key column is allowed to be empty, if you modify or delete the primary key of the master table, set the foreign key column referenced in the child table to NULL ).

Set Default ). If the Default value is specified, if the primary key of the master table is modified or deleted, set the Oracle foreign key referenced in the sub-table to the Default value ).

Cascade ). When the primary key of the primary table is changed to a new value, the Oracle foreign key value of the subtable is modified accordingly; or when the primary key record of the primary table is deleted, delete the records of Foreign keys in the sub-table.

Note: constraint created in Oracle includes primay key and foreign key. to modify the constraint, you must first alter table drop contraint con_name; then add constraint again.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.