Home > Database > Mysql Tutorial > body text

What is the return value of mysql insert?

青灯夜游
Release: 2023-04-11 18:06:01
Original
4993 people have browsed it

In mysql, the return value of the insert statement is the ID of the new data. The INSERT statement is used to insert one or more rows of tuple data into an existing table in the database. The syntax is "INSERT INTO table(column1,column2...) VALUES (value1,value2,...);"; "INSERT INTO" The parentheses after the clause specify the table name and a comma-separated list of columns.

What is the return value of mysql insert?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

The return value of insert is the ID of the new data. Of course, the premise is that the database supports auto-incrementing ID primary keys; if no auto-incrementing primary key is defined, a special number will be returned.

MySQL INSERT: Insert data (add data)

After the database and table are successfully created, data needs to be inserted into the database table. In MySQL, you can use the INSERT statement to insert one or more rows of tuple data into an existing table in the database.

1. Simple MySQL INSERT statement

MySQL INSERT statement is used to insert one or more rows into a table middle. The syntax of the INSERT statement is illustrated below:

INSERT INTO table(column1,column2...)
VALUES (value1,value2,...);
Copy after login

First , after the INSERT INTO clause, specify the table name within parentheses and separate it with commas List of columns.
Then , place the comma-separated values ​​of the corresponding columns in parentheses after the VALUES keyword.

Before executing the insert statement, you need to have the INSERT permission to execute the INSERT statement.

Let us create a new table named tasks to practice the INSERT statement, refer to the following create statement-

USE testdb;

CREATE TABLE IF NOT EXISTS tasks (
    task_id INT(11) AUTO_INCREMENT,
    subject VARCHAR(45) DEFAULT NULL,
    start_date DATE DEFAULT NULL,
    end_date DATE DEFAULT NULL,
    description VARCHAR(200) DEFAULT NULL,
    PRIMARY KEY (task_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

For example, if you want to To insert a task into the tasts table, use the INSERT statement as follows:

INSERT INTO tasks(subject,start_date,end_date,description)
VALUES('Learn MySQL INSERT','2017-07-21','2017-07-22','Start learning..');
Copy after login

After executing this statement, MySQL returns a message to notify the number of affected rows. In this case, one row is affected.

Now use the following statement to query the data in tasks, as shown below-

SELECT * FROM tasks;
Copy after login

Execute the above query statement and get the following results-

+---------+--------------------+------------+------------+------------------+
| task_id | subject            | start_date | end_date   | description      |
+---------+--------------------+------------+------------+------------------+
|       1 | Learn MySQL INSERT | 2017-07-21 | 2017-07-22 | Start learning.. |
+---------+--------------------+------------+------------+------------------+
1 row in set
Copy after login

2. MySQL INSERT - Insert multiple rows

If you want to insert multiple rows into the table at one time, you can use the INSERT statement with the following syntax:

INSERT INTO table(column1,column2...)
VALUES (value1,value2,...),
       (value1,value2,...),
...;
Copy after login

In this form, the list of values ​​for each row is separated by commas. For example, to insert multiple rows into the tasks table, use the following statement:

INSERT INTO tasks(subject,start_date,end_date,description)
VALUES ('任务-1','2017-01-01','2017-01-02','Description 1'),
       ('任务-2','2017-01-01','2017-01-02','Description 2'),
       ('任务-3','2017-01-01','2017-01-02','Description 3');
Copy after login

After executing the above statement, return -

Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0
Copy after login

Now query tasks The data in the table is as follows-

select * from tasks;
Copy after login

Execute the above query statement and get the following results-

+---------+--------------------+------------+------------+------------------+
| task_id | subject            | start_date | end_date   | description      |
+---------+--------------------+------------+------------+------------------+
|       1 | Learn MySQL INSERT | 2017-07-21 | 2017-07-22 | Start learning.. |
|       2 | 任务-1             | 2017-01-01 | 2017-01-02 | Description 1    |
|       3 | 任务-2             | 2017-01-01 | 2017-01-02 | Description 2    |
|       4 | 任务-3             | 2017-01-01 | 2017-01-02 | Description 3    |
+---------+--------------------+------------+------------+------------------+
4 rows in set
Copy after login

If you specify the values ​​of the corresponding columns for all columns in the table, you can Ignore the column list in the INSERT statement like this:

INSERT INTO table
VALUES (value1,value2,...);
Copy after login

or -

INSERT INTO table
VALUES (value1,value2,...),
       (value1,value2,...),
...;
Copy after login

Note that it is not necessary to auto-increment the column (e.g. taskid column) specify the value because MySQL automatically generates values ​​for auto-incrementing columns.

3. MySQL INSERT with SELECT clause

In MySQL, you can use the columns and values ​​returned by the SELECT statement to Populate the value of the INSERT statement. This feature is very convenient because you can copy the table completely or partially using the INSERT and SELECT clauses like this:

INSERT INTO table_1
SELECT c1, c2, FROM table_2;
Copy after login

Suppose you want to copy the tasks The table is copied to the tasks_bak table.

First , create a new table named tasks_bak by copying the structure of the tasks table, as shown below:

CREATE TABLE tasks_bak LIKE tasks;
Copy after login

The second step, use the following INSERT statement to insert the data in the tasks table into the tasks_bak table:

INSERT INTO tasks_bak
SELECT * FROM tasks;
Copy after login

The third step, check the data in the tasks_bak table to see if the copy from the tasks table is actually completed.

mysql> select * from tasks;
+---------+--------------------+------------+------------+------------------+
| task_id | subject            | start_date | end_date   | description      |
+---------+--------------------+------------+------------+------------------+
|       1 | Learn MySQL INSERT | 2017-07-21 | 2017-07-22 | Start learning.. |
|       2 | 任务-1             | 2017-01-01 | 2017-01-02 | Description 1    |
|       3 | 任务-2             | 2017-01-01 | 2017-01-02 | Description 2    |
|       4 | 任务-3             | 2017-01-01 | 2017-01-02 | Description 3    |
+---------+--------------------+------------+------------+------------------+
4 rows in set
Copy after login

4. MySQL INSERT and ON DUPLICATE KEY UPDATE

If the new row violates the primary key (PRIMARY KEY) or UNIQUEConstraint, MySQL will cause an error. For example, if you execute the following statement:

INSERT INTO tasks(task_id,subject,start_date,end_date,description)
VALUES (4,'Test ON DUPLICATE KEY UPDATE','2017-01-01','2017-01-02','Next Priority');
Copy after login

MySQL will be unhappy and throw you an error message:

Error Code: 1062. Duplicate entry '4' for key 'PRIMARY' 0.016 sec
Copy after login

Because the primary key task_id column in the table already exists There is a row with a value of 4, so the statement violates the PRIMARY KEY constraint.

However, if you specify the ON DUPLICATE KEY UPDATE option in the INSERT statement, MySQL will insert a new row or update the original row record with a new value.

For example, the following statement updates the row where task_id is 4 with the new task_id and subject.

INSERT INTO tasks(task_id,subject,start_date,end_date,description)
VALUES (4,'Test ON DUPLICATE KEY UPDATE','2017-01-01','2017-01-02','Next Priority')
ON DUPLICATE KEY UPDATE 
   task_id = task_id + 1, 
   subject = 'Test ON DUPLICATE KEY UPDATE';
Copy after login

After executing the above statement, MySQL sends a message saying that row 2 is affected. Now, let's take a look at the data in the tasks table:

mysql> select * from tasks;
+---------+------------------------------+------------+------------+------------------+
| task_id | subject                      | start_date | end_date   | description      |
+---------+------------------------------+------------+------------+------------------+
|       1 | Learn MySQL INSERT           | 2017-07-21 | 2017-07-22 | Start learning.. |
|       2 | 任务-1                       | 2017-01-01 | 2017-01-02 | Description 1    |
|       3 | 任务-2                       | 2017-01-01 | 2017-01-02 | Description 2    |
|       5 | Test ON DUPLICATE KEY UPDATE | 2017-01-01 | 2017-01-02 | Description 3    |
+---------+------------------------------+------------+------------+------------------+
4 rows in set
Copy after login

The new row was not inserted, but the task_id value was updated to 4 OK. The above INSERT ON DUPLICATE KEY UPDATE statement is equivalent to the following UPDATE statement:

UPDATE tasks 
SET 
    task_id = task_id + 1,
    subject = 'Test ON DUPLICATE KEY UPDATE'
WHERE
    task_id = 4;
Copy after login

【相关推荐:mysql视频教程

The above is the detailed content of What is the return value of mysql insert?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template