天天看點

Microsoft SQL Server學習(二)

目錄

關于資料庫的文法:

1.建立資料庫

create database 資料庫名

on primary

(主檔案屬性(name,filename,size等))

  • -用逗号隔開次要主要檔案和次要檔案

    (次要檔案屬性(name,filename,size等))

    log on

    (日志檔案屬性(name,filename,size等))

樣例:

create database student on primary( name="student", filename="E:\SQL_test\student", size=5MB, maxsize=20MB, filegrowth=1MB ) log on( name="studeng_log", filename="E:\SQL_test\student_log", size=3MB )

===============================================

名詞概念

Name為邏輯名稱,相當于邏輯路徑(相對路徑)主要運用于資料庫開發人員在使用資料庫過程中進行的引用
    Filename:實體名稱,相當于絕對路徑,主要用于進行資料庫資料的實際存儲位址
           

編寫資料庫代碼的注意事項:

1.所有的編碼過程中都必須在英文狀态下進行

2.所有的屬性都必須寫在小括号内,屬性與屬性之間用逗号隔開,最後一個屬性不用加逗号

3.在sqlserver中,關鍵字不區分大小寫,但是内容區分大小寫,值的機關也不區分大小寫(如mb和MB)

4.值必須用單引号 ‘’引起來

5.值可以使用兩種方式,一種以兆數,一種以百分比

6.邏輯名是絕對不可以重名的

7.切換資料庫,use +資料庫名

8.建立資料庫,create database 資料庫名

關于檔案文法:

1.添加檔案文法
   Alter database 資料庫名
   Add file(添加檔案的檔案屬性資訊)
2.删除檔案文法:alter(修改)
  alter database 資料庫名
  remove  file 檔案名
3.查找資料庫檔案文法:Execute(執行)
  Exec  sp_helpfile 資料庫中所存在的檔案名,如果不接資料庫所在的檔案名的話則顯示該資料庫中的所有檔案資訊
4.修改資料庫的檔案資訊文法:modify(修改)
           
Alter  database  student
Modify  file(
Name=’student’,
Size=3MB,
Filegroweth=20%
)
           
5.資料庫的重命名:
  Exec sp_renamedb  舊名稱,新名稱
6.檔案名的重命名:
           
Alter  database  student
Modify  file(
Name=’student2’,
Newname=’student3’
)
           
7.添加檔案組:
  Alter  database  student
  Add  filegroup 組名
8.删除檔案組文法:alter(修改)
  alter database 資料庫名
  remove  filegroup 檔案組名
9.查找資料庫檔案文法: 
  Exec  sp_helpfilegroup
10.将檔案添加到檔案組:
           
Alter  database 資料庫名
Add  file (
Name=’學生’,
Filename=’e:\xuesheng.ndf’
)to  filegroup  組名
           

執行個體代碼

----------------------
----資料庫文法(一)
----Author=“Mr zhong”
----------------------

--建立學生資料庫
create database student
on primary(
name="student",
filename="F:\Micro SQL Express\workplace\student",
size=5MB,
maxsize=20MB,
filegrowth=1MB
)
log on(
name="student_log",
filename="F:\Micro SQL Express\workplace\student_log",
size=5MB
)

--切換資料庫
use student

--資料庫檔案的增、删、改、查
--添加檔案 ADD
alter database student
add file(
name="test_file_3",
filename="F:\Micro SQL Express\workplace\test_file_3",
size=1MB
)

--修改檔案 MODIFY
alter database student
modify file(
name="test_file",
size=4MB,
filegrowth=10%
)

--查找資料庫檔案
exec sp_helpfile test_file

--删除檔案 drop
alter database student
remove file test_file

--重命名
--資料庫重命名
exec sp_renamedb student,newstudent
exec sp_renamedb newstudent,student
--檔案重命名
alter database student
modify file(
name="test_file",
newname="new_test_file"
)

alter database student
modify file(
name="new_test_file",
newname="test_file"
)

exec sp_helpfile new_test_file

--添加檔案組
alter database student
add filegroup class_B

--添加檔案到組内
alter database student
add file(
name="test_file_4",
filename="F:\Micro SQL Express\workplace\test_file_4"
)to filegroup class_A

--查找檔案組
exec sp_helpfilegroup class_A

--删除檔案組
alter database student
remove filegroup class_B

           

轉載于:https://www.cnblogs.com/Mrking2017/p/7599621.html