0
0
SQLquery~5 mins

INSERT and auto-generated keys in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SQL INSERT statement do?
The INSERT statement adds new rows (records) into a database table.
Click to reveal answer
beginner
What is an auto-generated key in a database?
An auto-generated key is a unique value created automatically by the database, often used as a primary key to identify each row uniquely.
Click to reveal answer
intermediate
How do you insert a row and get the auto-generated key in SQL?
You use INSERT to add the row, then use RETURNING (PostgreSQL) or LAST_INSERT_ID() (MySQL) to get the new key.
Click to reveal answer
beginner
Why are auto-generated keys useful?
They ensure each row has a unique ID without manual input, making it easier to reference and manage data.
Click to reveal answer
beginner
Write a simple SQL INSERT statement for a table 'users' with columns 'name' and 'email'.
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
Click to reveal answer
What SQL clause can you use in PostgreSQL to get the auto-generated key after an INSERT?
ALAST_INSERT_ID()
BRETURNING
CFETCH
DOUTPUT
Which SQL function retrieves the last auto-generated ID in MySQL?
ALAST_INSERT_ID()
BGET_ID()
CRETURNING
DIDENTITY()
What happens if you try to insert a row without specifying a value for an auto-generated key column?
AThe database generates a unique key automatically
BThe insert fails with an error
CThe key is set to NULL
DThe key is set to zero
Which of these is NOT a reason to use auto-generated keys?
ATo avoid key duplication errors
BTo ensure unique row identification
CTo simplify referencing rows
DTo manually assign IDs for each row
What is the correct syntax to insert a new user named 'Bob' with email 'bob@example.com' into a 'users' table?
AADD INTO users (name, email) VALUES ('Bob', 'bob@example.com');
BINSERT users VALUES ('Bob', 'bob@example.com');
CINSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com');
DINSERT INTO users VALUES ('Bob', 'bob@example.com');
Explain how to insert a new row into a table and retrieve its auto-generated key.
Think about how databases create unique IDs automatically and how you can get that ID after adding data.
You got /4 concepts.
    Why are auto-generated keys important in database tables? Give an example.
    Consider how you would keep track of many records without repeating or missing IDs.
    You got /4 concepts.