简体   繁体   English

我收到此错误:ALTER TABLE 语句与 FOREIGN KEY 约束“foreign_key1”冲突

[英]i getting this error: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "foreign_key1"

CREATE TABLE singer( 
    name varchar(50) primary key,
    email varchar(50) not null,
    account_number varchar(50) not null,
    balance int not null
);
create table song(
    id int primary key,
    name varchar(50) not null,
    singer varchar(50) foreign key references singer,
    producer varchar(50) not null,
    album varchar(50) not null,
    total_sell int not null
);
CREATE TABLE producer(name VARCHAR(20) PRIMARY KEY);
ALTER TABLE song ALTER COLUMN producer VARCHAR(20);
ALTER TABLE song ADD CONSTRAINT foreign_key1 FOREIGN KEY(producer) REFERENCES producer;

In MySQL you can use this:在 MySQL 中你可以使用这个:

CREATE TABLE producer(
   name varchar(20),
   PRIMARY KEY (name)
);
CREATE TABLE song(
     id int,
     name varchar(50) not null,
     singer varchar(50) not null,
     producer varchar(50) not null,
     album varchar(50) not null,
     total_sell int not null,
     PRIMARY KEY (`id`),
     CONSTRAINT `foreign_key1` FOREIGN KEY (`producer`) REFERENCES `producer` (`name`)
);

OR:或者:

ALTER TABLE `song`
  ADD CONSTRAINT `foreign_key1`
    FOREIGN KEY (`producer`)
      REFERENCES `producer` (`name`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;

you can see Here .你可以在这里看到。

I can advise you to declare the same length as the name column.我可以建议您声明与 name 列相同的长度。 es. es. varchar(50)变量字符(50)

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

相关问题 ALTER TABLE 语句与 FOREIGN KEY 约束冲突 - The ALTER TABLE statement conflicted with the FOREIGN KEY constraint ALTER TABLE 语句与 FOREIGN KEY 约束冲突 - ALTER TABLE statement conflicted with the FOREIGN KEY constraint EF:ALTER TABLE语句与FOREIGN KEY约束冲突 - EF: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint 实体框架 ALTER TABLE 语句与 FOREIGN KEY 约束冲突 - Entity Framework The ALTER TABLE statement conflicted with the FOREIGN KEY constraint ALTER TABLE语句与FOREIGN KEY约束冲突 - The ALTER TABLE statement conflicted with the FOREIGN KEY constrain 错误:INSERT语句与FOREIGN KEY约束冲突 - Error : INSERT statement conflicted with the FOREIGN KEY constraint 错误:INSERT语句与FOREIGN KEY约束冲突 - Error: The INSERT statement conflicted with the FOREIGN KEY constraint 错误:INSERT语句与FOREIGN KEY约束冲突 - Getting error: INSERT statement conflicted with the FOREIGN KEY constraint 当我尝试在SQL Server中重新启用约束时,出现“ ALTER TABLE语句与COLUMN FOREIGN KEY约束冲突” - When I try to re-enable a constraint in SQL Server, I get “ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint” 与EF6中现有表的一对多关系:ALTER TABLE语句与FOREIGN KEY约束冲突 - One-to-Many relation to existed table in EF6: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM