数据库主键必须为整数吗? [英] Must database primary keys be integers?

查看:186
本文介绍了数据库主键必须为整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是看到MySQL数据库主键是整数.是因为主键必须是整数,还是因为在列上设置auto_increment时易于使用?

I always see MySQL database primary keys as integers. Is that because primary keys must be integers, or because of ease of use when setting auto_increment on the column?

我想知道,以防万一我将来希望我的主键成为varchar.

I am wondering just in case I want my primary key to be a varchar in the future.

推荐答案

您可以使用varchar,只要您确保每个人都是唯一即可.但是,这并不理想(有关更多信息,请参见下面的文章链接).

You can use varchar as well as long as you make sure that each one is unique. This however isn't ideal (see article link below for more info).

您要查找的内容称为自然键,但是由RDBMS处理并具有自动递增功能的主键称为代理键,这是首选方式.因此,您需要将其设置为整数.

What you are looking for is called natural key but a primary key with auto-increment and handled by the RDBMS is called surrogate key which is preferred way. Therefore you need to have it to be integer.

了解详情:

  • Surrogate Keys vs Natural Keys for Primary Key?
  • Why I prefer surrogate keys instead of natural keys in database design

这篇关于数据库主键必须为整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆