简体   繁体   English

MySQL中的一对多关系表

[英]One to Many relationship table in MySQL

I am designing a database for a real estate application. 我正在设计一个房地产应用程序的数据库。 the basic structure of the application is as follows. 应用程序的基本结构如下。

A) the application consist of storing the records related to real estate properties. A)申请包括存储与房地产相关的记录。

B) the properties are divided into categories and transaction type (sale, rent, lease) etc. B)物业分为类别和交易类型(销售,租赁,租赁)等。

c) categories is divided into subcategories. c)类别分为子类别。

take for example the following records exist. 例如,存在以下记录。

Transaction = Rent , Sale, Lease.

Categories = Residential , Commercial , Industrial , Agricultural

SubCategories = Residential_Apartment (belongs to residential category)
                Villa/House (belongs to residential category)
                Commercial_OfficeSpace (belongs to commercial category)
                Commercial_Plot (belongs to commercial category)
                Industrial_Plot (belongs to industrial category)

i have to create a relationship between the propert and all three above to identify which property is being stored for example 我必须在属性和上面所有三个之间建立一种关系,以确定例如存储哪个属性

Property with id:1 is Transaction:Rent, Category:Residential, Subcategory:Villa/House

Property with id:2 is Transaction:Sale, Category:Residential, Subcategory:Residential_Apartment

Property with id:3 is Transaction:Lease, Category:Commercial, Subcategory:Commercial_Officespace

my current table structure is as follows 我目前的表结构如下

CREATE TABLE IF NOT EXISTS `transaction` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `transactionName` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `categoryName` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `subcategory` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `subCategoryName` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `property` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `transaction_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  `subcategory_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

Edit: 编辑:

My Question is. 我的问题是。

seeing the relationship between them, is it the right way of storing the records? 看到它们之间的关系,这是存储记录的正确方法吗? my main concern here is categories and subcategory table, as because right now i am not able to think about design flaw that may occur in future. 我主要关心的是类别和子类别表,因为现在我无法考虑将来可能发生的设计缺陷。 i just want to know how would you store the records in table if you were told to design something similar. 我只是想知道如果你被告知要设计类似的东西,你将如何将记录存储在表中。 mainly the category and subcategory part. 主要是类别和子类别部分。

If subcategory belongs to one category, it should be enforced with foreign key constraint: 如果子类别属于一个类别,则应使用外键约束强制执行:

CREATE TABLE IF NOT EXISTS `subcategory` ( 
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int not null, 
 `subCategoryName` varchar(50) NOT NULL, 
 PRIMARY KEY (`id`) ,
 CONSTRAINT FK_subcategory_category_id FOREIGN KEY(category_id) 
 REFERENCES category(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

property table should not have category_id , just subcategory_id (which is FK to subcategory.id ) property表不应该有category_id ,只有subcategory_id (这是FK到subcategory.id

It all depends on the business rules for the types. 这一切都取决于类型的业务规则。 I am assuming a couple of rules for the below suggestion, if any are wrong, please let me know: 我对以下建议采取了一些规则,如果有任何错误,请告诉我:

  • A property can have only one transaction type 属性只能有一种事务类型
  • A property can have multiple categories 一个属性可以有多个类别
  • A property can have multiple sub categories 属性可以有多个子类别
  • A category cannot share its sub category with other categories (ie. (this is a bit contrived I know) if you want a subcategory of Residential_ZZZ under Residential, then you must create a totally new subcategory if you want a subcategory of the same name (Residential_ZZZ) under commercial.) 一个类别不能与其他类别共享其子类别(即。(我知道这有点做作)如果你想要在Residential下面的Residential_ZZZ子类别,那么你必须创建一个全新的子类别,如果你想要一个同名的子类别( Residential_ZZZ)商业广告。)

Table design: 表设计:

Property
    ID
    TransactionType_ID

TransactionType
    ID

Category
    ID

This is an inheritance structure btw 这是btw的继承结构

SubCategory
    ID
    CategoryID
    Name

Property_Category_List
    PropertyID
    CategoryID

Property_SubCategory_List
    PropertyID
    SubCategoryID

I think there is a way to clean up the category/sub category, but I cannot think of it at the moment, and it would depend on the business rules really. 我认为有一种方法可以清理类别/子类别,但我现在想不到它,它实际上取决于业务规则。

Under your current schema, you at least needed to have a way to tie the sub category to its existing category (done above by including the CategoryID. Also, your properties can only have one category and sub category since they only have the one column in property. If you plan on having multiple categories or sub categories, then you need the List/Map structure that I created in the last two tables. Those are the only two major changes here 在您当前的架构下,您至少需要一种方法将子类别与其现有类别联系起来(通过包含CategoryID完成上述。此外,您的属性只能有一个类别和子类别,因为它们只有一个列如果你计划有多个类别或子类别,那么你需要我在最后两个表中创建的列表/地图结构。这是这里唯一的两个主要变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM