Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在MySQL中添加外键约束错误?_Mysql_Foreign Keys_Constraints - Fatal编程技术网

无法在MySQL中添加外键约束错误?

无法在MySQL中添加外键约束错误?,mysql,foreign-keys,constraints,Mysql,Foreign Keys,Constraints,我一直在MySQL中遇到同样的错误,我不确定我在这里遗漏了什么。 我对MySQL非常陌生,所以我不确定是否需要添加约束?如果我不需要约束,是否有其他方法来定义关系?下面是我的语法: DROP TABLE IF EXISTS `art`; CREATE TABLE `art` ( `art_id` INT NOT NULL, `art_name` VARCHAR(45) NULL, `artist_id` INT(11) NULL, `location_i

我一直在MySQL中遇到同样的错误,我不确定我在这里遗漏了什么。 我对MySQL非常陌生,所以我不确定是否需要添加约束?如果我不需要约束,是否有其他方法来定义关系?下面是我的语法:

    DROP TABLE IF EXISTS `art`;
    CREATE TABLE `art` (

   `art_id` INT NOT NULL,
   `art_name` VARCHAR(45) NULL,
   `artist_id` INT(11) NULL,
   `location_id` INT(11) NULL,
   `employee_id` INT(11) NULL,
   `art_received` DATE NULL,

  primary key (`art_id`),
  FOREIGN KEY (`artist_id`) REFERENCES artist(`artist_id`),
  FOREIGN KEY (`location_id`) REFERENCES location(`location_id`),
  FOREIGN KEY (`employee_id`) REFERENCES employee(`employee_id`)

  ); 

  DROP TABLE IF EXISTS `employee`;
 CREATE TABLE `employee` (

`employee_id` int(11),
`employee_userid` varchar(45),
`employee_fname` varchar(45),
`employee_lname` varchar(45),
`employee_phone` varchar (10),

primary key (`employee_id`)

); 

 DROP TABLE IF EXISTS `artist`;
 CREATE TABLE `artist` (
`artist_id` int(11),
`artist_first` varchar(45),
`artist_last` varchar(45),
`artwork_title` varchar(45),

primary key (`artist_id`)

 ); 

 DROP TABLE IF EXISTS `location`;
 CREATE TABLE `location` (
`location_id` int(11),
`location_name` varchar(45),
`start_date` date,
`end_date` date,

primary key (`location_id`)

); 

只需切换首先创建表的顺序即可。当您创建 表
art
其他三个表尚未创建,因此它不知道艺术家(
artist\u id
)、位置(
location\u id
)、员工(
employee\u id
) 是的


欢迎来到StackOverflow。如果您发现此答案或任何其他答案有帮助,请将其标记为解决方案。这样可以帮助社区,如果其他程序员在将来遇到与您相同的问题,他们可以很容易地找到解决方案

你能分享你得到的错误吗?如果你按照这个顺序去做,那么在创建一个不存在的表的外键时就会出现问题。您需要确保首先创建艺术所引用的所有表格。这非常有意义,而且工作也非常完美。谢谢你的帮助。
 DROP TABLE IF EXISTS `employee`;
 CREATE TABLE `employee` (

   `employee_id` int(11),
   `employee_userid` varchar(45),
   `employee_fname` varchar(45),
   `employee_lname` varchar(45),
   `employee_phone` varchar (10),

   primary key (`employee_id`)

); 

 DROP TABLE IF EXISTS artist;
 CREATE TABLE `artist` (
   `artist_id` int(11),
   `artist_first` varchar(45),
   `artist_last` varchar(45),
   `artwork_title` varchar(45),

   primary key (`artist_id`)

 ); 

 DROP TABLE IF EXISTS `location`;
 CREATE TABLE `location` (
   `location_id` int(11),
   `location_name` varchar(45),
   `start_date` date,
   `end_date` date,

   primary key (`location_id`)

); 



DROP TABLE IF EXISTS `art`;
CREATE TABLE `art` (

   `art_id` INT NOT NULL,
   `art_name` VARCHAR(45) NULL,
   `artist_id` int(11) NULL,
   `location_id` int(11) NULL,
   `employee_id` int(11) NULL,
   `art_received` DATE NULL,

    primary key (`art_id`),

    FOREIGN KEY (`artist_id`) REFERENCES artist(`artist_id`),
    FOREIGN KEY (`location_id`) REFERENCES location(`location_id`),
    FOREIGN KEY (`employee_id`) REFERENCES employee(`employee_id`)



);