runoops.com

PostgreSQL AUTO INCREMENT(自动增长)

AUTO INCREMENT(自动增长) 会在新记录插入表中时生成一个唯一的数字。

PostgreSQL 使用序列来标识字段的自增长,数据类型有 smallserial、serial 和 bigserial 。这些属性类似于 MySQL 数据库支持的 AUTO_INCREMENT 属性。

使用 MySQL 设置自动增长的语句如下:

CREATE TABLE IF NOT EXISTS `runoops_tbl`(
   `runoops_id` INT UNSIGNED AUTO_INCREMENT,
   `runoops_title` VARCHAR(100) NOT NULL,
   `runoops_author` VARCHAR(40) NOT NULL,
   `submission_date` DATE,
   PRIMARY KEY ( `runoops_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

MySQL 是用 AUTO_INCREMENT 这个属性来标识字段的自增。

PostgreSQL 使用序列来标识字段的自增长:

CREATE TABLE runoops
(
    id serial NOT NULL,
    alttext text,
    imgurl text
)

SMALLSERIAL、SERIAL 和 BIGSERIAL 范围:

伪类型存储大小范围
SMALLSERIAL2字节1 到 32,767
SERIAL4字节1 到 2,147,483,647
BIGSERIAL8字节1 到 922,337,2036,854,775,807

语法

SERIAL 数据类型基础语法如下:

CREATE TABLE tablename (
   colname SERIAL
);

实例

假定我们要创建一张 COMPANY 表,并创建下面几个字段:

runoopsdb=# CREATE TABLE COMPANY(
   ID  SERIAL PRIMARY KEY,
   NAME           TEXT      NOT NULL,
   AGE            INT       NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

现在往表中插入几条记录:

INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'Paul', 32, 'California', 20000.00 );

INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ('Allen', 25, 'Texas', 15000.00 );

INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ('Teddy', 23, 'Norway', 20000.00 );

INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'Mark', 25, 'Rich-Mond ', 65000.00 );

INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'David', 27, 'Texas', 85000.00 );

INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'Kim', 22, 'South-Hall', 45000.00 );

INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'James', 24, 'Houston', 10000.00 );

查看 COMPANY 表的记录如下:

 id | name  | age | address    | salary
----+-------+-----+------------+--------
  1 | Paul  |  32 | California |  20000
  2 | Allen |  25 | Texas      |  15000
  3 | Teddy |  23 | Norway     |  20000
  4 | Mark  |  25 | Rich-Mond  |  65000
  5 | David |  27 | Texas      |  85000
  6 | Kim   |  22 | South-Hall |  45000
  7 | James |  24 | Houston    |  10000

Captcha Code

0 笔记

分享笔记

Inline Feedbacks
View all notes