Foreign database key, database

Source: Internet
Author: User

Foreign database key, database
Foreign key

Create:

Method 1: when creating a table, use foreign key (field name) references table name (primary key );

Method 2: add foreign key (field name) references table name (primary key) by modifying the table structure );

Delete:

Alter table Name drop foreign key name;

If you want to view the foreign key name: show create table name;

Foreign key constraints:

A field in Table B is the primary key field in table.

Table A is the master table, and table B is the slave table:

The master table is restricted by the slave table.

1. Table A cannot be deleted at will.

2. Changes in the records in Table A cannot be deleted or updated at will. The implication is certain.

Empty Constraint

When a record in the primary table is deleted, the call from the record's foreign key field is null.

Syntax:

On delete set null

Cascade Constraints

When the record of the primary table is updated, the foreign key field in the record is updated synchronously.

 

The slave table is restricted by the master table.

1. Data in the slave table: the value of the foreign key field in the slave table. It must be a value in the primary key field of the master table.

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.