Welcome! 登入 註冊
美寶首頁 美寶百科 美寶論壇 美寶落格 美寶地圖

Advanced

Postgresql 和 MySQL 語法相異處整理:Create Table, KEY / INDEX, BIGINT / INT UNSIGNED

Posted by HP 
1. PostgreSQL 沒有 CREATE TABLE IF NOT EXISTS 的語法

乍看之下好像 PostgreSQL 的語法比較弱,但事實上是 PostgreSQL 會自動判斷 table 存在與否,所以直接寫 CREATE TABLE 就可以了。

2. PostgreSQL 在 CREATE TABLE 時不能建 non-unique key

在 MySQL 中常會有這樣的語法:
CREATE TABLE table (
        request_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        user_id INT UNSIGNED NOT NULL ,
        KEY (user_id)
);

最後的 KEY (user_id) 是建立一個非唯一限制的 index,但在 PostgreSQL 中 CREATE TABLE 時只能建立 UNIQUE INDEX,所在基本上要建 non-unique key 只能獨立地以 CREATE INDEX 建立。

3. PostgreSQL 沒有 BIGINIT UNSIGNED / INT UNSIGNED 的 data type

PostgreSQL 和 MySQL 的 Data type 對照表:

http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL#Data_Types


參考資料:
1. Postgresql: how to create table only if it does not already exist?
http://stackoverflow.com/questions/435424/postgresql-how-to-create-table-only-if-it-does-not-already-exist
2. Converting MySQL to PostgreSQL
http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL