建立sqlserver資料庫指令碼 範例

iSQlServer發表於2010-09-06

下面是建立一個sqlserver資料庫的程式碼模板,加上一個建立表的模板。開發的時候可以拷貝過去直接改動一下就可以用了。

希望能幫上忙!

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtUSE [master]
GO

IF EXISTS(SELECT 1 FROM sysdatabases WHERE NAME=N'HkTemp')
BEGIN
DROP DATABASE HkTemp --如果資料庫存在先刪掉資料庫
END
GO

CREATE DATABASE HkTemp
ON
PRIMARY --建立主資料庫檔案
(
NAME
='HkTemp',
FILENAME
='E:\Databases\HkTemp.dbf',
SIZE
=5MB,
MaxSize
=20MB,
FileGrowth
=1MB
)
LOG ON --建立日誌檔案
(
NAME
='HkTempLog',
FileName
='E:\Databases\HkTemp.ldf',
Size
=2MB,
MaxSize
=20MB,
FileGrowth
=1MB
)
GO
--新增表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'Hksj_User') AND type in (N'U'))
BEGIN
CREATE TABLE Hksj_User
(
Id
INT IDENTITY(1,1) NOT NULL,
SName
NVARCHAR(20) NOT NULL,
SNickName
NVARCHAR(20),
SPassWord
NVARCHAR(30) NOT NULL,
DCreateDate
DATETIME ,
SCreator
NVARCHAR(20),
SEmail
NVARCHAR(50),
SPhone
NVARCHAR(50),
SIdentifyId
NVARCHAR(30),
DLastTimeLogOn
DATETIME

PRIMARY KEY CLUSTERED
(
Id
ASC
)
WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
)
ON [PRIMARY]
END
GO
--新增註釋
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'登入名' ,  @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Hksj_User', @level2type=N'COLUMN',@level2name=N'SName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'郵箱' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Hksj_User', @level2type=N'COLUMN',@level2name=N'SEmail'

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-672788/,如需轉載,請註明出處,否則將追究法律責任。

相關文章