Oracle FOREIGN KEY constraint (Forigen key)

Source: Internet
Author: User

Oracle FOREIGN KEY constraints

    1. A FOREIGN key constraint is defined as having a record of another table to constrain itself. Another table here is the main table.
    2. When the record of the primary table is deleted, we can delete the record (on delete CASCADE) with the main table, or the corresponding field is set to null (on delete set null), or the deletion is not allowed (default).

A) Note: When the primary table is cascade deleted (Dorp tabel WEN CASCADE CONSTRAINTS), the FOREIGN KEY constraint established from the table associated with the primary table will be deleted and the table data will not change.

      1. It is assumed that the foreign KEY constraint relationship will not be affected if the table is the primary table that corresponds to another table foreign key constraint.

b) Note Two: The Main table field that is pointed from the foreign key constraint must be a unique constraint or a PRIMARY KEY constraint.

      1. Because when a foreign key constraint points to a primary table record with duplicates, deleting one of them is accompanied by an ambiguous operation from the table.

Create a foreign key syntax when creating a table:

Primary[?pra?m?ri] Main, key

FOREIGN[?F?R?N] FOREIGN key

References[?refr?ns]

CONSTRAINT [K?N?STRE?NT]: Constraints, restrictions, coercion

CREATE TABLE WEN

(

MY CHAR (Ten) is not NULL,

Love CHAR (8) is not NULL,

PRIMARY KEY (MY),

CONSTRAINT wen_fk FOREIGN KEY (my,love)

REFERENCES Guoguo (my,love) on delete CASCADE-when the primary table record is deleted, accompany the delete from the table record

)

On DELETE SET NULL;

When the primary table record is deleted, the setting from table data is null, note that the field must be allowed to be null from the fields

Deleting the main table record is not allowed by default when the ON statement is not written.

To append a foreign key when modifying a table:

ALTER TABLE WEN

ADD CONSTRAINT wen_fk FOREIGN KEY (my,love) REFERENCES Guoguo (my,love) on DELETE CASCADE

ALTER TABLE Guoguo ADD CONSTRAINT guoguo_fk FOREIGN KEY (my,love) REFERENCES

WEN (My,love) on DELETE SET NULL

Oracle FOREIGN KEY constraint (Forigen key)

Related Article

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.