Reputation: 25
here is my table
Create table artist_song (
Artist_id int not null,
Song_id int not null,
Artist_song_type varchar(255) not null,
Artist_song_order int not null,
Constraint artist_song_pk primary key (artist_id, song_id),
Constraint artist_song_fk1 foreign key(artist_id) references artist(artist_id),
Constraint artist_song_fk2 foreign key(song_id) references song(song_id)
);
and I want to insert into date
INSERT INTO artist_song (artist_id, song_id, artist_song_type, artist_song_order) VALUES (2,13,'music',1);
but it said that
14:19:27 INSERT INTO artist_song (artist_id, song_id, artist_song_type, artist_song_order) VALUES (2,13,'music',1) Error Code: 1062.
Duplicate entry '2-13' for key 'PRIMARY' 0.000 sec
What should I DO? need I change artist_id ?
Upvotes: 0
Views: 83
Reputation: 4329
So here you have created Composite key, or composite primary key, refers to cases where more than one column is used to specify the primary key of a table.
So it seems there already exists a record with '2-13' entry in your Database table.Please check your table.
So in you table there must be 'artist_id' with value 2 and 'song_id' with value 13.
So whenever you define a primary key, it is unique throughout your column and since here you have created unique composite key it will be combinaiton of two must be unique throughout your table.
Upvotes: 1