計算機科学のブログ

ほしい物リスト

SQL - SQLite - 複数テーブルのデータベース設計 - 現行テーブルからの脱却 - 2NF、再設計

Head First SQL ―頭とからだで覚えるSQLの基本Lynn Beighley(著)、 佐藤 直生(監訳)、 松永 多苗子(翻訳)、 オライリージャパンの 7章(複数テーブルのデータベース設計 - 現行テーブルからの脱却)、p.333(自分で考えてみよう)の解答を求めてみる。

sample10.sql

create table toy_info
(
    toy_id integer primary key autoincrement,
    toy text,
    color text,
    cost real,
    weight real
);
create table store_info(
    store_id integer primary key autoincrement,
    store_address text,
    phone text,
    manager text
);
create table store_inventory(
    toy_id integer,
    store_id integer,
    inventory integer
);

入出力結果(Terminal, Zsh)

% sqlite3 toys.db < sample10.sql 
% sqlite3 toys.db 
SQLite version 3.50.1 2025-06-06 14:52:32
Enter ".help" for usage hints.
sqlite> .tables
store_info       store_inventory  toy_info       
sqlite> pragma table_info(store_info);
0|store_id|INTEGER|0||1
1|store_address|TEXT|0||0
2|phone|TEXT|0||0
3|manager|TEXT|0||0
sqlite> pragma table_info(store_inventory);
0|toy_id|INTEGER|0||0
1|store_id|INTEGER|0||0
2|inventory|INTEGER|0||0
sqlite> pragma table_info(toy_info);
0|toy_id|INTEGER|0||1
1|toy|TEXT|0||0
2|color|TEXT|0||0
3|cost|REAL|0||0
4|weight|REAL|0||0
sqlite> .quit
%