SQL Server身份对应问题
我正在使用 SQL Server 并且我想在其中使用身份约束
我知道如何以下列方式使用它
create table mytable
(
c1 int primary key identity(1,1);
)
上面的代码工作正常但是如果我希望身份列的值为 EMP001、EMP002...而不是1,2...
提前致谢, 大师
I'm using SQL Server and I want to use identity constraint in it
I know how to use it in following manner
create table mytable
(
c1 int primary key identity(1,1);
)
the above code works fine but what if i want the identity column to have values as EMP001, EMP002,... instead of 1,2....
Thanks in advance,
Guru
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标识列只能是整数。如果您希望它“看起来像”EMP001、EMP002 等,那么这只是一个显示问题,而不是存储问题(也就是说,您不需要存储“EMP001”、“EMP002”等,只需通过普通身份列将其存储为 1, 2, 3 并在应用程序中显示为“EMP001”、“EMP002”等)
Identity columns can only be integers. If you want it to "look like" EMP001, EMP002, etc then that's simply a display issue and not a storage one (that is, you don't need to store "EMP001", "EMP002" etc, just store it as 1, 2, 3 via a normal identity column and display it in your application as "EMP001", "EMP002", etc)
创建一个像这样连接的计算列:
'EMP' + RIGHT('00' + CAST(c1 AS varchar(3)), 3)
否则,作为代理键的 IDENTITY 列就是:无意义的数字。
我假设你只会有 999 行,或者在某个地方还有另一个序列吗?
Create a computed column that concatenates like this:
'EMP' + RIGHT('00' + CAST(c1 AS varchar(3)), 3)
Otherwise an IDENTITY column as surrogate key is just that: a meaningless number.
I assume you're only going to have 999 rows or is there another sequence somewhere?