数据库设计Primay键,ID vs String [英] Database Design Primay Key, ID vs String
问题描述
我目前正计划开发音乐流应用程式。我想知道什么是更好的作为主键在我的表在服务器上。一个ID int或一个唯一字符串。
I am currently planning to develop a music streaming application. And i am wondering what would be better as a primary key in my tables on the server. An ID int or a Unique String.
方法1:
Methods 1:
b $ b
歌曲表:
SongID (int),标题(字符串),*艺术家**(字符串),长度
Songs Table: SongID(int), Title(string), *Artist**(string), Length(int), *Album**(string)
类型表
类型(字符串),名称(字符串)
Genre Table Genre(string), Name(string)
SongGenre:
*** SongID ****(int),*** Genre ****(string)
SongGenre: ***SongID****(int), ***Genre****(string)
方法2
Method 2
歌曲表:
SongID ),* ArtistID **(int),Length(int),* AlbumID **(int)
Songs Table: SongID(int), Title(string), *ArtistID**(int), Length(int), *AlbumID**(int)
类型表
GenreID (int),Name(string)
Genre Table GenreID(int), Name(string)
SongGenre:
*** SongID ****(int),*** GenreID **** (int)
SongGenre: ***SongID****(int), ***GenreID****(int)
键:粗体 =主键,*字段** =外键
Key: Bold = Primary Key, *Field** = Foreign Key
我目前正在设计使用方法2,因为我相信它会加快查找性能和使用更少的空间,因为int比字符串少很多的空间。
I'm currently designing using method 2 as I believe it will speed up lookup performance and use less space as an int takes a lot less space then a string.
有什么理由这不是一个好主意吗?有什么我应该注意吗?
Is there any reason this isn't a good idea? Is there anything I should be aware of?
推荐答案
你正在做正确的事情 - identity字段应该是数字, ,用于空间节省和性能原因(字符串上的匹配键比整数上的匹配慢)。
You are doing the right thing - identity field should be numeric and not string based, both for space saving and for performance reasons (matching keys on strings is slower than matching on integers).
这篇关于数据库设计Primay键,ID vs String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!