Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL类别和子类别表结构_Mysql_Database_Database Design - Fatal编程技术网

MySQL类别和子类别表结构

MySQL类别和子类别表结构,mysql,database,database-design,Mysql,Database,Database Design,我在设置一个mysql表时遇到了一点问题,该表将包含类别和子类别的列表。我不知道如何设置桌子。是否需要两张单独的桌子?1个用于主要类别,1个用于子类别,或者可以全部放在一个表中?你喜欢这个工作吗 Create Table categories ( category_id INT UNSIGNED NOT NULL AUTO_INCREMENT, sub_id INT UNSIGNED NOT NULL, name VARCHAR(100) NOT NULL, PR

我在设置一个mysql表时遇到了一点问题,该表将包含类别和子类别的列表。我不知道如何设置桌子。是否需要两张单独的桌子?1个用于主要类别,1个用于子类别,或者可以全部放在一个表中?你喜欢这个工作吗

Create Table categories (
    category_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    sub_id INT UNSIGNED NOT NULL,
    name VARCHAR(100) NOT NULL,
    PRIMARY KEY (category_id)
)

CREATE TABLE items (
    item_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    description VARCHAR(100) NOT NULL,
    PRIMARY KEY (item_id),
    FOREIGN KEY (category_id) REFERENCES categories (category_id),
    FOREIGN KEY (sub_id) REFERENCES categories (sub_id)
)
这是有效的还是完全错误的?提前感谢您的帮助

视情况而定

类别和子类别真的是两种不同的东西吗?这意味着类别没有父类别,而子类别始终位于父类别中,并且它们本身没有其他子类别。那么两张桌子就可以了

如果它像一棵树,其中只有类别,既可以是子类别,也可以有子类别,那么您应该使用一个表(Google的“嵌套集”)


(或者,您可能不是指类别/子类别,而是指主要类别/次要类别,其中次要类别不固定为某个主要类别。电子产品+自行车,而不是自行车->车速表。如果它也可以是自行车+电子产品,那么您可以使用一个表格)

如果您100%确定只有两个级别的类别(主类别和子类别),您可以做一些不同的事情。这些都不是您建议的解决方案:

CREATE TABLE categories (
    id int not null primary key,
    main varchar(64)
    sub varchar(64)
);

CREATE TABLE objects (
    id int not null primary key,
    category_id int,
    name varchar(64),
    FOREIGN KEY (category_id) REFERENCES categories (id)
);
想要所有车辆吗

SELECT * 
FROM objects AS o 
INNER JOIN categories AS c ON o.category_id = c.id 
WHERE c.main = 'vehicles';
想要所有的机器人吗

SELECT * 
FROM objects AS o 
INNER JOIN categories AS c ON o.category_id = c.id 
WHERE c.main = 'vehicles' and c.sub='Roflcopters';
如果您想要“vehicle”类别中的内容,而不是verhicles的任何子类别中的内容,只需拥有一个类别记录,其中main='vehicles'具有sub NULL

当然,这不是特别灵活。您只能使用两个级别的分类,并且在您的类别模型中没有嵌入很多业务逻辑。但这可能足以满足你的需要



另外两个很好的、经过验证的模型是邻接列表模型和嵌套集模型,这两个模型都有描述,有很多很好的示例mysql代码,

这显然是可行的。但您可以使用单独的表格

create table categories 
(
    categoryId int not null,
    categoryName varchar(20) not null,
    primary key(categoryId)
);

create table subcategories 
(
    subcategoryId int not null,
    subcategoryName varchar(20) not null,
    parentId int not null, 
    primary key(subcategoryId),
    foreign key(categoryId) references categories(categoryId)
);


create tables items 
(
    item_id int unsigned not null auto_increment,
    name varchar(255) not null,
    description varchar(100) not null,
    primary key(item_id),
    foreign key(categoryId) references categories(categoryId),
    foreign key(subcategoryId) references subcategories(subcategoryId)
 )

@如果你看答案,在你投票的上下箭头下方会有一个小勾号,点击它。每个问题只能接受一个答案,但如果您觉得有用,您可以对任何其他问题(包括已接受的答案)进行投票。