2015-04-27 42 views
0

MysqlIDAuto Increment但是當轉換爲Postgres沒有Auto Increment無自動增量當MySQL數據庫轉換爲Postgres數據庫

Mysql數據庫

CREATE TABLE IF NOT EXISTS `cities` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `state_id` bigint(20) NOT NULL, 
    `district_id` bigint(20) NOT NULL, 
    `name` varchar(255) NOT NULL, 
    `description` varchar(1000) DEFAULT NULL, 
    `created_by` int(11) NOT NULL, 
    `modified_by` int(11) DEFAULT NULL, 
    `is_active` char(1) NOT NULL DEFAULT 'Y', 
    `created` datetime DEFAULT NULL, 
    `modified` datetime DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `state_id` (`state_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ; 

後轉換爲Postgres數據庫

CREATE TABLE cities (
    "id" bigint NOT NULL, 
    state_id bigint NOT NULL, 
    district_id bigint NOT NULL, 
    "name" varchar(255) NOT NULL, 
    description varchar(1000), 
    created_by int NOT NULL, 
    modified_by int, 
    is_active char(1) NOT NULL, 
    created timestamp, 
    modified timestamp, 
    PRIMARY KEY ("id") 
); 

INSERT INTO cities(state_id, district_id, city_type, name, description, 
    created_by, modified_by, is_active, created, modified) 
VALUES 
    (1, 1, '', 'Ramtek', null, 1, null, 'Y', 
    '2015-04-16 10:44:11', '2015-04-16 10:44:11'); 
+1

變化' 「ID」 BIGINT NOT NULL,''到ID BIGSERIAL不NULL'。手冊中的更多詳細信息:http://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL –

+0

另請參閱http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL –

回答

0

你可以在這種情況下使用bigserial。它在postgres中提供了一個自動遞增功能:

CREATE TABLE cities (
    "id" bigserial PRIMARY KEY, 

默認情況下會提供非空約束。

文檔:http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-SERIAL

+0

謝謝它的工作,但有一個疑問什麼時候使用smallserial,bigserial和serial – vnnogile

+0

從文檔「類型名稱serial和serial4是等價的:都創建整數列。類型名稱bigserial和serial8以同樣的方式工作,除了他們創建一個bigint如果預期在表的生命週期中使用超過231個標識符,則應使用bigserial。類型名稱smallserial和serial2也以相同的方式工作,但它們會創建一個smallint列。 – Balaji

+0

現在,當我試圖從前端添加數據它給了一些數據庫錯誤SQLSTATE [23502]:非空違反:7錯誤:列中的空值違反非空約束,當我將我的代碼更改爲「id」bigserial NOT NULL , – vnnogile