AUTO_INCREMENT automatically generates a unique number for a column when a new row is inserted, usually used for primary keys.
Click to reveal answer
beginner
What is SERIAL in SQL?
SERIAL is a shorthand in PostgreSQL that creates an integer column and automatically sets up a sequence to generate unique numbers for new rows.
Click to reveal answer
beginner
What is IDENTITY in SQL?
IDENTITY is a SQL standard feature that defines a column to auto-generate unique numbers, often used in SQL Server and newer versions of other databases.
Click to reveal answer
intermediate
How does AUTO_INCREMENT differ from SERIAL?
AUTO_INCREMENT is used mainly in MySQL and automatically increments a number, while SERIAL in PostgreSQL creates a sequence object behind the scenes to generate numbers.
Click to reveal answer
intermediate
Why might you choose IDENTITY over AUTO_INCREMENT or SERIAL?
IDENTITY follows the SQL standard and offers more control over number generation, making it more portable across different database systems.
Click to reveal answer
Which SQL feature automatically creates a sequence object in PostgreSQL?
AAUTO_INCREMENT
BSERIAL
CIDENTITY
DPRIMARY KEY
✗ Incorrect
SERIAL in PostgreSQL creates a sequence object to generate unique numbers automatically.
In which database is AUTO_INCREMENT commonly used?
AMySQL
BSQL Server
CPostgreSQL
DOracle
✗ Incorrect
AUTO_INCREMENT is commonly used in MySQL to auto-generate unique numbers.
What does the IDENTITY property do in SQL?
ACreates a foreign key
BCreates a view
CGenerates unique numbers automatically
DDefines a text column
✗ Incorrect
IDENTITY defines a column to auto-generate unique numbers, often for primary keys.
Which of these is a SQL standard feature for auto-numbering?
ASEQUENCE
BSERIAL
CAUTO_INCREMENT
DIDENTITY
✗ Incorrect
IDENTITY is part of the SQL standard for auto-generating unique numbers.
Which feature is NOT typically used for auto-incrementing primary keys?
AVARCHAR
BSERIAL
CIDENTITY
DAUTO_INCREMENT
✗ Incorrect
VARCHAR is a text data type and does not auto-increment numbers.
Explain the differences between AUTO_INCREMENT, SERIAL, and IDENTITY in SQL.
Think about which database uses which feature and how they generate numbers.
You got /4 concepts.
Why is IDENTITY considered more portable than AUTO_INCREMENT or SERIAL?
Consider standards and cross-database compatibility.
You got /3 concepts.
Practice
(1/5)
1. Which keyword is used in MySQL to automatically generate unique numbers for a primary key column?
easy
A. IDENTITY
B. SERIAL
C. SEQUENCE
D. AUTO_INCREMENT
Solution
Step 1: Understand MySQL auto-numbering
MySQL uses AUTO_INCREMENT to generate unique numbers automatically for columns like primary keys.
Step 2: Compare with other keywords
SERIAL is PostgreSQL-specific, IDENTITY is SQL standard used in SQL Server and newer PostgreSQL versions, and SEQUENCE is a separate object, not a column keyword.
Final Answer:
AUTO_INCREMENT -> Option D
Quick Check:
MySQL auto-number = AUTO_INCREMENT [OK]
Hint: MySQL uses AUTO_INCREMENT for auto numbering [OK]
Common Mistakes:
Confusing SERIAL with MySQL syntax
Using IDENTITY in MySQL
Thinking SEQUENCE is a column keyword
2. Which of the following is the correct syntax to create an auto-incrementing primary key column in PostgreSQL using the SERIAL keyword?
easy
A. CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50));
B. CREATE TABLE users (id AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
C. CREATE TABLE users (id IDENTITY PRIMARY KEY, name VARCHAR(50));
D. CREATE TABLE users (id SEQUENCE PRIMARY KEY, name VARCHAR(50));
Solution
Step 1: Identify PostgreSQL SERIAL syntax
In PostgreSQL, SERIAL is used as a shorthand to create an integer column that auto-increments.
Step 2: Check syntax correctness
CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50)); uses id SERIAL PRIMARY KEY, which is valid. Options B and C use MySQL and SQL Server syntax incorrectly in PostgreSQL. CREATE TABLE users (id SEQUENCE PRIMARY KEY, name VARCHAR(50)); uses SEQUENCE incorrectly as a column type.
Final Answer:
CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50)); -> Option A
Quick Check:
SERIAL syntax in PostgreSQL = CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50)); [OK]
Hint: PostgreSQL uses SERIAL for auto-increment columns [OK]
Common Mistakes:
Using AUTO_INCREMENT in PostgreSQL
Using IDENTITY without parentheses
Treating SEQUENCE as a column type
3. Given the following SQL Server table creation statement, what will be the output of inserting two rows without specifying the ID column?
CREATE TABLE products (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(20)); INSERT INTO products (name) VALUES ('Pen'); INSERT INTO products (name) VALUES ('Pencil'); SELECT * FROM products;
medium
A. [{"id": null, "name": "Pen"}, {"id": null, "name": "Pencil"}]
B. [{"id": 0, "name": "Pen"}, {"id": 1, "name": "Pencil"}]
C. [{"id": 1, "name": "Pen"}, {"id": 2, "name": "Pencil"}]
D. Syntax error on INSERT statements
Solution
Step 1: Understand IDENTITY behavior in SQL Server
The IDENTITY(1,1) means the ID column starts at 1 and increments by 1 automatically for each new row.
Step 2: Predict inserted values
Inserting two rows without specifying ID will assign IDs 1 and 2 respectively. The SELECT will show these IDs with names.