Postgresql Autoincrement
# PostgreSQL AUTO INCREMENT
AUTO INCREMENT generates a unique number when a new record is inserted into the table.
PostgreSQL uses sequences to identify the auto-incrementing of fields. The data types are smallserial, serial, and bigserial. These attributes are similar to the AUTO_INCREMENT attribute supported by MySQL database.
The statement to set auto-increment using MySQL is as follows:
```sql
CREATE TABLE IF NOT EXISTS `tutorial_tbl`(
`tutorial_id` INT UNSIGNED AUTO_INCREMENT,
`tutorial_title` VARCHAR(100) NOT NULL,
`tutorial_author` VARCHAR(40) NOT NULL,
`submission_date` DATE,
PRIMARY KEY ( `tutorial_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
MySQL uses the AUTO_INCREMENT attribute to identify the auto-incrementing of fields.
PostgreSQL uses sequences to identify the auto-incrementing of fields:
```sql
CREATE TABLE tutorial (
id serial NOT NULL,
alttext text,
imgurl text
)
SMALLSERIAL, SERIAL, and BIGSERIAL ranges:
| Pseudo Type | Storage Size | Range |
| --- | --- | --- |
| `SMALLSERIAL` | 2 bytes | 1 to 32,767 |
| `SERIAL` | 4 bytes | 1 to 2,147,483,647 |
| `BIGSERIAL` | 8 bytes | 1 to 9,223,372,036,854,775,807 |
YouTip