简体   繁体   English

在 SQL 中是否可以创建一个以特定字母开头并后跟特定数量数字的串行主键?

[英]In SQL is it possible to create a serial primary key that begins with a specific letter and is followed by a specific amount of numbers?

I have many lines of existing data which seems to use a unique identifier column that begins with the letter K and is followed by 8 or 9 random numbers.我有很多行现有数据,这些数据似乎使用以字母 K 开头并后跟 8 或 9 个随机数的唯一标识符列。 I would like to use this column as the primary key in a table.我想将此列用作表中的主键。 For all future additions to the table I'd like to automatically generate a unique PK that follows the same format as the existing data.对于将来添加到表中的所有内容,我想自动生成一个唯一的 PK,它遵循与现有数据相同的格式。 Is there a way to achieve this within SQL code, or would I need to create an elaborate (for my beginner level) Python function that generates the unique PK?有没有办法在 SQL 代码中实现这一点,或者我是否需要创建一个精心设计的(对于我的初学者级别) Python function 来生成唯一的 PK?

I would leave the existing rows as they are and continue with a different scheme for future rows, for example a plain number without letter (cast to a string).我会保留现有的行,并继续为未来的行使用不同的方案,例如没有字母的纯数字(转换为字符串)。 That can be generated with a sequence conveniently, and there is no risk of collisions.这可以方便地用序列生成,并且没有冲突的风险。

Simplicity over perceived beauty!简单胜过感知美!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM