SQLiを実行する際の便利な文法なので、一般的なSQLの参考にはならない。
データベース一覧
DBMS固有の関数を使わずに、select文を使用。
MySQL | select distinct table_schema from information_schema.tables; |
PostgreSQL | select datname from pg_catalog.pg_database; |
SQLite | – |
さらに上記の結果を1つのカラムにまとめる方法。
MySQL | select group_concat(distinct(table_schema)) from information_schema.tables; |
PostgreSQL | select array_to_string(array_agg(distinct(datname)), ‘,’) from pg_catalog.pg_database; |
SQLite | – |
テーブルのカラム一覧
さらに上記の結果を1つのカラムにまとめる方法。
DB名に存在するテーブル一覧
MySQL | select table_name from information_schema.tables where table_schema = ‘<DB名>’; |
PostgreSQL | select tablename from pg_catalog.pg_tables where schemaname = ‘public’; |
SQLite | select name from sqlite_master where type=’table’; |
さらに上記の結果を1つのカラムにまとめる方法。
MySQL | select group_concat(distinct(table_name), ‘,’) from information_schema.tables where table_schema = ‘<DB名>’; |
PostgreSQL | postgres=# select array_to_string(array_agg(distinct(tablename)), ‘,’) from pg_catalog.pg_tables where schemaname = ‘public’; |
SQLite | select group_concat(distinct(name)) from sqlite_master where type=’table’; |
テーブル作成
MySQL | create table users (id int auto_increment, name varchar(255), email varchar(255), password varchar(255), primary key(id)); |
PostgreSQL | create table users (id serial, name varchar(255), email varchar(255), password varchar(255), primary key(id)); |
SQLite | create table users(id integer primary key, name string, email string, password, string); |
idをauto incremetする場合。