Which data type to use to reference SERIAL data type in PostgreSQL?

后端 未结 1 2048
轮回少年
轮回少年 2021-01-22 02:12

I have two tables in PostgreSQL. The first one should have an auto-incrementing ID field that the second one references:

CREATE TABLE tableA (id SERIAL NOT NULL          


        
相关标签:
1条回答
  • 2021-01-22 03:10

    A serial is an integer and it's not "unsigned". The sequence that is created automatically just happens to start at 1 - that's all. The column's data type is still an integer (you could make the sequence start at -2147483648 if you wanted to).

    Quote from the manual

    CREATE TABLE tablename (
        colname SERIAL
    );
    

    is equivalent to

    CREATE SEQUENCE tablename_colname_seq;
    CREATE TABLE tablename (
        colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
    );
    ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
    

    (emphasis mine)

    0 讨论(0)
提交回复
热议问题