Home > Database > Mysql Tutorial > body text

How to add rows in mysql

青灯夜游
Release: 2022-05-23 20:47:47
Original
11716 people have browsed it

Mysql method to add rows: 1. Use the "INSERT INTO table name column name 1,...column name n VALUES (value 1..., value n);" statement, the order of column names and values ​​corresponds; 2 , use the "INSERT INTO table name SET column name 1 = value 1, column name 2 = value 2,...;" statement to insert a row of data.

How to add rows in mysql

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

In mysql, adding a row is to insert a row of data into the database table. The following article introduces two methods to you.

Method 1. Use INSERT...VALUES statement

The syntax format is:

INSERT INTO <表名> [ <列名1> [ , … <列名n>] ]
VALUES (值1 [… , 值n ]);
Copy after login

The syntax description is as follows:

  • Table name: Specify the name of the table to be operated on.

  • Column name: Specify the column name into which data needs to be inserted. If data is inserted into all columns in the table, all column names can be omitted and INSERT<table name>VALUES(…) can be used directly.

  • VALUES or VALUE clause: This clause contains the list of data to be inserted. The order of data in the data list should correspond to the order of columns.

Method 2: Use the INSERT...SET statement

The syntax format is:

INSERT INTO <表名>
SET <列名1> = <值1>,
        <列名2> = <值2>,
        …
Copy after login

This statement is used to directly enter the table Certain columns in specify corresponding column values, that is, the column name of the data to be inserted is specified in the SET clause, col_name is the specified column name, and the equal sign is followed by the specified data. For unspecified columns, the column value will be assigned as the default value for the column.

Example:

insert values: Advantages: Can be inserted in batches; Disadvantages: Single execution efficiency is low.

insert into table(col1,col2,col3) values(&#39;val1&#39;,&#39;val2&#39;,&#39;val3&#39;);
Copy after login

insert set: Advantages: High execution efficiency; Disadvantages: Only one piece of data can be inserted at a time.

insert into table set col1=&#39;val1&#39;,col2=&#39;val2&#39;,col3=&#39;val3&#39;;
Copy after login

How to add rows in mysql

[Related recommendations: mysql video tutorial]

The above is the detailed content of How to add rows in mysql. 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