简体   繁体   中英

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. 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. 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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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