SQL Server使用介紹(一)

北大青鳥通州校區(qū)學(xué)術(shù)部提供: 

相信做軟件開發(fā)的朋友很多會(huì)使用到數(shù)據(jù)庫(kù),這里我就簡(jiǎn)單的介紹一下SQL Server數(shù)據(jù)庫(kù)的使用。

第一個(gè)命令:Create DataBase

創(chuàng)建一個(gè)新數(shù)據(jù)庫(kù)及存儲(chǔ)該數(shù)據(jù)庫(kù)的文件,創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)快照,或從先前創(chuàng)建的數(shù)據(jù)庫(kù)的已分離文件中附加數(shù)據(jù)庫(kù)。

簡(jiǎn)言之就是創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),下面介紹3種創(chuàng)建數(shù)據(jù)庫(kù)的方法

 

1.偷懶法

--切換到master數(shù)據(jù)庫(kù)
use master 
go
--
檢查是否存在ACCP數(shù)據(jù)庫(kù),存在的話刪除ACCP數(shù)據(jù)庫(kù)
if exists(select * from sysdatabases where name='ACCP')
 drop database ACCP
go
--
創(chuàng)建數(shù)據(jù)庫(kù)
create database ACCP
go

--切換到ACCP數(shù)據(jù)庫(kù)
use ACCP
go

 

2.需要設(shè)置相關(guān)參數(shù)

--切換到master數(shù)據(jù)庫(kù)
use master 
go
--
檢查是否存在ACCP數(shù)據(jù)庫(kù),存在的話刪除ACCP數(shù)據(jù)庫(kù)
if exists(select * from sysdatabases where name='ACCP')
 drop database ACCP
go
--
創(chuàng)建數(shù)據(jù)庫(kù)
create database ACCP
on primary        --
主要數(shù)據(jù)庫(kù)文件
(
 name ='ACCP_DB',     --
邏輯名
 filename='e:\accp\accpp_db.mdf', --
數(shù)據(jù)庫(kù)文件存放位置
 size = 5MB,       --
數(shù)據(jù)文件初始大小
 filegrowth = 1MB,     --
文件增長(zhǎng)方式,5MB寫滿了就增長(zhǎng)1MB
 maxsize = 100MB      --
文件最大限制,不寫的話就沒(méi)有限制,寫滿硬盤為止
)
log on         --
日志文件,參數(shù)可以參考上面的
(
 name ='ACCP_LOG',
 filename='e:\accp\accpp_log.ldf',
 size = 4MB,
 filegrowth = 10%     --
文件增長(zhǎng)方式,4MB寫滿了就增長(zhǎng)10%,最后一個(gè)參數(shù)不要加逗號(hào)
)
go

--切換到ACCP數(shù)據(jù)庫(kù)
use ACCP
go

3.需要多個(gè)數(shù)據(jù)庫(kù)文件

--切換到master數(shù)據(jù)庫(kù)
use master 
go
--
檢查是否存在ACCP數(shù)據(jù)庫(kù),存在的話刪除ACCP數(shù)據(jù)庫(kù)
if exists(select * from sysdatabases where name='ACCP')
 drop database ACCP
go
--
創(chuàng)建數(shù)據(jù)庫(kù)
create database ACCP
on primary        --
主要數(shù)據(jù)庫(kù)文件
(
 name ='ACCP_DB1',     --
邏輯名
 filename='e:\accp\accpp_db.mdf', --
數(shù)據(jù)庫(kù)文件存放位置
 size = 5MB,       --
數(shù)據(jù)文件初始大小
 filegrowth = 1MB,     --
文件增長(zhǎng)方式,5MB寫滿了就增長(zhǎng)1MB
 maxsize = 100MB      --
文件最大限制,不寫的話就沒(méi)有限制,寫滿硬盤為止
),          --
次要數(shù)據(jù)庫(kù)文件可有可無(wú),可有多個(gè)
(
 name ='ACCP_DB2',     --
邏輯名
 filename='e:\accp\accpp_db.ndf', --
數(shù)據(jù)庫(kù)文件存放位置
 size = 5MB,       --
數(shù)據(jù)文件初始大小
 filegrowth = 1MB,     --
文件增長(zhǎng)方式,5MB寫滿了就增長(zhǎng)1MB
 maxsize = 100MB      --
文件最大限制,不寫的話就沒(méi)有限制,寫滿硬盤為止
)
log on         --
日志文件,參數(shù)可以參考上面的,日志文件可以寫多個(gè),至少有1個(gè)
(
 name ='ACCP_LOG1',
 filename='e:\accp\accpp_log1.ldf',
 size = 4MB,
 filegrowth = 10%     --
文件增長(zhǎng)方式,4MB寫滿了就增長(zhǎng)10%,最后一個(gè)參數(shù)不要加逗號(hào)
),
(
 name ='ACCP_LOG2',
 filename='e:\accp\accpp_log2.ldf',
 size = 4MB,
 filegrowth = 10%
)
go

--切換到ACCP數(shù)據(jù)庫(kù)
use ACCP
go

注意:數(shù)據(jù)庫(kù)的邏輯名不可以重名,filename的值也不可以一樣,否則重名了么。
這樣呢,一個(gè)數(shù)據(jù)庫(kù)就創(chuàng)建好了。

北大青鳥網(wǎng)上報(bào)名
北大青鳥招生簡(jiǎn)章