简体   繁体   English

数据库拆分; 多个表

[英]database splitting; multiple tables

I am coding a classifieds ad web app. 我正在编写分类广告网络应用程序。

What is the optimal way to structure the database for this? 为此构建数据库的最佳方法是什么? Because of the high repeatability, would it be faster (in terms of searching/indexing) to have a separate table in the database for each city? 由于重复性高,在每个城市的数据库中有一个单独的表会更快(就搜索/索引而言)吗?

Or would it be okay to just have one table for every city (it would have a lot of rows..). 还是每个城市只有一张桌子(行数很多。)。

The classifieds table has id, user_id, city_name, category,[description and detail fields]. 分类表具有id,user_id,city_name,类别,[描述和详细信息字段]。

Rather than creating a seperate table for each City, I would create the following tables: 与其为每个城市创建一个单独的表,不如创建以下表:

Advert
Category
City
User

Advert will have Foreign Keys to Category, City and User. 广告将具有类别,城市和用户的外键。 It will also contain details about the item. 它还将包含有关该项目的详细信息。

City will have an ID and all of the necessary Cities. 城市将具有ID和所有必要的城市。 The ID will be the Foreign Key for the Advert table. 该ID将作为Advertise表的外键。

Category will be the same as City with Category details. 类别将与具有类别详细信息的城市相同。

User will have an ID and details of the user. 用户将具有用户的ID和详细信息。 The ID will be the Foreign Key for the Advert table. 该ID将作为Advertise表的外键。

You should break up the elements in your table into 4 tables. 您应该将表中的元素分成4个表。

The first table, a city table, would have city_id and city_name. 第一个表(城市表)将具有city_id和city_name。 This table would need a unique index on city_name, city_id, as well as a primary index on city_id. 该表将需要在city_name,city_id上的唯一索引以及在city_id上的主索引。

The second table, a category table, would have category_id, and category. 第二个表是类别表,将具有category_id和category。 This table would need a unique index on category, category_id, as well as a primary index on category_id. 该表将需要在category_id类别上具有唯一索引,并在category_id上具有主要索引。

The third table, a user table, would have user_id, user identification. 第三个表,一个用户表,将具有user_id,即用户标识。

The fourth table, an ad table, would have ad_id, title, description, and date_inserted. 第四个表格是广告表格,其中包含ad_id,标题,说明和date_inserted。 City_id, category_id, and user_id would be foreign keys to your ad table, City_id,category_id和user_id将是您广告表的外键,

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

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