mysql为什么说我的表不可更新,mysql表中的不可更新列

How to define a mysql table that should have one column as non updatable I mean once inserted it should not allow user to update in any way.

For Example table named tbl_xyz have 3 columns where one is non updatable then

tbl_xyz(c1,c2,c3), suppose I Insert values as (MBX,123,POQ) then lets say if c3 is non updatable then it should be always have value with first entry as POQ if some one tries to update then it should give some constrained column specific error.

If someone have did same table level defination with above mentioned restriction then please help and let me know please.

Thanks

解决方案

This can be done via a trigger in MySQL:

DELIMITER ;;

CREATE TRIGGER `trig_tbl_xyz_before_update`

BEFORE UPDATE ON `tbl_xyz` FOR EACH ROW

BEGIN

IF NEW.c3 != OLD.c3 THEN

SIGNAL SQLSTATE '45000'

SET MESSAGE_TEXT = 'tbl_xyz.c3 is not allowed to be updated, stop trying to update it.';

END IF;

END;;

DELIMITER ;

This will give the following response when trying to update tbl_xyz.c3 to a different value:

Error Code: 1644. tbl_xyz.c3 is not allowed to be updated, stop trying to update it.

I used the 45000 error code because it was suggested in the SIGNAL documentation.

To signal a generic SQLSTATE value, use '45000', which means “unhandled user-defined exception.”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值