CodeBlanks Logo Icon CodeBlanks

PRIMARY KEY

Define a primary key.

AUTO_INCREMENT

With AUTO_INCREMENT (MySQL) a column auto-fills with an increasing number when omitted. PostgreSQL uses SERIAL.

CREATE TABLE products (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100)
);

Id auto-increments; no manual value needed.

The table products is created.

AUTO_INCREMENT — details

PostgreSQL uses SERIAL or IDENTITY; SQL Server uses IDENTITY(1,1). An auto-increment column is usually the primary key.

Practice AUTO_INCREMENT

Create products with id INT AUTO_INCREMENT PRIMARY KEY and name VARCHAR(100).

Starter code

query.sql
-- create table
CREATE TABLE products

Sample solution

query.sql
CREATE TABLE products (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100)
)

Expected output

Tablo oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Auto increment

Question
CREATE TABLE products (
  id INT ____ PRIMARY KEY,
  name VARCHAR(100)
);
Answer
CREATE TABLE products (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100)
);

Explanation: AUTO_INCREMENT is the right answer.

2 PRIMARY KEY: match the terms

Term Meaning
AUTO_INCREMENT auto increment
SERIAL PostgreSQL
PRIMARY KEY primary
DEFAULT default

3 What is used for auto-increment in PostgreSQL?

  • SEQUENCE
  • AUTO_INCREMENT
  • IDENTITY
  • SERIAL Correct answer

Explanation: PostgreSQL uses SERIAL.

All lessons in Beginner