duanhan7001 2014-10-10 05:49
浏览 66
已采纳

当我尝试在表格中添加Check Constraint时没有任何变化

I am trying to add check constraint in my table that prevents adding more data into a table if the sum of rows shop_id is greater than 3. I have written the following code and its just not working. Kindly check this and guide me.

ALTER TABLE kinect_temp_data
ADD CONSTRAINT my_const CHECK (sum(distinct(shop_id))<3)

The above query runs successful,but it does not create any effect and i can still able to add more rows, and when i query this, it display that no check constraint was added.

SHOW CREATE TABLE kinect_temp_data

Output

CREATE TABLE `kinect_temp_data` (
 `cart_number` int(11) NOT NULL AUTO_INCREMENT,
 `product_id` varchar(50) NOT NULL,
 `shop_id` varchar(50) NOT NULL,
 `product_name` varchar(50) NOT NULL,
 `item_number` varchar(50) NOT NULL,
 `image1_path` varchar(50) NOT NULL,
 `image2_path` varchar(50) NOT NULL,
 `image3_path` varchar(50) NOT NULL,
 `price` int(11) NOT NULL,
 PRIMARY KEY (`cart_number`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1

Kindly check this and guide me what i am doing wring here.

Thanks.

  • 写回答

1条回答 默认 最新

  • duanlin6989 2014-10-10 06:16
    关注

    MySQL don't support check constraints -- they are ignored.

    But you can use BEFORE INSERT and BEFORE UPDATE triggers to realize such functionality.

    There is good explanation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记