dongwusang0314 2012-02-03 16:02
浏览 29
已采纳

如果两个源同时向SQL表添加条目,它们都会添加“ADD UNIQUE”吗?

I have code similar to the following:

$sql2 = "ALTER TABLE table ADD UNIQUE(field2, feild3, field4, field5);";    
$sql2 .= "INSERT IGNORE INTO table (field1, field2, feild3, field4, field5) VALUES ('$_POST[field1]','$_POST[field2]','$_POST[field3]','$_POST[field4]','$_POST[field5]')";

if (!mysql_query($sql2,$con))
{
    die('Error: ' . mysql_error());
}
echo "1 record added";
}

I have found that I get an error if the table already has two rows which are the same but works well otherwise. I am worried that if I add the duplicates from two different sources that they will both be added to the table and later requests will trigger the error I got earlier because of the two almost identical rows. Is it possible to add two almost identical rows as shown above if the php file is executed at the same time from two different sources?

EDIT: FYI The code is a snippet from a larger program. I am sending data from an android app I am working on to the $_POST fields

  • 写回答

3条回答 默认 最新

  • douang4294 2012-02-03 16:15
    关注
    1. mysql_query DOES NOT ALLOW execute two queries in single call

    2. insert ignore won't return error when duplicate found (that's why it named as ignore)

    So, the FIRST error is caused by the alter ...; insert ...

    Then alter table is require execute once only,
    on your next execution, you are no longer require to perform alter table (again).
    Otherwise, you will getting the duplicate INDEX error (trying to add an existing index)

    For the unique index, is meant to prevent duplicate,
    so, it only will have one unique record (no matter what)

    And for you funny almost identical,
    it will never happen

    it blocked when duplication found on field 2..5

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么