Postgresql 建立主鍵並設定自動遞增的三種方法

來源:互聯網
上載者:User

Postgresql 有以下三種方法設定主鍵遞增的方式,下面來看下相同點和不同點。

--方法一
create table test_a
(
  id serial,
  name character varying(128),
constraint pk_test_a_id primary key( id)
);

NOTICE:  CREATE TABLE will create implicit sequence "test_a_id_seq" for serial column "test_a.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pk_test_a_id" for table "test_a"
CREATE TABLE

--方法二
create table test_b
(
  id serial PRIMARY KEY,
  name character varying(128)
);

NOTICE:  CREATE TABLE will create implicit sequence "test_b_id_seq" for serial column "test_b.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_b_pkey" for table "test_b"
CREATE TABLE

--方法三
create table test_c
(
  id integer PRIMARY KEY,
  name character varying(128)
); 
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_c_pkey" for table "test_c"
CREATE TABLE

//方法三上面的一小段是工具產生的,如果表已經建好,只要用下面的語句即可產生自動成長序列

CREATE SEQUENCE test_c_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
   
alter table test_c alter column id set default nextval('test_c_id_seq');

     很明顯從上面可以看出,方法一和方法二隻是寫法不同,實質上主鍵都通過使用 serial 類型來實現的,
使用serial類型,PG會自動建立一個序列給主鍵用,當插入表資料時如果不指定ID,則ID會預設使用序列的
NEXT值。   
   
    方法三是先建立一張表,再建立一個序列,然後將表主鍵ID的預設值設定成這個序列的NEXT值。這種寫法
似乎更符合人們的思維習慣,也便於管理,如果系統遇到sequence 效能問題時,便於調整 sequence 屬性;

--比較三個表的表結構
skytf=> \d test_a
                                 Table "skytf.test_a"
Column |          Type          |                      Modifiers                     

--------+------------------------+-----------------------------------------------------
id     | integer                | not null default nextval('test_a_id_seq'::regclass)
name   | character varying(128) |
Indexes:
    "pk_test_a_id" PRIMARY KEY, btree (id)
   
   
skytf=> \d test_b
                                 Table "skytf.test_b"
Column |          Type          |                      Modifiers                     

--------+------------------------+-----------------------------------------------------
id     | integer                | not null default nextval('test_b_id_seq'::regclass)
name   | character varying(128) |
Indexes:
    "test_b_pkey" PRIMARY KEY, btree (id)
       
   
skytf=> \d test_c
                                 Table "skytf.test_c"
Column |          Type          |                      Modifiers                     

--------+------------------------+-----------------------------------------------------
id     | integer                | not null default nextval('test_c_id_seq'::regclass)
name   | character varying(128) |
Indexes:
    "test_c_pkey" PRIMARY KEY, btree (id)
   
     從上面可以看出,三個表表結構一模一樣, 三種方法如果要尋找差別,可能僅有以下一點,
當 drop 表時,方法一和方法二會自動地將序列也 drop 掉, 而方法三不會。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.