Jump to: navigation, search

Postgresql create auto-increment and sequence

From w3cyberlearnings

Contents

Postgresql auto-increment

  • In postgresql will not support auto-increment as in MySQL.
  • Use sequence to define the auto-increment in Postgresql.

Create Sequence

CREATE SEQUENCE mdl_shop_category_id_seq;

Create Table and Assign Sequence to the table column

create table mdl_shop_category(
	id INTEGER DEFAULT NEXTVAL('mdl_shop_category_id_seq'),
    	category VARCHAR(100) NOT NULL,
   	createdate timestamp,
	modifydate timestamp,
 	PRIMARY KEY(id)

);

The id in the mdl_shop_category will be automatically increment

insert into mdl_shop_category (category,createdate)
values('Science',CURRENT_TIMESTAMP);
Navigation
Web
SQL
MISC
References