如何在 mysql 表中创建具有不同选项的类别
How can I create categories with different options of it in mysql tables
我想在 MySQL 中设计一个关于带有类别选项的类别的数据库。
示例:脚本分类..
当您要在“汽车类别”中添加广告时,您必须为这辆车添加一些选项,但是当您在“待售公寓类别”中添加广告时,您必须为该公寓添加一些选项,例如,这张来自 Dubizzle 网站的图片说明了分类。
当您想在“汽车类别”中添加汽车时,您将显示这些选项
当您想在“待售公寓类别”中添加广告时
您将显示这些选项:
如何设计数据库?
categories and sub-categories with different options for any categories
create table category
(
id int auto_increment primary key,
catName varchar(100) not null
);
create table catOption
( -- no attempt to share option across multiple categories
id int auto_increment primary key,
catId int not null,
descr varchar(100) not null,
listingOrder int not null -- order listed on screen
-- include FK into category
);
create table listing
(
id int auto_increment primary key,
catId int not null,
title varchar(100) not null,
verbeage text,
price decimal(12,0) not null
-- include FK into category
);
create table listOptions
( -- this table houses a particular listing options (answers if you will)
-- programmer chooses whether or not to save 'blanks'
id int auto_increment primary key,
listingId int not null,
optionId int not null,
answer varchar(100) not null -- you choose datatype
-- include FK into listing
-- include FK into catOption
);
The FOREIGN KEY constraint is used to prevent actions that would
destroy links between tables.
The FOREIGN KEY constraint also prevents invalid data from being
inserted into the foreign key column, because it has to be one of the
values contained in the table it points to.
类别
首先,您需要定义类别 table,例如 categories(id, name, description)
控件类型
正如我们从您分享的图片中看到的那样,有时您有一组复选框,有时您有下拉选项等。要支持这一点,您需要 table 比如 control_types(身份证、姓名、描述)。在您的应用程序中,您将通过控件类型值知道需要使用什么控件以及应如何理解 selection。
控件
现在,您可以像这样创建控件:controls(id, control_type_id, title)。请注意,我们在此 table 中没有 category_id
,因为我们希望能够支持在多个类别中重用某些控件的可能性,只要它们与这些控件兼容即可。例如,两个类别都需要您拥有的 price
控件。所以假设每个控件都链接到一个类别是不够的,恰恰相反,一个类别可以有多个控件并且一个控件可能属于多个类别。
类别控件
为了实现上一章中描述的多对多关系,我们创建了一个 table 来存储对:controls_of_categories(id, category_id, control_id).
控制项
很高兴我们有控件,但有时它们并不微不足道。有时我们需要 select 某些选项(下拉列表)中的单个值,或者我们可能需要能够同时 select 多个选项(复选框)。为此,我们需要另一个 table,像这样:control_items(id, control_id, label, value).
Category_sub_category_tab:
CId PK
,CName Text
,SubCatId FK(Category_sub_category_tab(CId))
选项:
ID PK
,OName Text
,CID FK(Category_sub_category_tab(CId))
它是一个动态方案,首先你必须定义你的类别、控件(文本、drop-down 等)然后你必须定义 categoryFields 这将包含 categoryId、字段信息..,你必须为类型 drop-down 定义字段值 table 之类的东西
我想在 MySQL 中设计一个关于带有类别选项的类别的数据库。
示例:脚本分类..
当您要在“汽车类别”中添加广告时,您必须为这辆车添加一些选项,但是当您在“待售公寓类别”中添加广告时,您必须为该公寓添加一些选项,例如,这张来自 Dubizzle 网站的图片说明了分类。
当您想在“汽车类别”中添加汽车时,您将显示这些选项
当您想在“待售公寓类别”中添加广告时 您将显示这些选项:
如何设计数据库?
categories and sub-categories with different options for any categories
create table category
(
id int auto_increment primary key,
catName varchar(100) not null
);
create table catOption
( -- no attempt to share option across multiple categories
id int auto_increment primary key,
catId int not null,
descr varchar(100) not null,
listingOrder int not null -- order listed on screen
-- include FK into category
);
create table listing
(
id int auto_increment primary key,
catId int not null,
title varchar(100) not null,
verbeage text,
price decimal(12,0) not null
-- include FK into category
);
create table listOptions
( -- this table houses a particular listing options (answers if you will)
-- programmer chooses whether or not to save 'blanks'
id int auto_increment primary key,
listingId int not null,
optionId int not null,
answer varchar(100) not null -- you choose datatype
-- include FK into listing
-- include FK into catOption
);
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.
类别
首先,您需要定义类别 table,例如 categories(id, name, description)
控件类型
正如我们从您分享的图片中看到的那样,有时您有一组复选框,有时您有下拉选项等。要支持这一点,您需要 table 比如 control_types(身份证、姓名、描述)。在您的应用程序中,您将通过控件类型值知道需要使用什么控件以及应如何理解 selection。
控件
现在,您可以像这样创建控件:controls(id, control_type_id, title)。请注意,我们在此 table 中没有 category_id
,因为我们希望能够支持在多个类别中重用某些控件的可能性,只要它们与这些控件兼容即可。例如,两个类别都需要您拥有的 price
控件。所以假设每个控件都链接到一个类别是不够的,恰恰相反,一个类别可以有多个控件并且一个控件可能属于多个类别。
类别控件
为了实现上一章中描述的多对多关系,我们创建了一个 table 来存储对:controls_of_categories(id, category_id, control_id).
控制项
很高兴我们有控件,但有时它们并不微不足道。有时我们需要 select 某些选项(下拉列表)中的单个值,或者我们可能需要能够同时 select 多个选项(复选框)。为此,我们需要另一个 table,像这样:control_items(id, control_id, label, value).
Category_sub_category_tab:
CId PK
,CName Text
,SubCatId FK(Category_sub_category_tab(CId))
选项:
ID PK
,OName Text
,CID FK(Category_sub_category_tab(CId))
它是一个动态方案,首先你必须定义你的类别、控件(文本、drop-down 等)然后你必须定义 categoryFields 这将包含 categoryId、字段信息..,你必须为类型 drop-down 定义字段值 table 之类的东西