您能否在MySql中使用自动增量而不将其作为主键 [英] Can you use auto-increment in MySql with out it being the primary Key

查看:75
本文介绍了您能否在MySql中使用自动增量而不将其作为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将GUID用作我所有其他表的主键,但是我有一个要求,它需要有一个递增的数字.我试图用自动增量在表中创建一个字段,但是MySql抱怨它必须是主键.

I am using GUIDs as my primary key for all my other tables, but I have a requirement that needs to have an incrementing number. I tried to create a field in the table with the auto increment but MySql complained that it needed to be the primary key.

我的应用程序使用MySql 5,nhibernate作为ORM.

My application uses MySql 5, nhibernate as the ORM.

我想到的可能的解决方案是:

Possible solutions I have thought of are:

  • 将主键更改为自动递增字段,但ID仍为GUID,因此我的应用程序其余部分保持一致.

  • change the primary key to the auto-increment field but still have the Id as a GUID so the rest of my app is consistent.

同时使用GUID和自动增量字段创建复合键.

create a composite key with both the GUID and the auto-increment field.

目前,我的想法趋向于复合关键构想.

My thoughts at the moment are leaning towards the composite key idea.

行ID(主键)当前为GUID.我想添加一个自动递增的INT字段,以使其易于阅读.我只是不想在将GUID用作主键的应用程序中脱离当前标准.

The Row ID (Primary Key) is the GUID currently. I would like to add an an INT Field that is Auto Incremented so that it is human readable. I just didn't want to move away from current standard in the app of having GUID's as primary-keys.

推荐答案

GUID值在表甚至数据库之间都是唯一的,因此,请使auto_increment列为主索引,并为GUID创建UNIQUE索引

A GUID value is intended to be unique across tables and even databases so, make the auto_increment column primary index and make a UNIQUE index for the GUID

这篇关于您能否在MySql中使用自动增量而不将其作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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