SQL - SQLite - 複数テーブルのデータベース設計 - 現行テーブルからの脱却 - alter, set, add, drop, column
Head First SQL ―頭とからだで覚えるSQLの基本、 Lynn Beighley(著)、 佐藤 直生(監訳)、 松永 多苗子(翻訳)、 オライリージャパンの 7章(複数テーブルのデータベース設計 - 現行テーブルからの脱却)、p.290(自分で考えてみよう)の解答を求めてみる。
入出力結果(Terminal, Zsh)
% sqlite3 gregs_list.db
SQLite version 3.50.1 2025-06-06 14:52:32
Enter ".help" for usage hints.
sqlite> .headers on
sqlite> select * from my_contacts;
last_name|first_name|gender|email|birthday|profession|location|status|interests|seeking|phone|city|state
アンダーソン|ジリアン|F|jill_anderson@breakneckpizza.com|1980-09-05|テクニカルライター|カリフォルニア州パロアルト|独身|カヤック乗り、爬虫類|恋人、友達|||
sqlite> pragma table_info(my_contacts);
cid|name|type|notnull|dflt_value|pk
0|last_name|TEXT|0||0
1|first_name|TEXT|0||0
2|gender|TEXT|0||0
3|email|TEXT|0||0
4|birthday|date|0||0
5|profession|TEXT|0||0
6|location|TEXT|0||0
7|status|TEXT|0||0
8|interests|TEXT|0||0
9|seeking|TEXT|0||0
10|phone|TEXT|0||0
11|city|TEXT|0||0
12|state|TEXT|0||0
sqlite> alter table my_contacts add column interest1 text;
sqlite> alter table my_contacts add column interest2 text;
sqlite> alter table my_contacts add column interest3 text;
sqlite> alter table my_contacts add column interest4 text;
sqlite> select substr(interests, 1, instr(interests, '、') - 1) from my_contacts ;
substr(interests, 1, instr(interests, '、') - 1)
カヤック乗り
sqlite> update table my_contacts set int
interest1 interest2 interest3 interest4 interests INTERSECT INTO
sqlite> update table my_contacts set interest1 = substr(interests, 1, instr(interests, '、') - 1);
Parse error: near "table": syntax error
update table my_contacts set interest1 = substr(interests, 1, instr(interests,
^--- error here
sqlite> update my_contacts set interest1 = substr(interests, 1, instr(interests, '、') - 1);
sqlite> select * from my_contacts;
last_name|first_name|gender|email|birthday|profession|location|status|interests|seeking|phone|city|state|interest1|interest2|interest3|interest4
アンダーソン|ジリアン|F|jill_anderson@breakneckpizza.com|1980-09-05|テクニカルライター|カリフォルニア州パロアルト|独身|カヤック乗り、爬虫類|恋人、友達||||カヤック乗り|||
sqlite> select length(interest1) from my_contacts ;
length(interest1)
6
sqlite> select substring(interests, length(interest1)) from my_contacts ;
substring(interests, length(interest1))
り、爬虫類
sqlite> select substring(interests, length(interest1) + 2) from my_contacts ;
substring(interests, length(interest1) + 2)
爬虫類
sqlite> update my_contacts set interests = substring(interests, length(interest1) + 2);
sqlite> select * from my_contacts;
last_name|first_name|gender|email|birthday|profession|location|status|interests|seeking|phone|city|state|interest1|interest2|interest3|interest4
アンダーソン|ジリアン|F|jill_anderson@breakneckpizza.com|1980-09-05|テクニカルライター|カリフォルニア州パロアルト|独身|爬虫類|恋人、友達||||カヤック乗り|||
sqlite> update my_contacts set interest2 = substr(interests, 1, instr(interests,
'、') - 1);
sqlite> select * from my_contacts;
last_name|first_name|gender|email|birthday|profession|location|status|interests|seeking|phone|city|state|interest1|interest2|interest3|interest4
アンダーソン|ジリアン|F|jill_anderson@breakneckpizza.com|1980-09-05|テクニカルライター|カリフォルニア州パロアルト|独身|爬虫類|恋人、友達||||カヤック乗り|||
sqlite> update my_contacts set interest2 = interests;
sqlite> select * from my_contacts;
last_name|first_name|gender|email|birthday|profession|location|status|interests|seeking|phone|city|state|interest1|interest2|interest3|interest4
アンダーソン|ジリアン|F|jill_anderson@breakneckpizza.com|1980-09-05|テクニカルライター|カリフォルニア州パロアルト|独身|爬虫類|恋人、友達||||カヤック乗り|爬虫類||
sqlite> alter table drop column interests;
Parse error: near "drop": syntax error
alter table drop column interests;
^--- error here
sqlite> alter table my_contacts drop column interests;
sqlite> select * from my_contacts;
last_name|first_name|gender|email|birthday|profession|location|status|seeking|phone|city|state|interest1|interest2|interest3|interest4
アンダーソン|ジリアン|F|jill_anderson@breakneckpizza.com|1980-09-05|テクニカルライター|カリフォルニア州パロアルト|独身|恋人、友達||||カヤック乗り|爬虫類||
sqlite> pragma table_info(my_contacts);
cid|name|type|notnull|dflt_value|pk
0|last_name|TEXT|0||0
1|first_name|TEXT|0||0
2|gender|TEXT|0||0
3|email|TEXT|0||0
4|birthday|date|0||0
5|profession|TEXT|0||0
6|location|TEXT|0||0
7|status|TEXT|0||0
8|seeking|TEXT|0||0
9|phone|TEXT|0||0
10|city|TEXT|0||0
11|state|TEXT|0||0
12|interest1|TEXT|0||0
13|interest2|TEXT|0||0
14|interest3|TEXT|0||0
15|interest4|TEXT|0||0
sqlite> .quit
%