简体   繁体   中英

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.

B) the properties are divided into categories and transaction type (sale, rent, lease) etc.

c) categories is divided into subcategories.

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 )

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.)

Table design:

Property
    ID
    TransactionType_ID

TransactionType
    ID

Category
    ID

This is an inheritance structure 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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