码迷,mamicode.com
首页 > 数据库 > 详细

【SQL】 常用语句之系统语法

时间:2014-11-16 17:11:24      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   color   ar   sp   for   数据   div   

【SQL】 常用语句之系统语法

1. 获取取数据库服务器上所有数据库的名字
SELECT name FROM master.dbo.sysdatabases
 
2. 获取取数据库服务器上所有非系统数据库的名字
SELECT [name] FROM master.dbo.sysdatabases  ORDER BY [Name]
 
3. 获取数据库下所有的表名
SELECT name from sys.tables
 
4. 获取表结构(字段名,属性)
SELECT
     C.name as [字段名],T.name as [字段类型]
     ,convert(bit,C.IsNullable) as [可否为空]
     ,convert(bit,case when exists(SELECT 1 FROM sysobjects where xtype=‘PK‘ and parent_obj=c.id and name in (
         SELECT name FROM sysindexes WHERE indid in(
             SELECT indid FROM sysindexkeys WHERE id = c.id AND colid=c.colid))) then 1 else 0 end)
                 as [是否主键]
     ,convert(bit,COLUMNPROPERTY(c.id,c.name,‘IsIdentity‘)) as [自动增长]
     ,C.Length as [占用字节]
     ,COLUMNPROPERTY(C.id,C.name,‘PRECISION‘) as [长度]
     ,isnull(COLUMNPROPERTY(c.id,c.name,‘Scale‘),0) as [小数位数]
     ,ISNULL(CM.text,‘‘) as [默认值]
     ,isnull(ETP.value,‘‘) AS [字段描述]
     --,ROW_NUMBER() OVER (ORDER BY C.name) AS [Row]
FROM syscolumns C
INNER JOIN systypes T ON C.xusertype = T.xusertype
left JOIN sys.extended_properties ETP ON ETP.major_id = c.id AND ETP.minor_id = C.colid AND ETP.name =‘MS_Description‘
left join syscomments CM on C.cdefault=CM.id
WHERE C.id = object_id(‘IpInfo‘)

5. 清理 sql 日志库 
Select NAME,size From sys.database_files
ALTER DATABASE tableName SET RECOVERY SIMPLE WITH NO_WAIT
ALTER DATABASE tableName SET RECOVERY SIMPLE
USE Social360_A
DBCC SHRINKFILE(tableLog,0,TRUNCATEONLY)
ALTER DATABASE tableName SET RECOVERY FULL WITH NO_WAIT
ALTER DATABASE tableName SET RECOVERY FULL
 
6. 数据库备份语句(完整备份)
DECLARE @name varchar(100)
SET @name= N‘db_‘+CONVERT(VARCHAR(10),GETDATE(),120)
 
BACKUP DATABASE [btb_shop_cangzhou]
TO  DISK = N‘G:\数据库备份\db.bak‘ WITH NOFORMAT,
NAME = @name,
SKIP, REWIND, NOUNLOAD,  STATS = 10
 
7. 开启执行计划(检测 SQL 性能)
Ctrl + L
 
 
 
 

【SQL】 常用语句之系统语法

标签:des   style   io   color   ar   sp   for   数据   div   

原文地址:http://www.cnblogs.com/fzz2727551894/p/4101544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!