Skip to content
On this page

Inserting Data

To add a new record to a database table, the INSERT statement is used.

Basic Syntax

The basic syntax for the INSERT statement is:

sql
INSERT INTO <table_name> (column1, column2, ...)
VALUES (value1, value2, ...);

Example

To insert a new record into the students table, specify the column names followed by the corresponding values in the VALUES clause:

sql
-- Add a new record:
INSERT INTO students (class_id, name, gender, score)
VALUES (2, '大牛', 'M', 80);

To check the result, you can run:

sql
SELECT * FROM students;

Notes

  • You do not need to specify the id column if it is an auto-incrementing primary key, as its value will be automatically generated by the database.
  • If a column has a default value, you can omit it in the INSERT statement.

The order of columns in the INSERT statement does not have to match the order in the table, but the values must correspond to the specified columns. For example:

sql
INSERT INTO students (score, gender, name, class_id)
VALUES (80, 'M', '大牛', 2);

Inserting Multiple Records

You can also insert multiple records at once by specifying multiple sets of values in the VALUES clause, separated by commas:

sql
-- Insert multiple new records:
INSERT INTO students (class_id, name, gender, score) VALUES
  (1, '大宝', 'M', 87),
  (2, '二宝', 'M', 81),
  (3, '三宝', 'M', 83);

To check the results:

sql
SELECT * FROM students;

Summary

Using INSERT, you can add one or more records to a table efficiently.

Inserting Data has loaded