[英]how can I verify that each row of a table is associated with at least one row of another table in MySQL?
I'm trying to verify that in a table of my DB each row is associated with at least one row of another table.我正在尝试验证在我的数据库的表中,每一行都与另一个表的至少一行相关联。
In particular I have these 3 tables:特别是我有这3张桌子:
CREATE TABLE IF NOT EXISTS coltraneShop.Personage (
`Name` VARCHAR(20) NOT NULL,
CONSTRAINT PK_Personage PRIMARY KEY (`Name`)
);
CREATE TABLE IF NOT EXISTS coltraneShop.`Product_Personage` (
`Product code` BIGINT UNSIGNED NOT NULL,
`Personage's name` VARCHAR(20) NOT NULL,
CONSTRAINT PK_Product_Personage PRIMARY KEY (`Product code`, `Personage's name`)
);
ALTER TABLE coltraneShop.`Product_Personage` ADD CONSTRAINT `FK_ProductPersonage_Personage` FOREIGN KEY (`Personage's name`) REFERENCES coltraneShop.Personage(`Name`)
ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE IF NOT EXISTS coltraneShop.Product (
`Product code` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`Category` CHAR(9) NOT NULL,
CHECK(`Category`="DVD" OR `Category`='Album' OR `Category`='Comics' OR `Category`='Book' OR `Category`='Videogame'),
`Title` VARCHAR(50) NOT NULL,
`Quantity` INT UNSIGNED NOT NULL,
CHECK(`Quantity` BETWEEN 000 AND 999),
`Year of publication` INT UNSIGNED NOT NULL,
CHECK(`Year of publication` BETWEEN 0000 AND 9999),
`Price` DECIMAL(6,2) UNSIGNED NOT NULL,
`Shipment's name` CHAR(13) NOT NULL,
CHECK(
(SELECT count(*)
FROM coltraneShop.Product_Personage AS PP
WHERE `Category`= "Comics" AND PP.`Product code` = `Product code`) >= 1
),
CONSTRAINT PK_Product PRIMARY KEY (`Product code`)
);
ALTER TABLE coltraneShop.Product ADD CONSTRAINT `FK_Product_Shipment` FOREIGN KEY(`Shipment's name`) REFERENCES coltraneShop.Shipment(`Name`)
ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE coltraneShop.`Product_Personage` ADD CONSTRAINT `FK_ProductPersonage_Product` FOREIGN KEY (`Product code`) REFERENCES coltraneShop.Product(`Product code`)
ON DELETE CASCADE ON UPDATE CASCADE;
OK, So.好的,所以。 the table "Product_Personage" expresses a many-to-many relationship between product and personage.
表“Product_Personage”表示产品和人物之间的多对多关系。 I want to check that if a row in the product table has the "category" attribute set to "Comics" then this product must appear at least once in the Product_Personage table.
我想检查如果产品表中的一行将“类别”属性设置为“漫画”,那么该产品必须在 Product_Personage 表中至少出现一次。
In the code I left what seems to me to be correct to do in the product table.在代码中,我在产品表中留下了我认为正确的做法。 That is CHECK.
那就是检查。
I'm using MySQL and when execute this code I get an error that is:我正在使用 MySQL ,执行此代码时出现错误:
CREATE TABLE IF NOT EXISTS coltraneShop.Product ( `Product code` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`Category` CHAR(9) NOT NULL,
CHECK(`Category`="DVD" OR `Category`='Album' OR `Category`='Comics' OR `Category`='Book' OR `Category`='Videogame'), `Title` VARCHAR(50) NOT NULL,
`Quantity` INT UNSIGNED NOT NULL,
CHECK(`Quantity` BETWEEN 000 AND 999),
`Year of publication` INT UNSIGNED NOT NULL,
CHECK(`Year of publication` BETWEEN 0000 AND 9999),
`Price` DECIMAL(6,2) UNSIGNED NOT NULL,
`Shipment's name` CHAR(13) NOT NULL,
CHECK( (SELECT count(*) FROM coltraneShop.Product_Personage AS PP WHERE `Category`= "Comics" AND PP.`Product code` = `Product code`) >= 1 ),
CONSTRAINT PK_Product PRIMARY KEY (`Product code`) )
Error Code: 3815. An expression of a check constraint 'Product_chk_4' contains disallowed function.
Does it make sense to do such a check, and if so is it correct to write it as I did?做这样的检查是否有意义,如果是这样,像我一样写它是否正确? Thanks to all!
谢谢大家!
As in the mysql manual stated如 mysql 手册中所述
Stored functions and user-defined functions are not permitted.
Stored procedure and function parameters are not permitted.
Variables (system variables, user-defined variables,
and stored program local variables) are not permitted.
Subqueries are not permitted.
https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
that is why这就是为什么
CHECK((SELECT count(*) FROM coltraneShop.Product_Personage AS PP
WHERE `Category`= "Comics" AND PP.`Product code` = `Product code`) >= 1),
fails.失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.