Analysis of the production process of MySQL database Insert the same record twice, but the primary key ID is not the same problem

Email a friend laopan which receive requests for assistance

laopan:


INSERT INTO HudsonResult (the JobID, EnvironmentID, FirstSessionID, RerunSessionID, State, Desp, OtherInfo) values 
((SELECT ID from Hudson WHERE Stream = 'A7510_R52_Integration' and State = 'N' and pakName = 'needCompile' and the User = 'jinhaiz') , 0, 'N', ' N', 'N', 'smoke_test', '')
if there is no record identical to execute the above command to insert, insert is prevented from being performed twice.
How to write this sentence?

I can not understand?
Now there is a problem is insert the same record twice, but the primary key ID is not the same.



me:
If mysql database, then:

1 If your table has a unique health, it can be carried out by the only healthy competition election.
2 If there is no unique health, you need to determine whether the data duplication conditions, according to the conditions of the first select it to see if there is, if there is no insert, if either coverage exists or to ignore is not performed, this process takes a number of sql statement, the best open transactions to be controlled.


And has a Mysql replace Insert into ..... on duplicate key update judgment is repeated to insert or update data, but this is based on the primary key or a unique judged healthy. You can refer to the following article.



http: //blog.csdn.




1. Screening how unique key? The question now is the only key difference is not the same, the other columns are the same.


me:
the table structure to me, I see, by executing the command show create table HudsonResult; derive a table structure.
If there is no unique health, it is not possible to determine the level to sql unique data, or resource consumption is too high. From the application point source of data required to verify the angle filter.



laopan:
MySQL> Create Show Table HudsonResult;
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table        | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| HudsonResult | CREATE TABLE `HudsonResult` (
  `JobID` int(32) NOT NULL,
  `EnvironmentID` int(32) NOT NULL,
  `FirstSessionID` varchar(100) default NULL,
  `RerunSessionID` varchar(100) default NULL,
  `State` varchar(5) default NULL,
  `ID` int(32) NOT NULL auto_increment,
  `Desp` varchar(100) default NULL,
  `ExecNum` int(32) default NULL,
  `FailNum` int(32) default NULL,
  `ATCStartTime` datetime default NULL,
  `ATCEndTime` datetime default NULL,
  `FocNumBefore` int(32) default '0',
  `FocNumAfter` int(32) default '0',
  `Priority` int(32) default '0',
  `FocDesp` varchar(5000) default '',
  `OtherInfo` varchar(100) default NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=14910 DEFAULT CHARSET=latin1 | 
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
The question now background is this, after insert on the page, the computer card a bit, this time has not yet returned, and point a bit, into and insert once, resulting in two record
content is the same, but the primary key is not the same of, java you have done, there is no idea of what to avoid this?


ME:
the submit button to do the next deal, very simple, after submission once the button is set to gray, the operator can not click, only the outcome of pending submission process to proceed to the next step.


laopan:

 

Get, huh, huh, ha easy to use, consider more, Thanks.

 

 

Summary: There are many times seen are due to erroneous data resulting from the application of the bug, while cleaning up the data from the root causes of the problem, sql data plane can not move as much as possible not to move.

Guess you like

Origin blog.csdn.net/csdnhsh/article/details/91784331