The basic syntax of the insert statement "INSERT INTO table name (column 1, column 2, column 3, ...), VALUES (value 1, value 2, value 3, ...);", " "Table name" is the name of the table into which data is to be inserted, "Column 1", "Column 2", "Column 3", etc. are the names of the columns in the table into which data is to be inserted, "Value 1", "Value 2", "Value 3" etc. are the data values to be inserted.
The Insert statement is a statement in SQL used to insert one or more rows of data into a database table. It can insert data into specified columns of the table or replace existing data in the table. The following is the basic syntax of the Insert statement:
INSERT INTO 表名 (列 1, 列 2, 列 3, ...) VALUES (值 1, 值 2, 值 3, ...);
Where:
- `Table name`: The name of the table into which data is to be inserted.
- `Column 1`, `Column 2`, `Column 3`, etc.: The names of the columns in the table into which data is to be inserted.
- `Value 1`, `Value 2`, `Value 3`, etc.: The data value to be inserted.
The following are some examples of using the Insert statement:
1. Insert a row of data:
Suppose there is a table named `students`, which contains `id`, There are three columns: `name` and `age`. To insert a row of data into this table, you can use the following statement:
INSERT INTO students (id, name, age) VALUES (1, 'Alice', 20);
This will insert a row of data in the `students` table, where `id` is 1, `name` is 'Alice', `age` for 20.
2. Insert multiple rows of data:
To insert multiple rows of data at one time, you can use the following statement:
INSERT INTO students (id, name, age) VALUES (1, 'Alice', 20), (2, 'Bob', 22), (3, 'Charlie', 23);
This will insert in the `students` table Three rows of data.
3. Insert data and calculate the value of the new column:
Suppose there is a table named `orders`, which contains three columns: `id`, `customer_id` and `total_amount` . To insert a row of data into the table and calculate the new value of the `total_amount` column, you can use the following statement:
INSERT INTO orders (id, customer_id, total_amount) VALUES (1, 1001, 500 + 300);
This will insert a row of data in the `orders` table, where `id` is 1,` customer_id` is 1001 and `total_amount` is 800.
4. Replace the data in the table:
To replace the data in the table, you can use the Insert statement and compare the value in the `VALUES` clause with the existing value in the table Compare. For example, suppose there is a table named `students`, which contains three columns: `id`, `name`, and `age`. To replace a row of data in the table, you can use the following statement:
INSERT INTO students (id, name, age) VALUES (1, 'Alice', 20) WHERE id = 1;
This will replace the row with `id` as 1 in the `students` table, where `name` is 'Alice' and `age` is 20.
Note: When executing the Insert statement, if a row with the same primary key value already exists in the table, a conflict will occur. In this case, you can use the `ON DUPLICATE KEY UPDATE` clause to specify how to handle the conflict. For example, to insert rows with the same `id`, and update the value of the `age` column, you can use the following statement:
INSERT INTO students (id, name, age) VALUES (1, 'Alice', 20) ON DUPLICATE KEY UPDATE age = age + 1;
This will insert a row of data in the `students` table, where `id` is 1. `name` is 'Alice' and `age` is 20. If a conflict occurs, the value of the `age` column is updated so that it is increased by 1.
The above is the detailed content of insert statement. For more information, please follow other related articles on the PHP Chinese website!