Home Common Problem insert statement

insert statement

Sep 15, 2023 pm 01:30 PM
insert

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.

insert statement

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:

1

2

INSERT  INTO  表名  (列  1,  列  2,  列  3,  ...)  

VALUES  (值  1,  值  2,  值  3,  ...);

Copy after login

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:

1

2

INSERT  INTO  students  (id,  name,  age)  

VALUES  (1,  'Alice',  20);

Copy after login

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:

1

2

3

4

INSERT  INTO  students  (id,  name,  age)  

VALUES  (1,  'Alice',  20),  

         (2,  'Bob',  22),  

         (3,  'Charlie',  23);

Copy after login

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:

1

2

INSERT  INTO  orders  (id,  customer_id,  total_amount)  

VALUES  (1,  1001,  500  +  300);

Copy after login

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:

1

2

3

INSERT  INTO  students  (id,  name,  age)  

VALUES  (1,  'Alice',  20)  

WHERE  id  =  1;

Copy after login

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:

1

2

3

INSERT  INTO  students  (id,  name,  age)  

VALUES  (1,  'Alice',  20)  

ON  DUPLICATE  KEY  UPDATE  age  =  age  +  1;

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)