columnã®drop
sqlで面白そうなことやってたのでツッこんでみる
çµæã¤ãããçµæ§ã§ãããªã£ãã®ã§ãã£ã¡ã§ã
ã¾ãã¯ãã¹ããã¼ãã«ã®ä½æ
sqlite> .explain on sqlite> create table test ( ...> id integer, ...> field1 text ...> );
ã«ã©ã ã®è¿½å
sqlite> alter table test add column field2 text; sqlite> insert into test values (1, 'field1', 'field2'); sqlite> select * from test; id field1 field2 ---- -------------- ---------- 1 field1 field2
ã«ã©ã ã®åé¤
sqlite> alter table test drop column field1; SQL error: near "drop": syntax error
試ãã¦ã¿ãã¨ãã確ãã«drop column使ãã¾ãããã
ããã¼ã
sqlite> alter table test rename to temp; sqlite> select * from temp; id field1 field2 ---- -------------- ---------- 1 field1 field2
ãã¼ãã«ã®ãªãã¼ã ã¯å¯è½ã§ãã
ããã§æ§ãã¼ãã«ãããããªãã«ã©ã ããããããããã¼ãã«ãä½ãç´ãã»ã»ã»
sqlite> create table test ( ...> id integer, ...> field2 text ...> );
insert into table_name selectã»ã»ã»ã§ãªãã¼ã ãããã¼ãã«ããæ®ã£ãã«ã©ã ã®ãã¼ã¿ãã³ãã¼ããã°ã»ã»ã»
sqlite> insert into test select id, field2 from temp; sqlite> select * from test; id field2 ---- -------------- 1 field2
ã«ã©ã ã®ããããããããã¼ãã«ãã§ããããã¾ãã
sqlite> drop table temp; sqlite> select * from temp; SQL error: no such table: temp
ãã¨ã¯ãããªããªã£ãæ§ãã¼ãã«ãåé¤ããã°ï¼¯ï¼«ã
ãããæã®ãã¼ã¸ã§ã³ã ã£ããã®MySQLã ã£ããã§ã®drop columnæ¹æ³ã¨ãã¦ã©ã£ãã§è¼ã£ã¦ãæ¹æ³ã ã£ãã¯ãã§ããããããããPostgreSQLã ã£ãããããã¾ãããã»ã»ã»
å ã®ãã¼ãã«ããã¤ã«ã©ã åã¨ãåãããããªãã¨æ°è¦ã«ä½ãç´ããªãã®ã§å¤å°é¢åã§ããã©(ãã¼ã¿ãã¼ã¹ã«ãã£ã¦ã¯ã·ã¹ãã ãã£ã¯ã·ã§ããªãªã©ããåå¨ãããã¼ãã«æ å ±ãã«ã©ã æ å ±ãåãåºããã¨ãåºæ¥ãç°¡åã«ä½ãããã¼ã¿ãã¼ã¹ãããã¾ã)ã