SQLの可読性が下がる上にバグになりやすいけど、こんな事もできるという程度のメモ。
PostgreSQLの場合、テーブル名やカラム名で予約語を使うには、ダブルクォートで囲う。
文字列にダブルクォートが入っている場合は、重ねてエスケープする。
psql=> CREATE TABLE "table" ("select" integer, "*" text, "quo""te" text); psql=> \d table テーブル "public.table" カラム | 型 | 修飾語 --------+---------+-------- select | integer | * | text | quo"te | text | psql=> INSERT INTO "table" ("select", "*", "quo""te") VALUES (1, 'val_1', 'val_2'); psql=> SELECT "select", "*", "quo""te" FROM "table"; select | * | quo"te --------+-------+-------- 1 | val_1 | val_2
ダブルクォートだらけになって分かりにくい。
SQLiteの場合も、ダブルクォートで囲う。
sqlite> CREATE TABLE "table" ("select" integer, "*" text, "quo""te" text); sqlite> .schema "table" CREATE TABLE "table" ("select" integer, "*" text, "quo""te" text); sqlite> INSERT INTO "table" ("select", "*", "quo""te") VALUES (1, 'val_1', 'val_2'); sqlite> SELECT "select", "*", "quo""te" FROM "table"; 1|val_1|val_2
MySQLの場合は、バッククォートで囲う。
mysql> CREATE TABLE `table` (`select` integer, `*` text, `quo"te` text); mysql> DESC `table`; +--------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | select | int(11) | YES | | NULL | | | * | text | YES | | NULL | | | quo"te | text | YES | | NULL | | +--------+---------+------+-----+---------+-------+ mysql> INSERT INTO `table` (`select`, `*`, `quo"te`) VALUES (1, 'val_1', 'val_2'); mysql> SELECT `select`, `*`, `quo"te` FROM `table`; +--------+-------+--------+ | select | * | quo"te | +--------+-------+--------+ | 1 | val_1 | val_2 | +--------+-------+--------+