在SQL Server中创建唯一标识列。

huangapple go评论62阅读模式
英文:

Create unique identifier column in SQL Server

问题

You can achieve this by using the CONCAT function in SQL. Here's the SQL code to update your Unique_ID to have the prefix 04cust_:

UPDATE [dbo].[my_table]
SET [Unique_ID] = CONCAT('04cust_', [Unique_ID])
WHERE [Unique_ID] = 45;

This code will update the Unique_ID from 45 to '04cust_45' for the table 'april_customers'.

英文:
CREATE TABLE [dbo].[my_table]
(
    [V01_V] [real] NULL,
    [V02_V] [real] NULL,
    [V03_V] [real] NULL,
    [V04_V] [real] NULL,
    [V05_V] [real] NULL
    [Date_Inserted] [datetime] NOT NULL,
    [Unique_ID] [int] IDENTITY(1,1) NOT NULL,

    CONSTRAINT [PK_row_id] 
        PRIMARY KEY CLUSTERED ([Unique_ID] ASC)
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                          ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

I currently have Unique_ID which just captures the row number in the table. I am trying to add table information to these records, so it's easier to identify the table they are coming from.

  • Table name: april_customers
  • Current Unique_ID: 45
  • Goal for Unique_ID: 04cust_45

How do I add 04cust_ to my current Unique_ID?

答案1

得分: 1

你需要添加一个计算列来生成这样的ID

CREATE TABLE [dbo].[my_table](
    [V01_V] [real] NULL,
    [V02_V] [real] NULL,
    [V03_V] [real] NULL,
    [V04_V] [real] NULL,
    [V05_V] [real] NULL,
    [Date_Inserted] [datetime] NOT NULL,
    [Unique_ID] [int] IDENTITY(1,1) NOT NULL,
    CONSTRAINT [PK_row_id] PRIMARY KEY CLUSTERED
(
    [Unique_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO [dbo].[my_table] values (1,1,1,1,1, CURRENT_TIMESTAMP  ),(2,2,2,2,2, CURRENT_TIMESTAMP  )
ALTER TABLE dbo.my_table ADD new_id AS ('04cust_' + CONVERT(varchar(16),Unique_ID));
SELECT * FROM [dbo].[my_table] 
V01_V V02_V V03_V V04_V V05_V Date_Inserted Unique_ID new_id
1 1 1 1 1 2023-03-20 22:37:27.493 1 04cust_1
2 2 2 2 2 2023-03-20 22:37:27.493 2 04cust_2

fiddle

当然,如果将来遇到类似的问题,你也可以添加一个UUID。

英文:

You would add a computed column to generate such an id

CREATE TABLE [dbo].[my_table](
    [V01_V] [real] NULL,
    [V02_V] [real] NULL,
    [V03_V] [real] NULL,
    [V04_V] [real] NULL,
    [V05_V] [real] NULL,
    [Date_Inserted] [datetime] NOT NULL,
    [Unique_ID] [int] IDENTITY(1,1) NOT NULL,
    CONSTRAINT [PK_row_id] PRIMARY KEY CLUSTERED
(
    [Unique_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO [dbo].[my_table] values (1,1,1,1,1, CURRENT_TIMESTAMP  ),(2,2,2,2,2, CURRENT_TIMESTAMP  )
ALTER TABLE dbo.my_table ADD new_id AS ('04cust_' + CONVERT(varchar(16),Unique_ID));
SELECT * FROM [dbo].[my_table] 
V01_V V02_V V03_V V04_V V05_V Date_Inserted Unique_ID new_id
1 1 1 1 1 2023-03-20 22:37:27.493 1 04cust_1
2 2 2 2 2 2023-03-20 22:37:27.493 2 04cust_2

fiddle

Of course you could add a uuid also, if you such problems in future

huangapple
  • 本文由 发表于 2023年3月21日 01:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定