GO
SET ANSI_NULLS ON
GO
--创建
IF OBJECT_ID(N'Groups') IS NOT NULL
BEGIN
PRINT 'This table have been existed';
DROP TABLE Groups;
END
ELSE
BEGIN
CREATE TABLE [dbo].[Groups]
(
[GroupID] SMALLINT IDENTITY(1, 1),
[GroupName] NVARCHAR(50),
[Description] NVARCHAR(100),
CONSTRAINT [PK_Groups_GroupID] PRIMARY KEY(GroupID)
)
END
GO
--添加数据
INSERT INTO dbo.Groups
VALUES ('SuperAdmin', '超级管理员');
INSERT INTO dbo.Groups
VALUES('CusServGroup', '客服部门组');
INSERT INTO dbo.Groups
VALUES('CommonGroup', '普通部门组');
GO
DROP TABLE dbo.Users
CREATE TABLE [dbo].[Users]
(
[UserId] BIGINT IDENTITY(1, 1) NOT NULL ,
[UserName] NVARCHAR(25) NULL ,
[PassWord] NVARCHAR(50) NULL ,
[Sex] BIT NULL ,
[GroupID] SMALLINT ,
CONSTRAINT [PK_Users_UserId] PRIMARY KEY CLUSTERED ( [UserId] ASC ),
CONSTRAINT [FK_Users_Groups_GroupID] FOREIGN KEY(GroupID) REFERENCES Groups(GroupID)
)
GO
ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_Sex] DEFAULT ((0)) FOR [Sex]
GO
CREATE TRIGGER TRG_Users ON dbo.Users
AFTER DELETE
AS
SET IDENTITY_INSERT dbo.Users ON;
INSERT INTO UserHistory
(UserId, UserName, PassWord, Sex, GroupID)
SELECT * FROM deleted
GO
INSERT INTO dbo.Users( UserName, PassWord, Sex, GroupID )
VALUES ( 'Kerry', '312ddfjdf', 1, 1 )
INSERT INTO dbo.Users( UserName, PassWord, Sex, GroupID )
VALUES ( 'test', '312ddfjdf', 0, 3 )
我们用下面的语句复制下表Users,我们具体可以从下图中看到表User与TestUser结构的不同了