MySQL可以通過CREATE TABLE 或者ALTER TABLE 來定義MySQL外鍵。下面就為您介紹這兩種方法相關的語法,希望對您學習MySQL外鍵方面有所幫助。
CREATE TABLE 定義MySQL外鍵的語法:
- CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
- [(create_definition,…)]
- create_definition:
- column_definition
- | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,…) [reference_definition]
- column_definition:
- col_name type [NOT NULL | NULL] [DEFAULT default_value]
- [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
- [COMMENT ’string’] [reference_definition]
- index_col_name:
- col_name [(length)] [ASC | DESC]
- reference_definition:
- REFERENCES tbl_name [(index_col_name,…)]
- [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
- [ON DELETE reference_option]
- [ON UPDATE reference_option]
- reference_option:
- RESTRICT | CASCADE | SET NULL | NO ACTION
ALTER TABLE 定義MySQL外鍵的語法:
- ALTER [IGNORE] TABLE tbl_name
- alter_specification [, alter_specification] …
- alter_specification:
- | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,…)
- | ADD [CONSTRAINT [symbol]] UNIQUE [INDEX] [index_name] [index_type] (index_col_name,…)
- | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,…) [reference_definition]
- | DROP FOREIGN KEY fk_symbol
使用MySQL外鍵的四個條件
MySQL外鍵和參照完整性的聯系
MySQL設置自增字段的方法介紹
mysql插入Clob字段的實例
常見MySql字段的默認長度