CMD命令操作MSSQL2005数据库(命令整理)

前端之家收集整理的这篇文章主要介绍了CMD命令操作MSSQL2005数据库(命令整理)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

打开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' 原文链接:https://www.f2er.com/mssql/63498.html

猜你在找的MsSQL相关文章