The usage of the super-simple method of updating data in MYSQL, replace into, and the routine additions, deletions, and changes

When I was programming today, I learned the usage of replace into, which is really easy to use. It is an enhanced version of insert into. When inserting data into a table, we often encounter such situations: 1. First determine whether the data exists; 2. If it does not exist, insert it; 3. If it exists, update it.

This can be done in SQL  Server like this:

if not exists (select 1 from t where id = 1)?
insert into t(id, update_time) values(1, getdate())
else
update t set update_time = getdate() where id = 1

So how to implement such logic in MySQL? There is an easier way in MySQL: replace into

replace into t(id, update_time) values(1, now());

or

replace into t(id, update_time) select 1, now();

The function of replace into is similar to insert, the difference is: replace into first tries to insert data into the table, 1. If it is found that there is already this row of data in the table (judged by the primary key or unique index), delete this row of data first, and then insert a new row of data. The data. 2. Otherwise, insert new data directly.

It should be noted that the table into which data is inserted must have a primary key or a unique index! Otherwise, replace into will insert data directly, which will result in duplicate data in the table.


MySQL replace into has three forms:

1. replace into tbl_name(col_name, ...) values(...)

2. replace into tbl_name(col_name, ...) select ...

3. replace into tbl_name set col_name=value, ...

The first form is similar to the usage of insert into,

The second usage of replace select is also similar to insert select. This usage does not necessarily require column names to match. In fact, MYSQL doesn't even care about the column names returned by select, it needs the position of the column. For example, replace into tb1( name, title, mood) select rname, rtitle, rmood from tb2;? This example uses replace into to import all data from ?tb2 into tb1.

The third use of replace set is similar to the use of update set, using an assignment such as "SET col_name = col_name + 1", the reference to the column name on the right is treated as DEFAULT(col_name). Therefore, the assignment is equivalent to SET col_name = DEFAULT(col_name) + 1.

The first two forms are used more. The "into" keyword can be omitted, but it is better to add "into" to make the meaning more intuitive. In addition, for those columns that are not given a value, MySQL will automatically assign a default value to these columns.


Here is a simple case of my code:




#Find out the id greater than 1030 in the ceshi table

select * from ceshi where id>1030;


#Find the data of the flightDate column in the ceshi table as 2017-12-28 and the arr column as beijing

SELECT * FROM ceshi where UNIX_TIMESTAMP(flightDate)=UNIX_TIMESTAMP('2017-12-28') and arr='beijing';


# The value of official_price with id greater than 1044 in the ceshi table is changed to 10;

update ceshi set official_price='10' where id>1044;


#删除ceshi表中id=3的那一行

DELETE FROM ceshi WHERE id=3;


#将 ceshi表中获取 author 字段中以 COM 为结尾的的所有记录

SELECT * from ceshi  WHERE author LIKE '%COM';

      

#删除ceshi表中id字段

ALTER TABLE ceshi DROP id;


#增加ceshi表中age字段,并定义数据类型

ALTER TABLE ceshi ADD age INT;


#查找name字段中以'st'为开头的所有数据:

SELECT name FROM ceshi WHERE name REGEXP '^st';


#查找name字段中以'ok'为结尾的所有数据:

SELECT name FROM ceshi WHERE name REGEXP 'ok$';


#查找name字段中包含'mar'字符串的所有数据:

SELECT name FROM ceshi WHERE name REGEXP 'mar';


#查找name字段中以元音字符开头或以'ok'字符串结尾的所有数据:

SELECT name FROM ceshi WHERE name REGEXP '^[aeiou]|ok$';


#将 ceshi 表中获取 runoob_author 字段中以 COM 为结尾的的所有记录取出

SELECT * from ceshi  WHERE runoob_author LIKE '%COM';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325637771&siteId=291194637