SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The INSERT INTO
statement is used to insert new records into a table in SQL.
There are two main ways you can use the INSERT INTO
statement:
Inserting Full Rows:
You can specify both the column names and the values for the new row.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
For example, if you have a table named students
with columns id
, first_name
, and last_name
, you can insert a new record like this:
INSERT INTO students (id, first_name, last_name) VALUES (1, 'John', 'Doe');
Inserting Specific Columns:
If you only want to insert data into specific columns, you can do so by specifying just those columns. The other columns will get their default values (or NULL if no default is specified).
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Continuing with the students
table example:
INSERT INTO students (first_name) VALUES ('Jane');
If you omit the id
and it is an auto-incrementing primary key, the database will automatically assign a value for you. If last_name
has a default value or can accept NULL, then it will be filled accordingly.
Inserting Multiple Rows:
In many modern SQL databases, you can insert multiple rows in a single INSERT
statement:
INSERT INTO table_name (column1, column2) VALUES (value1a, value2a), (value1b, value2b), ...;
For our students
table:
INSERT INTO students (first_name, last_name) VALUES ('Jane', 'Smith'), ('Alice', 'Johnson');
Remember to always be cautious when using the INSERT
statement to ensure you're adding the correct data to the correct table and columns. Always make backups and test on a sample database when possible.
INSERT INTO
is used to add new rows to a table.INSERT INTO employees (employee_id, employee_name, salary) VALUES (1, 'John Doe', 50000);
INSERT INTO products (product_id, product_name, price) VALUES (101, 'Widget', 19.99);
INSERT INTO customers (customer_id, customer_name) VALUES (1, 'John Doe'), (2, 'Jane Doe');
INSERT INTO SELECT
.INSERT INTO archive_customers (customer_id, customer_name) SELECT customer_id, customer_name FROM customers WHERE registration_date < '2022-01-01';
INSERT INTO
statement.INSERT INTO orders (order_id, product_id) VALUES (1, 101);
INSERT INTO
can be used with a WHERE
clause to conditionally insert data.INSERT INTO employees (employee_id, employee_name, salary) SELECT user_id, user_name, salary FROM temp_users WHERE salary > 50000;
INSERT INTO customers (customer_id, customer_name, email) VALUES (1, 'John Doe', NULL);
INSERT INTO new_customers SELECT * FROM old_customers;
INSERT INTO
for better performance.INSERT INTO transactions (transaction_id, amount) VALUES (1, 100), (2, 150), (3, 200), ... ;
INSERT INTO
statement to insert dynamic values.DECLARE @product_name VARCHAR(255) = 'New Widget'; INSERT INTO products (product_name) VALUES (@product_name);
INSERT IGNORE
or ON DUPLICATE KEY UPDATE
.INSERT IGNORE INTO products (product_id, product_name) VALUES (101, 'Widget');
INSERT INTO
statement.INSERT INTO audit_log (user_id, action, timestamp) SELECT user_id, 'LOGIN', CURRENT_TIMESTAMP FROM users WHERE username = 'john_doe';
INSERT INTO
should comply with table constraints, such as NOT NULL, UNIQUE, and foreign key constraints.INSERT INTO orders (order_id, product_id, quantity) VALUES (1, 101, 3);
INSERT INTO employees (employee_id, employee_name) VALUES (1, 'John Doe', 50000);