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

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能