工作經(jīng)常使用的SQL整理,實戰(zhàn)篇,地址一覽:
工作經(jīng)常使用的SQL整理,實戰(zhàn)篇(一)
工作經(jīng)常使用的SQL整理,實戰(zhàn)篇(二)
工作經(jīng)常使用的SQL整理,實戰(zhàn)篇(三)
?
目錄概覽:
1.數(shù)據(jù)庫
2.表
3.臨時表
4.索引和約束
5.范式
6.增刪改查
7.連接
8.分組和排序
9.通配符
10.視圖
11.存儲過程和事務(wù)
12.游標
13.觸發(fā)器
14.作業(yè)
?
自己親手編寫的一些常用的SQL,希望對大家有用喔,廢話不多說了,直接入正題~
1.數(shù)據(jù)庫
創(chuàng)建數(shù)據(jù)庫
use master if exists ( select * from sysdatabases where name = ' OrderDB ' ) drop database OrderDB create database OrderDB on ( name = ' OrderDB_data ' , filename = ' D:\DB\OrderDB_data.mdf ' , size = 10 , filegrowth = 15 % ) log on ( name = ' OrderDB_log ' , filename = ' D:\DB\OrderDB_log.ldf ' , size = 3 , filegrowth = 10 % )
刪除數(shù)據(jù)庫
drop database OrderDB
2.表
創(chuàng)建表
--用戶表
if exists ( select * from sysobjects where name = ' Tse_User ' ) drop table Tse_User Create table Tse_User ( ID int identity ( 1 , 1 ), UserID int not null , UserName varchar ( 64 ) not null , RealName varchar ( 64 ) null , PRIMARY KEY (UserID) )
?
-- 產(chǎn)品表 if exists ( select * from sysobjects where name = ' Tse_Product ' ) drop table Tse_Product Create table Tse_Product ( ID INT IDENTITY ( 1 , 1 ), ProductID varchar ( 64 ) not null , ProductName varchar ( 256 ) not null , Price float not null , Storage int not null , -- 庫存 PRIMARY KEY (ProductID) )
?
-- 訂單表 if exists ( select * from sysobjects where name = ' Tse_Order ' ) drop table Tse_Order Create table Tse_Order ( ID int identity ( 1 , 1 ), OrderID varchar ( 64 ) not null , UserID int not null , ProductID varchar ( 64 ) not null , Number int not null , -- 購買數(shù)量 PostTime datetime not null , PRIMARY KEY (OrderID), FOREIGN KEY (UserID) REFERENCES Tse_User(UserID), FOREIGN KEY (ProductID) REFERENCES Tse_Product(ProductID) )
刪除表
drop table Tse_User
清空表
truncate ? table ? Tse_User ????清除表中所有數(shù)據(jù),下次插入編號從 1 開始
delete ? from ? Tse_User ??????清除表中所有數(shù)據(jù),但下次插入編號從原有編號 +1 開始
3.臨時表
生成臨時表,插入數(shù)據(jù),將員工姓名全部打印出來
use master go create table #Employee ( ID int identity ( 1 , 1 ), Name varchar ( 64 ) not null , primary key (ID) ) insert into #Employee(Name) values ( ' zhangsan ' ) insert into #Employee(Name) values ( ' lisi ' ) insert into #Employee(Name) values ( ' wangwu ' ) insert into #Employee(Name) values ( ' tony ' ) insert into #Employee(Name) values ( ' mike ' ) declare @i int declare @Name varchar ( 64 ) declare @Count int declare @Str nvarchar ( 4000 ) set @i = 0 select @Count = COUNT ( 0 ) from #Employee while ( @i < @Count ) begin set @Str = ' select top 1 @Name = Name from #Employee where id not in (select top ' + STR ( @i ) + ' id from #Employee) ' exec sp_executesql @Str ,N ' @Name varchar(64) output ' , @Name output select @Name , @i set @i = @i + 1 End
查看表結(jié)構(gòu)及表附加屬性
SP_HELP Tse_User
4.索引和約束
聚集索引確定表中數(shù)據(jù)的物理順序。聚集索引類似于電話簿,后者按姓氏排列數(shù)據(jù)。由于聚集索引規(guī)定數(shù)據(jù)在表中的物理存儲順序,因此一個表只能包含一個聚集索引。但該索引可以包含多個列(組合索引),就像電話簿按姓氏和名字進行組織一樣。
非聚集 索引,該索引中索引的邏輯順序與磁盤上行的物理存儲順序不同。 一個表可以創(chuàng)建多個非聚集索引。
創(chuàng)建聚集索引
CREATE ? UNIQUE ? CLUSTERED ? INDEX ? [PK_Tse_ID] ? ON ? [dbo] . [Tse_User] ?
( ??-- 唯一聚集索引
[ID] ? ASC
) WITH? ( PAD_INDEX ?? = ? OFF , ? STATISTICS_NORECOMPUTE ?? = ? OFF , ? SORT_IN_TEMPDB ? = ? OFF , ? IGNORE_DUP_KEY ? = ? OFF , ? DROP_EXISTING ? = ? OFF ,
? ONLINE ? = ? OFF , ALLOW_ROW_LOCKS ?? = ? ON , ? ALLOW_PAGE_LOCKS ?? = ? ON ) ? ON ? [PRIMARY]
GO
?
創(chuàng)建非聚集索引
CREATE ? UNIQUE ? NONCLUSTERED ? INDEX ? [IX_Tse_UserID] ? ON ? [dbo] . [Tse_User] ?
( ??-- 唯一非聚集索引
[UserID] ? ASC
) WITH? ( PAD_INDEX ?? = ? OFF , ? STATISTICS_NORECOMPUTE ?? = ? OFF , ? SORT_IN_TEMPDB ? = ? OFF , ? IGNORE_DUP_KEY ? = ? OFF , ? DROP_EXISTING ? = ? OFF , ?
ONLINE ? = ? OFF , ? ALLOW_ROW_LOCKS ?? = ? ON , ? ALLOW_PAGE_LOCKS ?? = ? ON ) ? ON ? [PRIMARY]
GO
?
約束
alter ? table ? Tse_User
add ? constraint ? CS_UserName ? check? ( len ( Username ) ? > ?3 ),
constraint ? CS_Email ? check? ( charindex ( '@' , ? Email ) ? > ?0 )
5.范式
第一范式 1NF
第一范式需滿足兩個條件:
1)每個數(shù)據(jù)行必須包含具有原子性(即不可再分)的值;
2)每個數(shù)據(jù)行必須包含一個獨一無二的值,即主鍵。
舉例:假如客戶表中存在地址列,如果經(jīng)常需要按城市歸類,那么,應(yīng)該地址列拆分為省份,城市,縣,街道地址等列。
?
第二范式 2NF
第二范式需要確保數(shù)據(jù)庫表中的每一列都和主鍵相關(guān),而不能只與主鍵的某一部分相關(guān)(主要針對聯(lián)合主鍵而言)。也就是說在一個數(shù)據(jù)庫表中,一個表中只能保存一種 數(shù)據(jù),不可以把多種數(shù)據(jù)保存在同一張數(shù)據(jù)庫表中。
舉例:比如常用的選課表中,以學(xué)號和課程號為聯(lián)合主鍵,不能將課程名,學(xué)分等課程相關(guān)信息寫入選課表,因為他們只與主鍵的一部分(課程號)相關(guān)。
?
第三范式 3NF
第三范式需要確保數(shù)據(jù)表中的每一列數(shù)據(jù)都和主鍵直接相關(guān),而不能間接相關(guān)。
舉例:訂單表中以訂單號為主鍵,用戶真實姓名和郵箱等信息與用戶有關(guān),與訂單沒有直接關(guān)系,因此,用戶真實姓名和郵箱等不能放到訂單表中。
由于時間關(guān)系,余下的幾個問題在下一篇中討論,謝謝關(guān)注~,下一篇地址為: 工作經(jīng)常使用的SQL整理,實戰(zhàn)篇(二)
如果您有什么問題,歡迎在下面評論,我們一起討論,謝謝~
如果您覺得還不錯,不妨點下右下方的推薦,有您的鼓勵我會繼續(xù)努力的~
?
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
