这是我的T-SQL代码。
CREATE TABLE [dbo].[AspNetUsers]
(
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[UserName] NVARCHAR (256) NOT NULL,
[FirstName] NVARCHAR(50) NOT NULL DEFAULT /*what goes in here*/,
[LastName] NVARCHAR(50) NOT NULL DEFAULT 'LastName',
CONSTRAINT [PK_dbo.AspNetUsers]
PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex]
ON [dbo].[AspNetUsers]([UserName] ASC);
我希望FirstName
和LastName
不被允许为空,但我一直收到错误,说我应该添加默认值,或者我应该允许它们为空(我不会# 39; t想要做什么)。所以我想我会添加默认值。我想获取Id并将其与单词FirstName
和LastName
连接在一起。
例如,如果我的Id
为345,我希望FirstName
和LastName
的默认值分别为(nvarchar类型)345FirstName
和345LastName
那我该怎么做?
答案 0 :(得分:2)
DEFAULT约束必须是常量,不能在DEFAULT定义上下文中使用列名。我认为唯一的方法是使用触发器(我不确定这是不是过度工程)。看一下这个例子:
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[UserName] NVARCHAR (256) NOT NULL,
[FirstName] NVARCHAR(50) NOT NULL,
[LastName] NVARCHAR(50) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE TRIGGER trInsert_AspNetUsers
ON [dbo].[AspNetUsers] INSTEAD OF INSERT
AS
INSERT [dbo].[AspNetUsers]([Id], [Email], [PasswordHash], [UserName], [FirstName], [LastName])
SELECT [Id], [Email], [PasswordHash], [UserName], COALESCE([FirstName], [Id]+'FirstName'), COALESCE([LastName], [Id]+'LastName')
FROM inserted
GO
INSERT dbo.AspNetUsers VALUES('1', '2', '3','4',NULL,NULL)
INSERT dbo.AspNetUsers VALUES('5', '6', '7', '8', 'Paweł', 'Dyl')
SELECT * FROM dbo.AspNetUsers
SELECT返回以下表格:
Id Email PasswordHash UserName FirstName LastName
1 2 3 4 1FirstName 1LastName
5 6 7 8 Paweł Dyl
答案 1 :(得分:1)
您可以将此字段设为可空,并在更新后添加,插入触发器:
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[UserName] NVARCHAR (256) NOT NULL,
[FirstName] NVARCHAR(50) NULL,
[LastName] NVARCHAR(50) NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex]
ON [dbo].[AspNetUsers]([UserName] ASC);
GO
CREATE TRIGGER [UserNameTrigger]
ON [dbo].[AspNetUsers]
AFTER INSERT, UPDATE
AS
UPDATE anu
SET [FirstName] = ISNULL(i.[FirstName],i.Id + 'FirstName'),
[LastName] = ISNULL(i.[LastName],i.Id + 'LastName')
FROM [dbo].[AspNetUsers] anu
INNER JOIN inserted i
ON i.[Id] = anu.Id
GO
因此,如果您运行此查询:
INSERT INTO [dbo].[AspNetUsers]
VALUES
(NEWID(),'example1@example.com','qwerty','username1',NULL,NULL),
(NEWID(),'example2@example.com','qwerty','username2','John',NULL),
(NEWID(),'example3@example.com','qwerty','username3',NULL,'Smith'),
(NEWID(),'example4@example.com','qwerty','username4','Jason','Anderson')
GO
SELECT [Id]
,[Email]
,[PasswordHash]
,[UserName]
,[FirstName]
,[LastName]
FROM [dbo].[AspNetUsers]
GO
你会得到:
Id Email PasswordHash UserName FirstName LastName
07AA1C80-7386-4E48-9556-9D81E2FBFCB0 example2@example.com qwerty username2 John 07AA1C80-7386-4E48-9556-9D81E2FBFCB0LastName
6310E4E6-ADDB-4F50-9D1E-5C756059DB40 example1@example.com qwerty username1 6310E4E6-ADDB-4F50-9D1E-5C756059DB40FirstName 6310E4E6-ADDB-4F50-9D1E-5C756059DB40LastName
6F539B99-2B4C-440E-A96A-8F5D54A9B758 example3@example.com qwerty username3 6F539B99-2B4C-440E-A96A-8F5D54A9B758FirstName Smith
9665747B-A216-46CC-9BA3-159512AAC80C example4@example.com qwerty username4 Jason Anderson