doumi2106 2011-04-02 22:21 采纳率: 100%
浏览 38
已采纳

MySQL禁止具有2个相同列值的行与另一行

This is my MySQL table:

enter image description here

  • The left column auto increments.
  • The center column is the id# of the user.
  • The right column is the id# of a post that they like.

I am inserting data into the table like so:

$stmt = $db->prepare("INSERT INTO likes (user_id,post_id) VALUES (?,?)");
$stmt->execute(array($_SESSION['user'],$_POST['id']));

This works fine, but as you can see the MySQL table has two identical rows (id's 3 & 4) which both have a user_id of 5 and a post_id of 196. In other words my MySQL table records that the user with id = 5 likes the post with id = 196 two times. how do I disallow a situation where there are two rows that have identical user_id's and identical post_id's

  • 写回答

3条回答 默认 最新

  • dsmupo6631 2011-04-02 22:26
    关注

    You need to set a unique constraint on the user_id and post_id columns

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

报告相同问题?

悬赏问题

  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用