首页 > 技术文章 > SQL开发与数据库管理笔记

developer2020 2019-12-27 17:32 原文

简介: SQL开发与数据库管理笔记,看过的都说好!

原创: 丶平凡世界
文章链接:https://mp.weixin.qq.com/s/Y9TmoHOyh7To7jUrMulvEw

 

SQL开发与数据库管理笔记

 

一、开发管理篇

1.按姓氏笔画排序

Select * From TableNameOrder By CustomerNameCollate Chinese_PRC_Stroke_ci_as

2.数据库加密:

select encrypt('原始密码')select pwdencrypt('原始密码')select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同select pwdencrypt('原始密码')select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同

3.取回表中字段:

declare @list varchar(1000),@sql nvarchar(1000)select @list=@list+','+b.namefrom sysobjects a,syscolumns bwhere a.id=b.id and a.name='表A'set @sql='select '+right(@list,len(@list)-1)+'     from 表A' exec (@sql)

4.查看硬盘分区:

EXEC master..xp_fixeddrives

5.比较A,B表是否相等:

if (select checksum_agg(binary_checksum(*)) from A)     =    (select checksum_agg(binary_checksum(*)) from B)print '相等'elseprint '不相等'

6.杀掉所有的事件探察器进程:

DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid)FROM master.dbo.sysprocessesWHERE program_name IN('SQL profiler',N'SQL 事件探查器')EXEC sp_msforeach_worker '?'

7.记录搜索:

--开头到N条记录Select Top N * From 表--N到M条记录(要有主索引ID)Select Top M-N * From 表Where ID in (Select Top M ID From 表)Order by ID   Desc--N到结尾记录Select Top N * From 表 Order by ID Desc

例如:一张表有一万多条记录,表的第一个字段 RecID 是自增长字段, 写一个SQL语句, 找出表的第31到第40个记录。

select top 10 recid from Awhere recid not  in(select top 30 recid from A)

分析:

如果这样写会产生某些问题,如果recid在表中存在逻辑索引。

select top 10 recid from A where

是从索引中查找,而后面的

select top 30 recid from A

则在数据表中查找,这样由于索引中的顺序有可能和数据表中的不一致,这样就导致查询到的不是本来的欲得到的数据。

解决方案

a,用order by select top 30 recid from A order by ricid 如果该字段不是自增长,就会出现问题

b,在那个子查询中也加条件:select top 30 recid from A where recid>-1

9:获取当前数据库中的所有用户表

select Name from sysobjectswhere xtype='u' and status>=0

文章进行了部分删减,完整内容请点击:https://developer.aliyun.com/article/740853?utm_content=g_1000096920

关键字:SQL 存储 数据库 数据安全/隐私保护 数据库管理 索引

推荐阅读