简体   繁体   English

MySQL FOREIGN KEY约束语法

[英]MySQL FOREIGN KEY Constraints Syntax

When I'm going to execute this code, I'm getting this error message: 当我要执行此代码时,我收到以下错误消息:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD CONSTRAINT fk_pay_grade_scale FOREIGN KEY pay_scale_id REFERENCES `pay_s' at line 11 请查看与您的MySQL服务器版本相对应的手册,以获取在'ADD CONSTRAINT fk_pay_grade_scale FOREIGN KEY pay_scale_id REFERENCES`pay_s'附近使用的正确语法

But I don't understand the problem. 但是我不明白这个问题。 Your help is appreciated! 感谢您的帮助!

CREATE TABLE IF NOT EXISTS `pay_grades` (
    `pay_grade_id` int(20) NOT NULL,
  `pay_scale_id` tinyint(4) NOT NULL,
  `name` varchar(100) NOT NULL,
  `basic_salary` decimal(10,2) NOT NULL,  
  `status` int(2) NOT NULL DEFAULT '1',  
   PRIMARY KEY (`pay_grade_id`),
   INDEX (`pay_scale_id`, `pay_grade_id`),  
   ADD CONSTRAINT `fk_pay_grade_scale` FOREIGN KEY `pay_scale_id` REFERENCES `pay_scales`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `pay_scales` (
`id` tinyint(4) NOT NULL,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

You can not use ADD CONSTRAINT in a CREATE TABLE declaration. 您不能在CREATE TABLE声明中使用ADD CONSTRAINT Declare your constraint after creating the table or in the CREATE TABLE . 在创建表之后或在CREATE TABLE声明约束。


First solution: Add the constraint in CREATE TABLE 第一个解决方案:在CREATE TABLE中添加约束

CREATE TABLE IF NOT EXISTS `pay_grades` (
  `pay_grade_id` int(20) NOT NULL,
  `pay_scale_id` tinyint(4) NOT NULL,
  `name` varchar(100) NOT NULL,
  `basic_salary` decimal(10,2) NOT NULL,  
  `status` int(2) NOT NULL DEFAULT '1',  
   PRIMARY KEY (`pay_grade_id`),
   INDEX (`pay_scale_id`, `pay_grade_id`),  
   FOREIGN KEY (`pay_scale_id`) REFERENCES `pay_scales`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Second solution: Alter table to add the constraint 第二种解决方案:修改表以添加约束

Create your table without the constraint, and then add your constraint as follow: 创建没有约束的表,然后按如下所示添加约束:

ALTER TABLE `pay_grades` 
ADD CONSTRAINT `pay_scale_id` FOREIGN KEY REFERENCES `pay_scales`(`id`) 
ON UPDATE CASCADE ON DELETE RESTRICT;

MySQL documentation for foreign keys declaration. MySQL文档的外键声明。

It seems the difference in order of table creation. 表创建顺序似乎有所不同。 First create primary key table than create the table of foreign key. 首先创建主键表,而不是创建外键表。

CREATE TABLE IF NOT EXISTS `pay_scales` (
`id` tinyint(4) NOT NULL,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `pay_grades` (
    `pay_grade_id` int(20) NOT NULL,
  `pay_scale_id` tinyint(4) NOT NULL,
  `name` varchar(100) NOT NULL,
  `basic_salary` decimal(10,2) NOT NULL,  
  `status` int(2) NOT NULL DEFAULT '1',  
   PRIMARY KEY (`pay_grade_id`),
   INDEX (`pay_scale_id`, `pay_grade_id`),  
   ADD CONSTRAINT `fk_pay_grade_scale` FOREIGN KEY `pay_scale_id` REFERENCES `pay_scales`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM