I am starting new project but can not under stand where to start from.I need database design which has categories and 4 level sub categories but the product can have more than one category. so i m confused how my database should be.Plz help me out. Thank you very much for giving me your time in advance.
我正在开始新的项目,但不能站在从哪里开始。我需要数据库设计,它有类别和4级子类别,但产品可以有多个类别。所以我很困惑我的数据库应该如何.Plz帮助我。非常感谢您提前给我时间。
1 个解决方案
#1
3
product: holds the products with id
category: hold the category with id and name AND the parent category of the category
product_category: holds the cross-relation between one or many products and one or many categories...
product:保存具有id类别的产品:保存具有id和name的类别以及category_category类别的父类别:保存一个或多个产品与一个或多个类别之间的交叉关系...
If you use InnoDB on mysql or a good DBMS you should define the foreign-key constraints:
如果你在mysql或一个好的DBMS上使用InnoDB,你应该定义外键约束:
CREATE TABLE IF NOT EXISTS `mydb`.`category` (
`id` INT NOT NULL ,
`name` VARCHAR(45) NULL ,
`parent_id` INT NULL ,
PRIMARY KEY (`id`) ,
INDEX `parentCategory_idx` (`parent_id` ASC) ,
CONSTRAINT `parentCategory`
FOREIGN KEY (`parent_id` )
REFERENCES `mydb`.`category` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`product` (
`id` INT NOT NULL ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`product_category` (
`product_id` INT NOT NULL ,
`category_id` INT NOT NULL ,
PRIMARY KEY (`product_id`, `category_id`) ,
INDEX `product_idx` (`product_id` ASC) ,
INDEX `category_idx` (`category_id` ASC) ,
CONSTRAINT `product`
FOREIGN KEY (`product_id` )
REFERENCES `mydb`.`product` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `category`
FOREIGN KEY (`category_id` )
REFERENCES `mydb`.`category` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
#1
3
product: holds the products with id
category: hold the category with id and name AND the parent category of the category
product_category: holds the cross-relation between one or many products and one or many categories...
product:保存具有id类别的产品:保存具有id和name的类别以及category_category类别的父类别:保存一个或多个产品与一个或多个类别之间的交叉关系...
If you use InnoDB on mysql or a good DBMS you should define the foreign-key constraints:
如果你在mysql或一个好的DBMS上使用InnoDB,你应该定义外键约束:
CREATE TABLE IF NOT EXISTS `mydb`.`category` (
`id` INT NOT NULL ,
`name` VARCHAR(45) NULL ,
`parent_id` INT NULL ,
PRIMARY KEY (`id`) ,
INDEX `parentCategory_idx` (`parent_id` ASC) ,
CONSTRAINT `parentCategory`
FOREIGN KEY (`parent_id` )
REFERENCES `mydb`.`category` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`product` (
`id` INT NOT NULL ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`product_category` (
`product_id` INT NOT NULL ,
`category_id` INT NOT NULL ,
PRIMARY KEY (`product_id`, `category_id`) ,
INDEX `product_idx` (`product_id` ASC) ,
INDEX `category_idx` (`category_id` ASC) ,
CONSTRAINT `product`
FOREIGN KEY (`product_id` )
REFERENCES `mydb`.`product` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `category`
FOREIGN KEY (`category_id` )
REFERENCES `mydb`.`category` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;