我试图改变一个名为Artist(id,name)的表,其id为主键,以便与另一个表Song(title,artist_id,year)匹配。列artist_id是外键。 删除时,artist_id列未设置为null,因此我使用此查询对其进行了更改:
ALTER TABLE歌曲ADD CONSTRAINT FK_song_4 FOREIGN KEY(artist_id)REFERENCES艺术家(id)ON DELETE SET NULL;
我也试图在Artist上实现相同的更改(在删除时将其主键设置为null,以便能够向其中一个表插入更多值,但完全相同的语法不正确改变主键,但MySQL Workbench指出语法错误。
总结一下,我的问题是: 如何在删除
上设置主键为空这是我的代码:
alter table Song add constraint FK_tragoudi_3 foreign key (artist_id) references Artist(id) ON DELETE set null ;
alter table Artist add constraint PK_artist_1 primary key (id) on delete set null;
SET @titl="Beat it";
SET @composer ="122";
SET @year= 1990;
SET @song_writ="211";
insert into Song (title, composer, year, song_writer) values (@titl, @compose, @yearr, @song_writ);
insert into Artist (id, name, surname, year) values (@composer, "George", "Smith", 1960);