SQL開發與數據庫管理筆記

簡介: 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 存儲 數據庫 數據安全/隱私保護 數據庫管理 索引

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章