0
0
MySQLquery~5 mins

INSERT INTO single row in MySQL

Choose your learning style9 modes available
Introduction

We use INSERT INTO to add new information into a table, one row at a time. It helps us save new data in the database.

Adding a new customer to a customer list.
Saving a new order placed by a buyer.
Recording a new employee's details in the company database.
Entering a new product into the inventory.
Logging a new event or transaction.
Syntax
MySQL
INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3);

Make sure the values match the order and type of the columns.

If you insert all columns, you can skip the column list.

Examples
Adds one user named Alice, age 30, living in New York.
MySQL
INSERT INTO users (name, age, city) VALUES ('Alice', 30, 'New York');
Adds a product with id 101, name Pen, price 1.5 assuming the table has exactly these columns in order.
MySQL
INSERT INTO products VALUES (101, 'Pen', 1.5);
Adds an employee with id 5 and name John. Other columns get default or NULL values.
MySQL
INSERT INTO employees (id, name) VALUES (5, 'John');
Sample Program

This creates a table named books, inserts one book row, then shows all rows in the table.

MySQL
CREATE TABLE books (id INT, title VARCHAR(50), author VARCHAR(50));
INSERT INTO books (id, title, author) VALUES (1, '1984', 'George Orwell');
SELECT * FROM books;
OutputSuccess
Important Notes

If you forget a column that does not allow NULL or has no default, the insert will fail.

Text values must be in single quotes.

Use SELECT to check if your row was added.

Summary

INSERT INTO adds one new row to a table.

Specify columns and matching values inside parentheses.

Always check your data types and quotes for text.