0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Accessで外部キー(参照整合性)設定

Last updated at Posted at 2022-10-18

やり方

まず前提として下記2テーブルがある。
image.png
image.png

データベースツール ⇒ リレーションシップ 。
image.png

何も無いところで右クリック ⇒ テーブルの表示 。
右側にテーブルリスト出てくる。
image.png

『テーブルの追加』のリストからドラッグドロップ。
image.png

主カラムから従カラムに向かってドラッグドロップすると、リレーションシップの小窓出る。
参照整合性にチェック入れて作成。
image.png

↓作成終了するとこんな感じ。
image.png

『リレーションシップの設定あるよ』の意味で+マーク付く。
image.png

参照整合性設定したらどんな動きするか

インサート

この状態で…
image.png

こうインサートしようとすると…
image.png

ダメだよって怒ってくれる。
image.png

デリート

この状態で…
image.png
image.png

下記レコードを消そうとすると…
image.png

ダメだよって怒ってくれる。
image.png

蛇足

テーブル間のデータの整合性は、アプリケーション側のロジックで担保するのではなく、データベース側の参照整合性で担保した方がいい。

参照整合性を『窮屈』と捉えるのではなく、 『ダブルチェックしてくれてる』 と考えよう!

バージョン

Windows 10 Pro 21H2 OSビルド 19044.2130
Microsoft Access for Microsoft 365 MSO (バージョン 2209 ビルド 16.0.15629.20196) 32 ビット

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?