简体   繁体   English

SQL 中是否始终需要 ID 列?

[英]Is ID column always required in SQL?

to be more specific, I've created a news module with a tag system.更具体地说,我创建了一个带有标签系统的新闻模块。 As each tag is unique (you are not allowed, as an admin, to create 2 identical tags), is the id column still useful?由于每个标签都是唯一的(作为管理员,您不允许创建 2 个相同的标签),id 列仍然有用吗? I guess no, but I was wondering about the performances.我想没有,但我想知道表演。

id |编号 | mews_title | mews_title | date... <-------------> news_id |日期... <-------------> news_id | tag_id <----------> id | tag_id <----------> id | tag_name标签名

VS VS

id |编号 | mews_title | mews_title | date... <-------------> news_id |日期... <-------------> news_id | tag_tag_name <----------> tag_name tag_tag_name <----------> tag_name

Thanks a lot!非常感谢!

The performance difference is insignificant.性能差异是微不足道的。

Advantages to using a numeric id for the tags in your example would be:在您的示例中为标签使用数字id的优点是:

  • to make the intersection table somewhat smaller because integers are smaller on average than a string使交集表稍微小一些,因为整数平均小于字符串
  • to allow changing the spelling of a tag name by updating one row instead of many rows允许通过更新一行而不是多行来更改标签名称的拼写

These may not be important considerations for your case.对于您的情况,这些可能不是重要的考虑因素。 So no, it's not required to use a numeric id .所以不,不需要使用数字id

I also wrote about this in a chapter titled "ID Required" in my book, SQL Antipatterns: Avoiding the Pitfalls of Database Programming .我还在我的书SQL Antipatterns: Avoiding the Pitfalls of Database Programming中题为“需要 ID”的一章中写到了这一点。

Numeric IDs are not required (You could have a table without a primary key), but they are useful:数字 ID 不是必需的(您可以有一个没有主键的表),但它们很有用:

  • Without and ID column, if you want to change a tag_name in the future (because for example it is misspelled), you will have to update all the tables that have this foreign key (tag_tag_name).如果没有和 ID 列,如果您想在将来更改 tag_name(例如因为拼写错误),则必须更新所有具有此外键 (tag_tag_name) 的表。 If you have and ID column you only will have to change the tag_name in one place.如果您有 ID 列,则只需在一处更改 tag_name。
  • They could be auto generated (Autoincremental).它们可以自动生成(自动增量)。
  • It could take up less space.它可以占用更少的空间。 For example when the natural primary key is composite and is the foreign key in another table, and id (one integer column) primary key take up less space.例如,当自然主键是复合的并且是另一个表中的外键时,id(一个 integer 列)主键占用的空间较小。
  • It's easier make joins with other tables (If the primary key has only a column than if it has more than one).与其他表进行连接更容易(如果主键只有一列而不是多列)。
  • if all yours table's primary key is the ID field, you don't have to remember others field names when you make joins, your data schema syntax will be consistent.如果所有表的主键都是 ID 字段,则在进行连接时不必记住其他字段名称,您的数据模式语法将保持一致。

You can specify the tag_name as unique, so two tag_name equals couldn't be inserted.您可以将 tag_name 指定为唯一的,因此无法插入两个等于 tag_name。

You don't even need a table of tags.您甚至不需要标签表。 Just a table with只是一张桌子

news_id | tag_name

This is a many-to-many mapping between news_ids and tags.这是 news_ids 和标签之间的多对多映射。 Since there is no tag table, the issue of duplicates vanishes.由于没有标签表,重复的问题就消失了。 The same tag ( tag_name ) can be associated with many news articles, but that is what you want .同一个标签 ( tag_name ) 可以与许多新闻文章相关联,但这正是您想要的。

INSERT INTO Tags (news_id, tag_name) -- change this to INSERT IGNORE INTO Tags (news_id, tag_name) so you don't have to worry about getting the same news article tagged the same way twice. INSERT INTO Tags (news_id, tag_name) -- 将其更改为INSERT IGNORE INTO Tags (news_id, tag_name) ,这样您就不必担心以相同方式标记相同的新闻文章两次。 And have并且有

PRIMARY KEY(news_id, tag_name), -- uniqueness and lookup tags for one article
INDEX(tag_name, news_id)  -- For finding all articles for one tag

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

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