Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Statements commonly used in mysql to insert data

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

MySQL is a relational database management system in which relational databases store data in different tables instead of all data in one large warehouse, which increases speed and flexibility.

The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts the dual licensing policy, which is divided into community version and commercial version. Because of its small size, high speed and low total cost of ownership, especially open source, the development of small and medium-sized websites generally choose MySQL as the website database.

Three commonly used statements to insert data in mysql:

Insert into means to insert data, and the database will check the primary key (PrimaryKey). If there is a duplicate, an error will be reported.

Note: although the abbreviation of insert is very simple, the value after Values must correspond to the order of the classes in the table, and the type must be maintained all the time, even if a column in the table does not need a value, it must be assigned to null. For example, our primary key id sets increment without setting value, but in this way, it must be assigned to null.

Why it is not recommended: if you use this method to insert data in actual development, the following table has been changed (for example, the order of the fields has changed), then the whole statement will report an error, the scalability is very poor, and it is difficult to maintain.

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

The REPLACE statement returns a number indicating the number of rows affected. This number is the sum of the number of rows deleted and inserted. If the number is 1 for a single line REPLACE, one row is inserted and no row is deleted. If the number is greater than 1, one or more old rows are deleted before the new row is inserted. If the 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 indicates that if the same record already exists in the, the current new data is ignored.

The following code illustrates the differences as follows: 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 / / it is still 1, "bb", 13, because id is the primary key and the primary key is duplicated but ignore is used, the error is ignored replace into testtb (id,name,age) values (1, "aa", 12); select * from testtb; / / data becomes 1, "aa", 12

These are the details of the method of inserting statements in mysql. Please pay more attention to other related articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report