Create Data (INSERT)
The Create operation in CRUD is performed using the INSERT INTO statement in SQL. It is used to add new records (rows) to an existing table.
Database Preparation
Run this block to set up the Authors table.
DROP TABLE IF EXISTS Authors;
CREATE TABLE Authors (
AuthorID INTEGER PRIMARY KEY,
AuthorName VARCHAR(100),
Nationality VARCHAR(50)
);
INSERT INTO Authors (AuthorID, AuthorName, Nationality)
VALUES (101, 'J.R.R. Tolkien', 'British'),
(102, 'George Orwell', 'British');
SELECT * FROM Authors;
INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
Syntax
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
If you are adding values for all the columns of the table in the correct order, you do not need to specify the column names in the SQL query.
Example
Operation: Add a new author to the Authors table.
Authors Table Before:
| AuthorID | AuthorName | Nationality |
|---|---|---|
| 101 | J.R.R. Tolkien | British |
| 102 | George Orwell | British |
| Query: |
INSERT INTO Authors (AuthorID, AuthorName, Nationality)
VALUES (103, 'Frank Herbert', 'American');
-- Check the result
SELECT * FROM Authors;
Authors Table After:
| AuthorID | AuthorName | Nationality |
|---|---|---|
| 101 | J.R.R. Tolkien | British |
| 102 | George Orwell | British |
| 103 | Frank Herbert | American |
Hands-on Exercise
Task: Insert a new author named 'Isaac Asimov' from 'American' with AuthorID 104.
Your Query:
-- Write your query here
Expected Results:
| AuthorID | AuthorName | Nationality |
|---|---|---|
| 101 | J.R.R. Tolkien | British |
| 102 | George Orwell | British |
| 103 | Frank Herbert | American |
| 104 | Isaac Asimov | American |