Introduction
We use INSERT to add new data into a table. Auto-generated keys help create unique IDs automatically for each new row, so we don't have to make them ourselves.
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); -- To get the auto-generated key (example in PostgreSQL): INSERT INTO table_name (column1, column2) VALUES (value1, value2) RETURNING id;
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com') RETURNING id;
INSERT INTO products (product_name, price) VALUES ('Pen', 1.5);
CREATE TABLE customers ( customer_id SERIAL PRIMARY KEY, name VARCHAR(50), email VARCHAR(50) ); INSERT INTO customers (name, email) VALUES ('John Doe', 'john@example.com') RETURNING customer_id;
INSERT statement do in SQL when adding a new row to a table with an auto-generated key?users with columns name and email, letting the database auto-generate the id?orders with columns order_id (auto-generated), product, and quantity, what will this query return?INSERT INTO orders (product, quantity) VALUES ('Book', 3) RETURNING order_id;id is an auto-generated key?INSERT INTO customers (id, name) VALUES (NULL, 'John');
products table with an auto-generated product_id and get all new IDs immediately. Which SQL approach is best?Pass the test and this pattern moves unknown