Mysql foreign key introduction and method for creating foreign key _ MySQL

Source: Internet
Author: User
Mysql foreign key

BitsCN.com

In MySQL, tables of the InnoDB engine type support foreign key constraints.
Usage conditions of foreign keys:
1. the two tables must be InnoDB tables, and MyISAM tables do not currently support foreign keys (it is said that foreign keys may be supported in later versions, but at least not supported currently );
2. the foreign key column must have an index. MySQL 4.1.2 and later versions will automatically create an index when creating the foreign key. However, if the foreign key column is in an earlier version, the index must be created;
3. the columns of the foreign key relationship must be of similar data types, that is, columns that can be converted to each other. for example, int and tinyint can be used, but int and char cannot;

Benefits of foreign keys: two tables can be associated to ensure data consistency and perform cascade operations;

Syntax for defining foreign keys:

[CONSTRAINT symbol] foreign key [id] (index_col_name ,...)
REFERENCES tbl_name (index_col_name ,...)
[On delete {RESTRICT | CASCADE | set null | no action | set default}]
[On update {RESTRICT | CASCADE | set null | no action | set default}]

This syntax can be used in create table and alter table. if the CONSTRAINT symbol is not specified, MYSQL automatically generates a name.
On delete and on update indicate the event trigger limit. you can set the following parameters:
RESTRICT)
CASCADE (with foreign key changes)
Set null (set null)
SET DEFAULT)
No action (no action, default)

BitsCN.com

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.