MySQL foreign key syntax

Source: Internet
Author: User

MySQL can use create table or alter table to define the MySQL foreign key. The following describes the syntaxes related to these two methods, and hopes to help you learn about MySQL Foreign keys.

Create table defines MySQL foreign key Syntax:

 
 
  1. CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name  
  2.       [(create_definition,…)]  
  3.  
  4. create_definition:  
  5.       column_definition  
  6.     | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,…) [reference_definition]  
  7.  
  8. column_definition:  
  9.       col_name type [NOT NULL | NULL] [DEFAULT default_value]  
  10.           [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]  
  11.           [COMMENT ’string’] [reference_definition]  
  12.  
  13. index_col_name:  
  14.       col_name [(length)] [ASC | DESC]  
  15.  
  16. reference_definition:  
  17.       REFERENCES tbl_name [(index_col_name,…)]  
  18.                  [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]  
  19.                  [ON DELETE reference_option]  
  20.                  [ON UPDATE reference_option]  
  21.  
  22. reference_option:  
  23.       RESTRICT | CASCADE | SET NULL | NO ACTION  

Alter table defines MySQL foreign key Syntax:

 
 
  1. ALTER [IGNORE] TABLE tbl_name  
  2.       alter_specification [, alter_specification] …  
  3.  
  4. alter_specification:  
  5.     | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,…)  
  6.     | ADD [CONSTRAINT [symbol]] UNIQUE [INDEX] [index_name] [index_type] (index_col_name,…)  
  7.     | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,…) [reference_definition]  
  8.     | DROP FOREIGN KEY fk_symbol  


Four conditions for using the MySQL foreign key

Connection between MySQL Foreign keys and reference integrity

How to Set auto-increment fields in MySQL

Mysql inserts an instance of the Clob Field

Default length of common MySql Fields

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.