MySQL美国参议员名单数据库设计
我正在设计一个表格,仅保存现任美国参议员的列表和一些相关信息(其中大部分信息是我从出色的纽约时报国会 API 中获得的)。为代表创建表很容易,因为我刚刚在他们的州
和区号
上创建了一个主键
。不过,这让我很困惑。
我需要每晚更新此表中的信息,并将执行INSERT...ON DUPLICATE KEY UPDATE
。
关于如何设计这个有什么想法吗?
I'm designing a table that simply keeps a list of current US Senators and some related information (much of which I'm getting from the awesome NY Times Congress API). Creating the table for Representatives was easy, because I just created a PRIMARY KEY
on their state
and district number
. This has stumped me, though.
I need to be updating the information in this table nightly, and will be doing an INSERT...ON DUPLICATE KEY UPDATE
.
Any thoughts on how to go about designing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
UPDATE
而不是插入,首先通过查询复合键值来检查记录是否存在。您没有说明您正在使用什么数据库,但 SQL Server 2008 有一个 MERGE 语句可以非常轻松地完成此操作。
Use an
UPDATE
rather than an insert, checking first if the record exists by querying for the composite key values.You don't say what database you're using, but SQL Server 2008 has a
MERGE
statement that accomplishes this very easily.如果问题是使用什么作为主键,为什么不将名字、姓氏和州作为主键呢?该公约适用于参议员和众议员。
If the question is what to use for the primary key, why not make the primary key on the first name, last name, and state? That convention could be used for both senators and representatives.
不要使用 INSERT 来更新现有数据,而使用 UPDATE 和您的 PK 来更新现有数据。
你在其他地方说你在选择PK时遇到困难,因为每个州都有两名参议员,而你不知道如何区分他们。美国参议员分为三个类别,并且来自一个州的两名参议员不会属于同一类别,因此你的PK可能是状态和阶级。
Don't use INSERT to update existing data, use UPDATE and your PK for that.
You say elsewhere that you're having trouble choosing a PK because each state has two senators and you don't know how to differentiate them. US senators are grouped into three classes and no two senators from a single state are in the same class so your PK could be state and class.