statement according to the primary key is update table_name set column1 = val1, Column2 = Val2 [,......] WHERE id =? or using the Updatebyprimarykey interface provided by MyBatis, When we want to update a large amount of data at once, each SQL can only perform one update, and MySQL supports another
MySQL random query data I have discussed this issue in the group before. It is quite interesting. mysqll syntax is really fun. they all wanted to use PHP to implement random queries, but retrieving multiple entries seemed to require more than two queries. after reading the manual, find the following statement to complete the task.
CopyCode The Code is as follows: Select * From table_name order by rand () l
UPDATE c=c+1;After execution, the value of C becomes 4 (the second is repeated with the first one, and C is +1 on the original value).Copy the code code as follows:INSERT into TABLE (a,c) VALUES (1,3), (1,7) on DUPLICATE KEY UPDATE c=values (c);After execution, the value of C changes to 7 (the second is repeated with the first one, and C is the value of the duplicate 7 directly).Note: On DUPLICATE KEY
This is my study note for my old boy teacher's MySQL DBA Advanced OPS course, the old boy teacher spoke very well and thanked the old boy teacher very much. Just contact operations have a lot of knowledge, if I publish the article has incorrect place, please operation and maintenance industry elite, teacher timely guidance, hehe ~ I will also update their own learning Ysql DBA advanced operation and Mainten
;
Note the following when using MySQL database, update, and select to update table data:
For example, table test1A B12 B3 c
Table test2A B2 d3 e4 fA simple combination of update and select is intended to change columns B of Rows a = 2 and a = 2 in Table test1 to
' = ' C ' +1 WHERE ' a ' = 1;
If the row is inserted as a new record, the value of the affected row is 1, and if the existing record is updated, the value of the affected row is 2.
NOTE: If column B is also a unique column, insert is equivalent to this UPDATE statement:
The code is as follows
Copy Code
UPDATE ' table ' SET ' c ' = ' C ' +1 WHERE ' a ' =1 OR ' B ' =2
insertion with insert. If the returned value is greater than 2, then there are multiple unique indexes, and multiple records are deleted and inserted.
INSERT ... On DUPLICATE KEY UPDATE
INSERT ... On DUPLICATE KEY Update: When inserting data, if the value of the corresponding primary key or unique index in the inser
value must be assigned to at least one column. If a field uses a missing value (such as default value or auto-increment value), you can omit these fields in either of the two methods. If auto-increment is used in the ID field, the preceding two statements can be written as follows:
Insert into users (name, age) values ('Yao ming', 25 );
Insert into uses set name = 'Yao Ming ', age = 25;
MySQL has also made some changes in values. If nothing is writte
Tags: Specifies that implementations are shared with the use post where set OWA Example 1: In e-commerce, there is often a small inventory, the purchase of a very large number of people, how to ensure that the volume of goods will not be purchased multiple times. In fact, it is very simple to use the transaction +for update can be resolved. (for update only for
', Sleep (), 0) or ') Update: Update test set pass= ' Baidu ' or Updatexml (1,concat (0x7e, (version ()), 0x7e), 0) or ' WHERE id=2 and name= ' 0 '; Update test set pass= ' Baidu ' or Extractvalue (1,concat (0x7e,database ())) or ' WHERE id=2 and name= ' 0 '; Delete: DELETE from Test WHERE id=2 or Updatexml (1,concat (0x7e, (version ()), 0x7e), 0) or '; DELETE
Recently there is a business needs, multiple machines need to query data from a MySQL table at the same time and do follow-up business logic, in order to prevent more than one machine to get the same data, each machine needs to be in the acquisition of data to lock the data section, to ensure that many machines do not get the same data.
Our MySQL storage engine
For example, if column A is defined as unique and the value is 1, the following statement has the same effect, that is, if there is a a=1 in the record that is in and out, the C = C + 1 is directly updated without performing the operation of C = 3.
Copy Code code as follows:
Insert into table (A, B, c) VALUES (1, 2, 3) on duplicate key
Update C = c + 1;1 Update table Set c = C + 1 where a
The problem of repeated data insertions before the line, by troubleshooting the problem and the default isolation level of MySQL-repeatable read (can be reread) , repeatable read ensures that multiple instances of the same transaction will see the same data row when concurrently reading the data. Now through the experiment, the problem is analyzed.1. Turn on transaction A at Terminal A, and check.TRANSACTIO
In the recent project, you need to copy several dump files (text format, 1 ~ 2 GB) records are imported into the MySQL database. Due to the large data volume (millions or tens of millions of records), there are insert records and update records, the insert/update speed is slow.
At the beginning, a single SQL statement was handed over to
Low_priority, low-priorityUPDATE [low_priority] tbl_name SET col_name1=expr1,col_name2=expr2,... MySQL update with low_priority lets update not lock the table MySQL allows you to change the priority of statement scheduling, which allows queries from multiple clients to work
The idea of updating multiple records at a time in mysql encountered a problem: update a data table.
This table is created by myself and has seven fields: id, name, package, etc.
During creation, because I saved the name and package information in two text files,
So I will use the Insert method to Insert all the names into the database at a time.
All the names a
#-*-Encoding: utf8 -*-'''@ Author: crazyant.netRead the (id, age) data of Table B, and update Table A in sequence;'''From common. DBUtil import DBDbUtil = DB ('127. 0.0.1 ', 127, 'root', '', 'test ')Rs = dbUtil. query ("SELECT id, age FROM table_ B ")For row in rs:(Idv, age) = rowPrint (idv, age)Update_ SQL = "update table_a set age = '% s' where id =' % s';" % (age, idv)Print update_ SQLDbUtil.
index:ALTER table name ENABLE KEYS;7, the principle of design index:
The most appropriate column for the index is the column that appears in the WHERE clause, or the column specified in the JOIN clause (ON statement), rather than the column that appears after the select.
The greater the number of values in the indexed column, the better the index works.
Use a short index: for char and varchar columns, creating an index with only a po
), cc = values (bb) + values (cc)
This method determines whether the primary key or unique value exists before insertion. After values, multiple records can be followed. If not, insert data in batches. If yes, update the data. The update rules can be customized. cc = values (bb) + values (cc)
2. Use replace into tab (),(),..
Replace into table (aa, bb, cc) value
This article implements the random numbers of MySQL, random data selection by MySQ, random data query by MySQ, and random data update by MySQ.
MySQ Random Data Query
I have discussed this issue in the group before. It is quite interesting. mysql syntax is really fun. they all wanted to use PHP to implement random queries, but retrieving
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.