Home >Database >Mysql Tutorial >How to insert data in mysql

How to insert data in mysql

下次还敢
下次还敢Original
2024-04-05 16:45:22525browse

MySQL data insertion is the operation of adding new records to the database. The syntax is: INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN); Steps Includes: 1. Specify the target table name; 2. List the columns to be inserted; 3. Provide column values. For example, the following query will insert a record containing the following information into the customer table: INSERT INTO customer (id, name, email) VALUES (5, 'John D

How to insert data in mysql

MySQL data insertion

Inserting data is one of the most important operations in MySQL, which allows developers to add new records to the database.

Syntax

<code class="sql">INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN);</code>

Steps

  1. Specify the table name: table_name is the target table into which you want to insert data.
  2. List the columns to be inserted: column1, column2, ..., columnN is the column you want to insert Provide the column of values.
  3. Provide column values: value1, value2, ..., valueN is you The values ​​to insert. The values ​​must be compatible with the data type of the specified column.

Example

The following query will insert a new record containing the following information To the customer table:

  • ID: 5
  • Name: John Doe
  • Email: john.doe@example.com
<code class="sql">INSERT INTO customer (id, name, email) VALUES (5, 'John Doe', 'john.doe@example.com');</code>

Other Notes

  • If you do not specify a column name, MySQL will insert values ​​in the order in which the table is defined.
  • You can use the LAST_INSERT_ID() function to get the ID of the recently inserted record.
  • If you want to insert multiple records, you can use the multi-value insertion syntax:
<code class="sql">INSERT INTO customer (id, name, email) VALUES
(5, 'John Doe', 'john.doe@example.com'),
(6, 'Jane Smith', 'jane.smith@example.com'),
(7, 'Mark Jones', 'mark.jones@example.com');</code>

The above is the detailed content of How to insert data in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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