Skip to content
Advertisement

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;

Advertisement

Answer

In MySQL you can use this:

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. es. varchar(50)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement