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];
備份
編輯恢復
編輯RESTORE DATABASE MyDB
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\Backup\2016-02-16-MyDB.bak'
WITH REPLACE