如何设计这个数据库布局?
我正在开发一个类似于 Craigslist 的网站,用户可以在该网站上发帖并在不同的城市销售商品。我的网站和 Craigslist 之间的一个区别是您将能够通过邮政编码进行搜索,而不是在页面上列出所有城市。
我已经有了邮政编码数据库,其中包含每个城市的所有城市、州、纬度、经度和邮政编码信息。好的,现在深入了解我需要做什么以及需要帮助:
虽然我有邮政编码数据库,但它的设置并不适合我的使用。 (我从http://zips.sourceforge.net/免费从互联网下载了它)
我需要帮助设置我的数据库结构(例如我应该使用多少个不同的表以及我应该如何链接它们)。
我需要帮助设置我的数据库结构(例如
我将使用 PHP 和 MySQL。
到目前为止,我对如何设置数据库的想法是:(虽然我不确定这是否可行。)
场景
有人进入主页,它会告诉他们,“请输入您的邮政编码。”。例如,如果他们输入“17241”,则该邮政编码适用于位于宾夕法尼亚州名为纽维尔的城市。使用当前数据库设置,查询将如下所示:
SELECT city FROM zip_codes WHERE zip = 17241;
查询结果将是“Newville”。我现在在这里看到的问题是,当他们想要在网站的纽维尔部分发布某些内容时,我必须为纽维尔市的帖子设置整个表格。有超过 42,000 个城市,这意味着我必须有超过 42,000 张桌子(每个城市一张),所以必须这样做是疯狂的。
我考虑的一种方法是在邮政编码数据库中添加一列名为“city_id”的列,它是分配给每个城市的唯一编号。例如,纽维尔市的 city_id 为 83。现在,如果有人来并在纽维尔市发布列表,我只需要另一张表。另一张表的设置如下:(
CREATE TABLE postings (
posting_id INT NOT NULL AUTO_INCREMENT,
for_sale LONGTEXT NULL,
for_sale_date DATETIME NULL,
for_sale_city_id INT NULL,
jobs LONGTEXT NULL,
jobs_date DATETIME NULL,
jobs_city_id INT NULL,
PRIMARY KEY(posting_id)
);
for_sale 和 job_column 名称是用户可以在下面列出的帖子类型的类别。会有很多类别不止这两个,但这只是示例。)
因此,现在,当有人访问该网站并且他们正在寻找要购买而不是出售的东西时,他们可以输入他们的邮政编码,例如 17241,这是将运行的查询:(
SELECT city, city_id FROM zip_codes WHERE zip = 17241; //Result: Newville 83
我将使用 PHP 来存储用户在会话和 Cookie 中输入的邮政编码,以便在整个站点中记住它们)
现在它将告诉他们,“请选择您的类别。”。如果他们选择“待售物品”类别,那么这是运行查询并对结果进行排序:
SELECT posting_id, for_sale, for_sale_date FROM postings WHERE for_sale_city_id = $_SESSION['zip_code'];
所以现在我的问题是,这行得通吗?我很确定它会,但我不想设置这个东西并意识到我忽略了一些东西并且必须从头开始。
I am working on a site similar to Craigslist where users can make postings and sell items in different cities. One difference between my site and Craigslist will be you will be able to search by zip code instead of having all of the cities listed on the page.
I already have the ZIP Code database that has all of the city, state, latitude, longitude, and zip code info for each city. Okay, so to dive into what I need done and what I need help with:
Although I have the ZIP Code database, it is not setup perfectly for my use. (I downloaded it from the internet for free from http://zips.sourceforge.net/)
I need help setting up my database structure (e.g. how many different tables should I use and how should I link them).
I will be using PHP and MySQL.
These our my thoughts so far on how the database can be setup: (I am not sure if this will work though.)
Scenario
Someone goes to the homepage and it will tell them, "Please enter your ZIP Code.". If they enter "17241" for example, this ZIP Code is for a city named Newville located in Pennsylvania. The query would look like this with the current database setup:
SELECT city FROM zip_codes WHERE zip = 17241;
The result of the query would be "Newville". The problem I see here now is when they want to post something in the Newville section of the site, I will have to have an entire table setup just for the Newville city postings. There are over 42,000 cities which means I would have to have over 42,000 tables (one for each city) so that would be insane to have to do it that way.
One way I was thinking of doing it was to add a column to the ZIP Code database called "city_id" which would be a unique number assigned to each city. So for example, the city Newville would have a city_id of 83. So now if someone comes and post a listing in the city Newville I would only need one other table. That one other table would be setup like this:
CREATE TABLE postings (
posting_id INT NOT NULL AUTO_INCREMENT,
for_sale LONGTEXT NULL,
for_sale_date DATETIME NULL,
for_sale_city_id INT NULL,
jobs LONGTEXT NULL,
jobs_date DATETIME NULL,
jobs_city_id INT NULL,
PRIMARY KEY(posting_id)
);
(The for_sale and job_ column names are categories of the types of postings users will be able to list under. There will be many more categories than just those two but this is just for example.)
So now when when someone comes to the website and they are looking for something to buy and not sell, they can enter their ZIP Code, 17241, for example, and this is the query that will run:
SELECT city, city_id FROM zip_codes WHERE zip = 17241; //Result: Newville 83
(I will be using PHP to store the ZIP Code the user enters in SESSIONS and Cookies to remember them throughout the site)
Now it will tell them, "Please choose your category.". If they choose the category "Items For Sale" then this is the query to run and sort the results:
SELECT posting_id, for_sale, for_sale_date FROM postings WHERE for_sale_city_id = $_SESSION['zip_code'];
So now my question is, will this work? I am pretty sure it will but I do not want to set this thing up and realize I overlooked something and have to start from all over from scratch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是您所要求的,但看到这个结构告诉我您需要创建一个相关的表(以及一个可供选择的值的查找表),而不是这样做。如果您在表格中重复列出相同的内容,则需要一个相关的表格。我将有 posts 和 Posting_type (因为您希望每个帖子列出不止一种类型。更像是这种结构。
这使您可以自由地添加任意数量的类别,而无需更改表结构。
This isn't what you asked but seeing this structure tells me you need to create a related table (And a lookup table for the values to choose from) instead of doing things this way. If you are listing the same things repeatedly in a table, you need a related table. I would have postings and Posting_type (since you have more than one type you want listed per posting. Something more like this structure.
This gives you the freedom to add as many categories as you like without changing the table structures.
我不会为每种类型的列表都有一组列,而是一个“posting_type”列:
I wouldn't have a group of columns for every type of listing, but a "posting_type" column:
我会考虑像 mongodb 这样的 nosql 解决方案。除了城市-邮政编码关系之外,您还想在此处捕获任何真正的关系约束吗?
看来这里的答案是否定的。
@Konerak 是对的,开始一些事情并从那里开始构建。
I'd consider a nosql solution here like mongodb. Are there any real relational constraints you want to capture here aside from the city-zip relationship?
It seems like the answer would be no here.
@Konerak is right, get something started and build from there.