简体   繁体   English

如何验证表的每一行是否与 MySQL 中另一个表的至少一行相关联?

[英]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.

相关问题 MySQL:对于表中的每一行,更改另一表中的一行 - MySQL: For each row in table, change one row in another table 如何从同一mysql表的另一行中的值中减去一行中的值 - How can I subtract the values in one row from the values in another row in the same mysql table 如果该表的关联表的列之一不满足条件,如何忽略该表中的一行? - How can I ignore a row from a table if one of it's associated table's column fail to meet the condition? 如何将一行从一个表移动到mysql中的另一个表? - How to move one row from one table, to another table in mysql? MySQL-如何将一个表中不同行的信息合并到另一表中的一行? - MySQL - How can I combine information from different rows in one table into a single row in another? 对于一个表中的每一行只有一行来自另一个表(mysql) - For each row from one table only one row from another table (mysql) MySQL:在一个表中为包含特定日期的另一表中的每一行查找最新行 - MySQL: Find most recent row in one table for each row in another table containing a specific date 验证该行在mySQL表中是否不可用 - Verify if the row is not available in mySQL table 如何创建MySQL JOIN查询以仅选择一个表中的行,其中对该行的一定数量的引用存在于另一个表中? - How can I create a MySQL JOIN query for only selecting the rows in one table where a certain number of references to that row exist in another table? 如何根据在另一个表中的日期之后加时间戳的行从一个 mysql 表中选择行 - How can I select rows from one mysql table based on the row being timestamped after a date in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM