数据库设计,类别中的项目,子类别&主题 [英] Database Design , Items in Category, Sub Category & Theme

查看:125
本文介绍了数据库设计,类别中的项目,子类别&主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  CREATE TABLE产品(ProductID int,说明nvarchar(100))

CREATE TABLE CategoryID(CategoryID int,Description nvarchar(100),ProductID int)
CREATE TABLE SubCategoryID(SubCategoryID int,CategoryID int,Description nvarchar(100),ProductID int)

CREATE TABLE ThemeID(ThemeID int,Description nvarchar(100),ProductID int)

我使用的是Laravel ORM

 产品hasMany->类别
产品hasMany->子类别
产品hasMany->主题

类别BelongsTo->产品
子类别BelongsTo-> Category
Theme BelongsTo - >产品

每个项目都有一个主题,属于多个类别,子类别是可选的。



有关此设计的任何建议吗?提前感谢!



这是最佳做法吗?正在尝试开始

解决方案

让我们看看一个想法,我认为这是好的使用:
first创建类别表:

  CREATE TABLE`category`(
`id` int(11)NOT NULL AUTO_INCREMENT,
`name` varchar(100)NOT NULL,
`category_father_id` int(11)DEFAULT'0',
`is_active` tinyint(1)NOT NULL,
(`id`),
UNIQUE KEY`name`(`name`),
KEY`category_father_id`(`category_father_id`),
CONSTRAINT`constraint_name`FOREIGN KEY(`category_father_id`) REFERENCES`category`(`id`)ON删除CASCADE ON UPDATE CASCADE
)ENGINE = InnoDB;

那么对于您的产品表格,您可以保持原样:

  CREATE TABLE Product(ProductID int,Description nvarchar(100)); 

现在通常您可以拥有属于多个类别的产品。因此,正确的做法是在产品和类别之间有m:n关系。它可以通过添加:

  create table product_category(
ProductId int(11)not null,
CategoryId int(11)not null,
unique(ProductId,CategoryId),
外键(ProductId)引用在删除级联更新级联上的产品(ProductID),
外键)引用在删除级联上更新级联上的类(id)
)engine = innodb;

,您可以保留主题。



您将看到表可以使用 category_father_id 外键处理嵌套类别。 / p>

但是要记住的一点是,毕竟它总是关于你的域/业务逻辑。


CREATE TABLE Product (ProductID int, Description nvarchar(100))

CREATE TABLE CategoryID (CategoryID int, Description nvarchar(100),ProductID int)
CREATE TABLE SubCategoryID (SubCategoryID int, CategoryID int, Description nvarchar(100),ProductID int)

CREATE TABLE ThemeID (ThemeID int, Description nvarchar(100),ProductID int)

I'm using Laravel ORM

Product hasMany-> Category
Product hasMany-> SubCategory
Product hasMany-> Theme

Category BelongsTo->Product
SubCategory BelongsTo->Category
Theme BelongsTo -> Product

Each item has got a theme and belongs to multiple category, Sub Category is Optional.

Any advice on this design? Thanks in advance!

Is this best practice? Trying to Start right

解决方案

Let show you an idea which IMHO I think it is good to be used: first create the category table:

CREATE TABLE `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `category_father_id` int(11) DEFAULT '0',
  `is_active` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `category_father_id` (`category_father_id`),
  CONSTRAINT `constraint_name` FOREIGN KEY (`category_father_id`) REFERENCES `category` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

then for your product table you can keep it as it is:

CREATE TABLE Product (ProductID int, Description nvarchar(100));

Now Usually you can have a Product which belongs to several categories. Hence, the correct way to do it is to have m:n relation between Product and Category. and it can be done by adding:

create table product_category(
ProductId int(11) not null,
CategoryId int(11) not null,
unique (ProductId,CategoryId),
foreign key (ProductId) references Product (ProductID) on update cascade on delete cascade,
foreign key (CategoryId) references category (id) on update cascade on delete cascade
)engine=innodb;

and you can keep the theme as it is.

you will see that category table can handles the nesting categories using category_father_id foreign key on it self.

But a note to keep in mind is, after all, it is always about your domain/business logic.

这篇关于数据库设计,类别中的项目,子类别&主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆