开发者

Does it make sense to use a varchar column as foreignkey reference?

开发者 https://www.devze.com 2023-03-27 22:43 出处:网络
Given two tables books and authors where each of them already have a unique key which is of varchar type which identifys them throughout my ecosystem. Does it make sense to use those keys as Foreignke

Given two tables books and authors where each of them already have a unique key which is of varchar type which identifys them throughout my ecosystem. Does it make sense to use those keys as Foreignkeys in the Many-To-Many-Table or should I invent a primary key in the tables authors and books which is of type int to avoid overhead? The length of th开发者_如何学编程ose keys already existing is not more then 255.


I always prefer surrogate keys. It protects you from unkown changes in the future, because it is decoupled from application data.


int will be the better way. you may have a large effort to redo existing data however - which may influence you to not make the change...

i would suggest that making this change earlier rather than later will be easier for you in the long run.


Creating a new Primary Key is a good idea, as a primary key also enforces a not null constraint.


I think the better way is to use int key as primary key. Firstly - you will avoid overhead, secondly - you will gain some performance.


While I also think int keys are better, I don't think you should make any changes to your primary keys as it already works. Your varchar keys may make sense if it is, for exemple, ISBN codes for the books. If it's not broken, don't fix it.

0

精彩评论

暂无评论...
验证码 换一张
取 消