Home  >  Article  >  Database  >  Mysql insert statement method

Mysql insert statement method

hzc
hzcOriginal
2020-06-12 14:42:576686browse

Mysql insert statement method

Mysql insert statement method:

Three commonly used statements for inserting data in mysql:

insert into means inserting data, the database will check the primary key (PrimaryKey), and an error will be reported if there is a duplication;

replace into means inserting replacement data, if there is a PrimaryKey or unique index in the demand table, If data already exists in the database, replace it with new data. If there is no data, the effect is the same as insert into;

The REPLACE statement will return a number to indicate the number of affected rows. This number is the sum of the number of deleted and inserted rows. If this number is 1 for a single-row REPLACE, then one row is inserted and no rows are deleted. If this number is greater than 1, one or more old rows are deleted before a new row is inserted. If a table contains multiple unique indexes and the new row copies the values ​​of different old rows in different unique indexes, it is possible that a single row replaces multiple old rows.

insert ignore means that if the same record already exists, the current new data will be ignored;

下面通过代码说明之间的区别,如下:
create table testtb(
id int not null primary key,
name varchar(50),
age int
);
insert into testtb(id,name,age)values(1,"bb",13);
select * from testtb;
insert ignore into testtb(id,name,age)values(1,"aa",13);
select * from testtb;//仍是1,“bb”,13,因为id是主键,出现主键重复但使用了ignore,则错误被忽略
replace into testtb(id,name,age)values(1,"aa",12);
select * from testtb; //数据变为1,"aa",12

Recommended tutorial: "mysql tutorial

The above is the detailed content of Mysql insert statement method. 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