在现有数据库中,我想将现有ID更改为UUID上的int
如下所示,该列现在是具有CLUSTERED索引的主键
示例表创建代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblUsers](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[LastName] [nvarchar](100) NOT NULL,
[LoginName] [nvarchar](25) NOT NULL,
[RoleID] [int] NOT NULL,
CONSTRAINT [PK_tblUsers] PRIMARY KEY CLUSTERED
(
[UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
如何快速解决许多表的问题?
表可以有很多行,所以我想知道如何做索引。通常,您不应在UUID上创建索引。
编辑:
另一个引用表的示例:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblUsersDist](
[UserDistID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NOT NULL,
[LocationId] [int] NOT NULL,
CONSTRAINT [PK_tblUsersDist] PRIMARY KEY CLUSTERED
(
[UserDistID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
答案 0 :(得分:0)
您将必须:
PRIMARY KEY
放到原始表格上。uniqueidentifier
数据类型的新列。UUID
值更新新列。NOT NULL
(PRIMARY KEY
所需)。INT
列。UUID
列以匹配原始的INT
列(可选)。PRIMARY KEY
约束。示例:
CREATE TABLE Test (
UserID INT IDENTITY,
Data INT,
CONSTRAINT PK_Test PRIMARY KEY CLUSTERED (UserID))
INSERT INTO Test (Data)
VALUES (10), (20)
BEGIN TRANSACTION
ALTER TABLE Test DROP PK_Test
ALTER TABLE Test ADD UUID uniqueidentifier
UPDATE Test SET UUID = NEWID()
ALTER TABLE Test ALTER COLUMN UUID uniqueidentifier NOT NULL
ALTER TABLE Test DROP COLUMN UserID
EXEC sp_rename 'dbo.Test.UUID', 'UserID', 'COLUMN'
ALTER TABLE Test ADD CONSTRAINT PK_Test PRIMARY KEY (UserID)
COMMIT
请注意,该示例缺少正确的错误处理。此外,如果您有外键,则必须将其放在开头,然后在结尾处重新创建它们,但是 请注意新的数据类型 。
答案 1 :(得分:0)
个人而言,我将创建一个新表,插入旧值,为每个表效仿,然后删除旧表并重命名新表。混乱,但是更改PK的数据类型总是很麻烦。
ResourceNotFound Exception
这当然只是一个模糊的想法,因为我们对您的数据只有一个模糊的想法。