跳至內容

Microsoft SQL Server/Database manipulation

維基教科書,自由的教學讀本

創建資料庫

[編輯]

創建的每個資料庫都會創建相應的文件:

  • Master Database File (.mdf)
  • Log Database File (.ldf)
CREATE DATABASE [MyDB] ON  PRIMARY 
( NAME = N'MyDB', FILENAME = N'D:\DATASQL\MSSQL10.MSSQLSERVER\MSSQL\DATA\MyDB.mdf' , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'MyDB_log', FILENAME = N'D:\DATASQL\MSSQL10.MSSQLSERVER\MSSQL\DATA\MyDB_log.ldf' , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

讀取

[編輯]

顯式指明資料庫:

USE MyDB;
SELECT * FROM MyTable;

或使用全路徑:

SELECT * FROM [MyDB].[dbo].[MyTable];

備份

[編輯]

Template:...

恢復

[編輯]
RESTORE DATABASE MyDB
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\Backup\2016-02-16-MyDB.bak'
WITH REPLACE