MySQL production library Insert twice the same record but the primary key ID is not the same problem as the analysis process

  • 2020-06-07 05:25:53
  • OfStack

Email received a request for help from a friend laopan

laopan:
insert into HudsonResult(JobID,EnvironmentID,FirstSessionID,RerunSessionID,State,Desp,OtherInfo) values
((select ID from Hudson where Stream='A7510_R52_Integration' and State='N' and pakName='needCompile' and User='jinhaiz'),0,'N','N','N','smoke_test','')
If the same record does not exist, execute the insert command above to prevent insert from being executed twice.
How should I write this statement?

Does that make sense?
Now the problem is that insert has the same record twice but ID doesn't have the same primary key.

me :
If it's an mysql database:

1 If you have the only one in your table, you can use the only one to select.
2 If there is no only one key, you need to have a condition to determine whether the data is repeated. According to this condition, first come out select and see if there is one. If there is not, insert.

Mysql has 1 replace and Insert into... on duplicate key update to determine if the duplicate data is insert or update, but this is based on the primary health or only 1 health. You can refer to the following article (https: / / www. ofstack. com article / 47090 htm).


laopan:
1. How to select the only key? Now the problem is that the only difference is key not one, the other columns are all one.

me :
Send me the table structure so I can see it, by executing the command show create table HudsonResult; You get the table structure.
If there is no unique key, the uniqueness of data cannot be judged from the level of sql, otherwise the resource consumption will be too great. Filtering needs to be validated from an application perspective data source perspective.

laopan:

mysql > show create 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)

Now the background of the problem is as follows: after insert on the web page, the computer got stuck 1 time, but it did not return at this time. After clicking 1 time, it became insert again, resulting in 2 records
The content is 1, but the primary key is not 1, java you have also done, do you have any idea to avoid this?

ME :
The submit button is processed. It is very simple. After submitting once, press the button to set it to gray.

laopan:

Done, ha ha, easy to use ha, much thought, thanks.

Conclusion: It can be seen that many times the wrong data are caused by the application of bug. When cleaning the data, we should deal with the problem from the root. sql at the data level can be kept still as much as possible.


Related articles: