打開CMD命令
執行:sqlcmd/?
這是sqlcmd命令的一些幫助信息
通過上面可以知道怎麼連數據庫了
執行:sqlcmd -S 服務器地址 -d 數據庫名稱 -U 用戶密碼 -P 密碼
以下是操作數據庫的一下命令
復制代碼 代碼如下:
0. 創建數據庫
CREATE DATABASE DB001
1. 創建用戶
CREATE LOGIN USER1
WITH PASSWORD = 'USER_PW';
2. 修改數據的所有者
USE DB001
exec sp_changedbowner 'USER1'
GO
3. 設置READ_COMMITTED_SNAPSHOT
ALTER DATABASE [DB001] SET READ_COMMITTED_SNAPSHOT ON
GO
4.修改字符集
ALTER DATABASE DB001 COLLATE SQL_Latin1_General_CP437_CS_AS
GO
5. 獲取所有數據庫名:
Select Name FROM Master..SysDatabases
6 . 獲取所有表名:
select name from sysobjects where type='U'
XType='U':表示所有用戶表;
XType='S':表示所有系統表;
7. 獲取所有字段名:
Select Name from SysColumns Where id=Object_Id('TableName')
8. 查看與某一個表相關的視圖、存儲過程、函數
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%TableName%'
9. 查詢某一個表的字段和數據類型
select column_name,data_type from information_schema.columns
where table_name = N'TableName'
10. 獲取數據庫文件路徑
select ltrim(rtrim(filename)) from dbname ..sysfiles where charindex('MDF',filename)>0
or
select ltrim(rtrim(filename)) from dbname ..sysfiles where charindex('LDF',filename)>0
MSSQL2005_數據庫備份語句
--完整備份
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Full_20070908.bak'
--差異備份
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Diff_20070908.bak'
With Differential
--日志備份,默認截斷日志
Backup Log NorthwindCS
To disk='G:\Backup\NorthwindCS_Log_20070908.bak'
--日志備份,不截斷日志
Backup Log NorthwindCS
To disk='G:\Backup\NorthwindCS_Log_20070908.bak'
With No_Truncate
--截斷日志不保留
Backup Log NorthwindCS
With No_Log
--或者
Backup Log NorthwindCS
With Truncate_Only
--截斷之後日志文件不會變小
--有必要可以進行收縮
--文件備份
Exec Sp_Helpdb NorthwindCS --查看數據文件
Backup Database NorthwindCS
File='NorthwindCS' --數據文件的邏輯名
To disk='G:\Backup\NorthwindCS_File_20070908.bak'
--文件組備份
Exec Sp_Helpdb NorthwindCS --查看數據文件
Backup Database NorthwindCS
FileGroup='Primary' --數據文件的邏輯名
To disk='G:\Backup\NorthwindCS_FileGroup_20070908.bak'
With init
--分割備份到多個目標
--恢復的時候不允許丟失任何一個目標
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Full_1.bak'
,disk='G:\Backup\NorthwindCS_Full_2.bak'
--鏡像備份
--每個目標都是相同的
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Mirror_1.bak'
Mirror
To disk='G:\Backup\NorthwindCS_Mirror_2.bak'
With Format --第一次做鏡像備份的時候格式化目標
--鏡像備份到本地和遠程
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Mirror_1.bak'
Mirror
To disk='\\192.168.1.200\Backup\NorthwindCS_Mirror_2.bak'
With Format
--每天生成一個備份文件
Declare @Path Nvarchar(2000)
Set @Path ='G:\Backup\NorthwindCS_Full_'
+Convert(Nvarchar,Getdate(),112)+'.bak'
Backup Database NorthwindCS
To disk=@Path
--從NoRecovery或者
--Standby模式恢復數據庫為可用
Restore Database NorthwindCS_Bak
With Recovery
--查看目標備份中的備份集
Restore HeaderOnly
From Disk ='G:\Backup\NorthwindCS_Full_20070908.bak'
--查看目標備份的第一個備份集的信息
Restore FileListOnly
From Disk ='G:\Backup\NorthwindCS_Full_20070908_2.bak'
With File=1
--查看目標備份的卷標
Restore LabelOnly
From Disk ='G:\Backup\NorthwindCS_Full_20070908_2.bak'
--備份設置密碼保護備份
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Full_20070908.bak'
With Password = '123',init
Restore Database NorthwindCS
From disk='G:\Backup\NorthwindCS_Full_20070908.bak'
With Password = '123'