日期:2014-05-18 浏览次数:20740 次
-- table dbo.Users
if OBJECT_ID('Users') is not null
drop table Users
go
CREATE TABLE [dbo].[Users](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[Id] 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
ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_Id] DEFAULT (newid()) FOR [Id]
GO
insert into dbo.Users(Name)
select 'test1' union all
select 'test2' union all
select 'test3'
-- table dbo.Users end
-- table dbo.Letters
if OBJECT_ID('Letters') is not null
drop table Letters
go
CREATE TABLE [dbo].[Letters](
[Id] [uniqueidentifier] NOT NULL,
[SenderId] [uniqueidentifier] NOT NULL,
[ReceiverId] [uniqueidentifier] NOT NULL,
[Content] [nvarchar](50) NOT NULL,
[CreatedAt] [datetime] NOT NULL,
CONSTRAINT [PK_Letters] PRIMARY KEY CLUSTERED
(
[Id] 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
ALTER TABLE [dbo].[Letters] ADD CONSTRAINT [DF_Letters_Id] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[Letters] ADD CONSTRAINT [DF_Letters_CreatedAt] DEFAULT (getdate()) FOR [CreatedAt]
GO
declare @test1 uniqueidentifier
declare @test2 uniqueidentifier
declare @test3 uniqueidentifier
select @test1 = id from dbo.users where name = 'test1'
select @test2 = id from dbo.users where name = 'test2'
select @test3 = id from dbo.users where name = 'test3'
insert into dbo.Letters(senderId, receiverId, [content])
select @test1, @test2, 'test1 to test2' union all
select @test2, @test1, 'test2 to test1' union all
select @test1, @test3, 'test1 to test3' union all
select @test1, @test3, 'test1 to test3' union all
select @test2, @test3, 'test2 to test3' union all
select @test3, @test1, 'test3 to test1' union all
select @test2, @test1, 'test2 to test1'
-- table dbo.Users
if OBJECT_ID('Users') is not null
drop table Users
go
CREATE TABLE [dbo].[Users](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[Id] 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
ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_Id] DEFAULT (newid()) FOR [Id]
GO
insert into dbo.Users(Name)
select 'test1' union all
select 'test2' union all
select 'test3'
-- table dbo.Users end
-- table dbo.Letters
if OBJECT_ID('Letters') is not null
drop table Letters
go
CREATE TABLE [dbo].[Letters](
[Id] [uniqueidentifier] NOT NULL,
[SenderId] [uniqueidentifier] NOT NULL,
[ReceiverId] [uniqueidentifier] NOT NULL,
[Content] [nvarchar](50) NOT NULL,
[CreatedAt] [datetime] NOT NULL,
CONSTRAINT [PK_Letters] PRIMARY KEY CLUSTERED
(
[Id] 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
ALTER TABLE [dbo].[Letters] ADD CONSTRAINT [DF_Letters_Id] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[Letters] ADD CONSTRAINT [DF_Letters_CreatedAt] DEFAULT (getdate()) FOR [CreatedAt]
GO
declare @test1 uniqueidentifier
declare @test2 uniqueidentifier
declare @test3 uniqueidentifier
select @test1 = id from dbo.users