MySQL foreign key settings and role

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags clients control customer customer number data default delete example

The role of foreign key:

Maintain data consistency, integrity, the main purpose is to control the data stored in the foreign key table. So that the formation of the two tables, the foreign key can only refer to the appearance of the value of the column!

E.g:

ab two tables

a table holds the customer number, customer name

b There are orders for each customer in the table

With the foreign key

You can only delete customer x in a if you are certain that there is no order for customer x in table b

The premise of the establishment of foreign keys: The table column must be the same type of foreign key (the foreign key must be the primary key).

Specify the primary key keyword: foreign key (column name)

Reference foreign key keywords: references <foreign key table name> (foreign key column name)

Event trigger limits: on delete and on update, can be set parameters cascade (with the foreign key changes), restrict (foreign key changes in the appearance of changes), set Null (set null), set Default (default), [default ] no action

E.g:

outTable table primary key id type int

Create a table containing foreign keys:

create table temp (

id int,

name char (20),

foreign key (id) references outTable (id) on delete cascade on update cascade);

Description: The id column as a foreign key reference appearance outTable id column when the value of the foreign key to delete the corresponding column in the table screen when the value of the foreign key to change the corresponding column value changes in this table.

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.