mysql检查约束无效_mysql – 检查约束不起作用?

你需要的是两个触发无效年龄条件的触发器

>插入之前

>在更新之前

以下是基于“触发器验证数据”子标题下的第MySQL Stored Procedure Programming章第254-256页的MySQL触发器的简单错误捕获方法:

drop table mytable;

create table mytable (

id smallint unsigned AUTO_INCREMENT,

age tinyint not null,

primary key(id)

);

DELIMITER $$

CREATE TRIGGER checkage_bi BEFORE INSERT ON mytable FOR EACH ROW

BEGIN

DECLARE dummy,baddata INT;

SET baddata = 0;

IF NEW.age > 20 THEN

SET baddata = 1;

END IF;

IF NEW.age < 1 THEN

SET baddata = 1;

END IF;

IF baddata = 1 THEN

SELECT CONCAT('Cannot Insert This Because Age ',NEW.age,' is Invalid')

INTO dummy FROM information_schema.tables;

END IF;

END; $$

CREATE TRIGGER checkage_bu BEFORE UPDATE ON mytable FOR EACH ROW

BEGIN

DECLARE dummy,baddata INT;

SET baddata = 0;

IF NEW.age > 20 THEN

SET baddata = 1;

END IF;

IF NEW.age < 1 THEN

SET baddata = 1;

END IF;

IF baddata = 1 THEN

SELECT CONCAT('Cannot Update This Because Age ',NEW.age,' is Invalid')

INTO dummy FROM information_schema.tables;

END IF;

END; $$

DELIMITER ;

insert into mytable (age) values (10);

insert into mytable (age) values (15);

insert into mytable (age) values (20);

insert into mytable (age) values (25);

insert into mytable (age) values (35);

select * from mytable;

insert into mytable (age) values (5);

select * from mytable;

结果如下:

mysql> drop table mytable;

Query OK, 0 rows affected (0.03 sec)

mysql> create table mytable (

-> id smallint unsigned AUTO_INCREMENT,

-> age tinyint not null,

-> primary key(id)

-> );

Query OK, 0 rows affected (0.06 sec)

mysql> DELIMITER $$

mysql> CREATE TRIGGER checkage_bi BEFORE INSERT ON mytable FOR EACH ROW

-> BEGIN

-> DECLARE dummy,baddata INT;

-> SET baddata = 0;

-> IF NEW.age > 20 THEN

-> SET baddata = 1;

-> END IF;

-> IF NEW.age < 1 THEN

-> SET baddata = 1;

-> END IF;

-> IF baddata = 1 THEN

-> SELECT CONCAT('Cannot Insert This Because Age ',NEW.age,' is Invalid')

-> INTO dummy FROM information_schema.tables;

-> END IF;

-> END; $$

Query OK, 0 rows affected (0.08 sec)

mysql> CREATE TRIGGER checkage_bu BEFORE UPDATE ON mytable FOR EACH ROW

-> BEGIN

-> DECLARE dummy,baddata INT;

-> SET baddata = 0;

-> IF NEW.age > 20 THEN

-> SET baddata = 1;

-> END IF;

-> IF NEW.age < 1 THEN

-> SET baddata = 1;

-> END IF;

-> IF baddata = 1 THEN

-> SELECT CONCAT('Cannot Update This Because Age ',NEW.age,' is Invalid')

-> INTO dummy FROM information_schema.tables;

-> END IF;

-> END; $$

Query OK, 0 rows affected (0.07 sec)

mysql> DELIMITER ;

mysql> insert into mytable (age) values (10);

Query OK, 1 row affected (0.06 sec)

mysql> insert into mytable (age) values (15);

Query OK, 1 row affected (0.05 sec)

mysql> insert into mytable (age) values (20);

Query OK, 1 row affected (0.04 sec)

mysql> insert into mytable (age) values (25);

ERROR 1172 (42000): Result consisted of more than one row

mysql> insert into mytable (age) values (35);

ERROR 1172 (42000): Result consisted of more than one row

mysql> select * from mytable;

+----+-----+

| id | age |

+----+-----+

| 1 | 10 |

| 2 | 15 |

| 3 | 20 |

+----+-----+

3 rows in set (0.00 sec)

mysql> insert into mytable (age) values (5);

Query OK, 1 row affected (0.07 sec)

mysql> select * from mytable;

+----+-----+

| id | age |

+----+-----+

| 1 | 10 |

| 2 | 15 |

| 3 | 20 |

| 4 | 5 |

+----+-----+

4 rows in set (0.00 sec)

mysql>

另请注意,自动增量值不会浪费或丢失.

试试看 !!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值