2

私がやろうとしていることの基本的な要点は次のとおりです。

create table main(id char(1) primary key);

create table other (
  id int primary key auto_increment,
  main_id char(1),
  key m (main_id),
  constraint fk foreign key (main_id) references main(id)
);

insert into main(id) values('a');
insert into other(main_id) values('a');

update main inner join other on other.main_id=main.id
set main.id='b', other.main_id='b'
where main.id='a';

これにより、外部キー制約が失敗します。外部キーを削除せずにこれを達成する方法はありますか (実際には大規模な実稼働データベースのオプションではありません)。

4

1 に答える 1

2

foreign_key_checks=0これは、セッションで一時的に設定するだけで簡単に実行できます。

set foreign_key_checks=0;

update main inner join other on other.main_id=main.id
set main.id='b', other.main_id='b'
where main.id='a';

ON UPDATE CASCADEもう 1 つのオプションは、主キーが親テーブルで更新された場合に子テーブルにカスケードするように、オプションを使用して外部キーを構成することです。

create table main(id char(1) primary key);

create table other (
  id int primary key auto_increment,
  main_id char(1),
  key m (main_id),
  constraint fk foreign key (main_id) references main(id) on update cascade
);
于 2013-08-21T14:51:31.547 に答える