外键列是否可以在来自同一引用 table 的一行中具有多值?
Could a foreign key column has multivalue in one row from the same reference table?
我正在尝试在 MySQL Workbench 上创建数据库。如果外键列在一行中具有多值是否合法?我想这样做是因为根据事件类别,用户是否可以看到该事件。此外,一个事件可能有多个事件类别。我知道我可以为事件 table 创建一个复合键。但是我想知道我可以在一行中将多值作为外键吗?
这是我的活动 table:
CREATE TABLE IF NOT EXISTS `mydb`.`EVENT` (
`eventID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`eventCategory` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NOT NULL,
`eventDescription` VARCHAR(280) NULL,
`date` DATETIME(1) NOT NULL,
`locationDescription` VARCHAR(45) NOT NULL,
`regionID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`eventID`),
INDEX `fk_EVENT_category_1_idx` (`eventCategory` ASC) VISIBLE,
INDEX `fk_EVENT_region_1_idx` (`regionID` ASC) VISIBLE,
CONSTRAINT `fk_EVENT_region_1`
FOREIGN KEY (`regionID`)
REFERENCES `mydb`.`REGION` (`regionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_EVENT_category_1`
FOREIGN KEY (`eventCategory`)
REFERENCES `mydb`.`CATEGORY` (`categoryID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
而且我想在一个ro中存储多个类别。我想这样做是因为根据这些类别,某些用户将无法在应用程序中看到该事件。
这是我的分类 table:
CREATE TABLE IF NOT EXISTS `mydb`.`CATEGORY` (
`categoryID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`disorderID` INT UNSIGNED NOT NULL,
`categoryDescription` VARCHAR(45) NOT NULL,
PRIMARY KEY (`categoryID`, `disorderID`),
INDEX `fk_CATEGORY_disorder_1_idx` (`disorderID` ASC) VISIBLE,
CONSTRAINT `fk_CATEGORY_disorder_1`
FOREIGN KEY (`disorderID`)
REFERENCES `mydb`.`DISORDERS` (`disorderID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
And I want to store multiple categories in one ro. I want to do that because based on those categories, some of the users wouldn't be allowed to see the event in the application.
没有。不要那样做。这将使您的模式非规范化,并在以后使简单的事情变得非常复杂。
事件和类别之间存在多对多关系。表示它的正确方法是创建第三个 table,其中每个 event/category table 都存储在单独的行中。
类似于:
create table event_categories (
event_id int not null,
category_id int not null,
primary key (event_id, category_id),
foreign key(event_id) references event(event_id),
foreign key(category_id) references category(category_id),
);
我正在尝试在 MySQL Workbench 上创建数据库。如果外键列在一行中具有多值是否合法?我想这样做是因为根据事件类别,用户是否可以看到该事件。此外,一个事件可能有多个事件类别。我知道我可以为事件 table 创建一个复合键。但是我想知道我可以在一行中将多值作为外键吗?
这是我的活动 table:
CREATE TABLE IF NOT EXISTS `mydb`.`EVENT` (
`eventID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`eventCategory` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NOT NULL,
`eventDescription` VARCHAR(280) NULL,
`date` DATETIME(1) NOT NULL,
`locationDescription` VARCHAR(45) NOT NULL,
`regionID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`eventID`),
INDEX `fk_EVENT_category_1_idx` (`eventCategory` ASC) VISIBLE,
INDEX `fk_EVENT_region_1_idx` (`regionID` ASC) VISIBLE,
CONSTRAINT `fk_EVENT_region_1`
FOREIGN KEY (`regionID`)
REFERENCES `mydb`.`REGION` (`regionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_EVENT_category_1`
FOREIGN KEY (`eventCategory`)
REFERENCES `mydb`.`CATEGORY` (`categoryID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
而且我想在一个ro中存储多个类别。我想这样做是因为根据这些类别,某些用户将无法在应用程序中看到该事件。
这是我的分类 table:
CREATE TABLE IF NOT EXISTS `mydb`.`CATEGORY` (
`categoryID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`disorderID` INT UNSIGNED NOT NULL,
`categoryDescription` VARCHAR(45) NOT NULL,
PRIMARY KEY (`categoryID`, `disorderID`),
INDEX `fk_CATEGORY_disorder_1_idx` (`disorderID` ASC) VISIBLE,
CONSTRAINT `fk_CATEGORY_disorder_1`
FOREIGN KEY (`disorderID`)
REFERENCES `mydb`.`DISORDERS` (`disorderID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
And I want to store multiple categories in one ro. I want to do that because based on those categories, some of the users wouldn't be allowed to see the event in the application.
没有。不要那样做。这将使您的模式非规范化,并在以后使简单的事情变得非常复杂。
事件和类别之间存在多对多关系。表示它的正确方法是创建第三个 table,其中每个 event/category table 都存储在单独的行中。
类似于:
create table event_categories (
event_id int not null,
category_id int not null,
primary key (event_id, category_id),
foreign key(event_id) references event(event_id),
foreign key(category_id) references category(category_id),
);