mysql 动态添加条件,MySQL条件插入动态值

博客讨论了如何在MySQL中创建一个检查约束,确保`Member_Infos`表中针对不同会员类型的记录数量不超过`Member_Types`表中定义的`Member_Limit`。由于MySQL不强制执行CHECK约束,作者提出使用触发器作为替代方案,在插入新记录前检查记录数。文章提供了一个示例查询来实现这一功能。
摘要由CSDN通过智能技术生成

So before I explain my question, Here are a couple stripped down table definitions to help illustrate my issue:

-- Holds data about different memberships

CREATE TABLE IF NOT EXISTS `Member_Types` (

`ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,

`Name` VARCHAR(20) NOT NULL,

`Description` VARCHAR(255) NOT NULL,

`Member_Limit` TINYINT(2) UNSIGNED NOT NULL DEFAULT '0',

PRIMARY KEY( `ID` )

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `Member_Types` ( `ID`, `Name`, `Description`, `Member_Limit` ) VALUES

( 1, 'General Member', 'Description of this membership.', 00 ),

( 2, 'Extended Member', 'Description of this membership.', 00 ),

( 3, 'Premium Member', 'Description of this membership.', 00),

( 4, 'General Administrator', 'Description of this membership.', 05 ),

( 5, 'Master Administrator', 'Description of this membership.', 01 );

-- Stores the basic data about our site members

CREATE TABLE IF NOT EXISTS `Member_Infos` (

`ID` BIGINT(8) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,

`Username` VARCHAR(30) NOT NULL UNIQUE,

`Password` CHAR(41) NOT NULL,

`EmailAddr` VARCHAR(100) NOT NULL,

`Type_ID` INT UNSIGNED NOT NULL,

`Salt_ID` BIGINT(8) UNSIGNED ZEROFILL NOT NULL,

PRIMARY KEY( `ID` ),

FOREIGN KEY( `Type_ID` ) REFERENCES `Member_Types` ( `ID` )

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

The Member_Infos table contains a foreign key that binds Member_Infos.Type_ID = Member_Types.ID

The Member_Types table has a column Member_Limit which contains an integer that represents the maximum number of records that the Member_Infos table

can contain where its Type_ID is equal to Member_Types.ID

I was able to write a check constraint but apparently mysql doesn't enfore check constraints.

I would like to write a trigger that checks that the count of records in the member_infos table is <= the Member_Types.Member_Limit before inserting.

for example: using the data above, record 4 has a Member_Limit = 5. If a new record is attempted to be inserted into the Member_Infos table that

has a Type_ID = 4 and a count of records in the Member_Infos table with Type_ID <= 5 than the data is inserted, otherwise it is rejected.

Any advice would be greatly appreciated.

解决方案

Instead of a trigger you could write your own plain query to check the "constraints" before insert. Try:

INSERT INTO member_infos

SELECT 1, 'Timothy', 'secret', 'me@myself.com', 5, 0

FROM dual

WHERE (SELECT COUNT(*) FROM member_infos WHERE Type_ID = 5)

<

(SELECT Member_Limit FROM member_types WHERE ID = 5)

I have used to check in case of Type_ID = 5. This ignores if count criterion is not met and inserts only if count of member of entries in member_info with type id = 5 is less than the limit set in your member_types table

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值