SQL學(xué)習(xí)準(zhǔn)備為了方便練習(xí),在數(shù)據(jù)庫(kù)中創(chuàng)建演示數(shù)據(jù): create database TEST;use TEST ;----------gocreate table t_icitem (id int primary key, name varchar(255), model varchar(255), alexa varchar(255), country varchar(255) )insert into t_icitem values ('1','oppo','16g','1','CN'),('2','索尼','8g','13','JAP'),('3','蘋果','8g','2','USA'),('4','小米','64g','3','CN'),('5','華為','32g','4','CN'),('6','魅族','32gm','15','CN')select * from t_icitem;----------create table icstockbillentry (id int primary key, item_id int not null, [count] int not null, date date not null )insert into icstockbillentry values (1,1,45,'2016-05-10'),(2,3,100,'2016-05-13'),(3,1,230,'2016-05-14'),(4,2,10,'2016-05-14'),(5,5,205,'2016-05-14'),(6,4,13,'2016-05-15'),(7,3,220,'2016-05-15'),(8,5,545,'2016-05-16'),(9,3,201,'2016-05-17')select * from icstockbillentrySQL初級(jí)教程SELECTSELECT DISTINCTselect distinct column_name from table_name;--從列中選取不重復(fù)的值;即去重WHEREwhere子句中的運(yùn)算符
AND & ORselect * from table_name where column_name > valueand (column_name = 'a' or column_name = 'b');--and和or可以結(jié)合使用,使用括號(hào)可以組成復(fù)雜的表達(dá)式ORDER BYINSERT INTOinsert into table_name(column1,column2)values(1,a),(2,b);--沒(méi)有指定列名,則需要插入所有的值UPDATEDELETEdelete from table_name where some_column = some_value;--如果省略where子句,將刪除表中所有記錄,但不改變表結(jié)構(gòu)、屬性、索引等SQL高級(jí)教程SELECT TOPASselect T1.column_name as A,T2.column_name as B from table_name as T1,table_name as T2在下面的情況下,使用別名很有用:--查詢中涉及多個(gè)表--查詢中使用了函數(shù)--列名很長(zhǎng)或可讀性差--需要把多個(gè)列結(jié)合在一起JOIN (INNER JOIN)
SELECT INTOselect * into table_name_bak from table_name;--完全復(fù)制表,可以看作表的備份;select column_name,column_name into table_nanme_bak from table_name where column_name operator value;--復(fù)制部分列的滿足部分條件的記錄到新表;select T1.column,T2.column into #table from T1 join T2 ON T1.fid = T2.fid;--復(fù)制多個(gè)表中的數(shù)據(jù)到臨時(shí)表;INSERT INTO SELECTCREATEcreate database DB_name;--創(chuàng)建數(shù)據(jù)庫(kù)create table table_name(column_name data_type(size) constraint default(value),column_name data_type(size) constraint default(value),....);--創(chuàng)建數(shù)據(jù)表;data_type為數(shù)據(jù)類型;constraint為約束;default為默認(rèn)值;ALTERDECLARE--#聲明declare @dept varchar(255)declare @batchno varchar(255)declare @starttime datedeclare @endtime date--#賦值set @dept = ''set @batchno = ''set @starttime = '2017-01-01'set @endtime = '2017-12-30' --或者select @dept = '',@batchno = '',@starttime = '2017-01-01',@endtime = '2017-12-30'--#使用slect * from t1,t2,t4 where t4.FName like '%'+@dept+'%'--like比較值格式:'%'+@變量+'%'and t2.FBatchNo like '%'+@batchno+'%'and t1.FDate >= @starttimeand t1.FDate <= @endtimeIF (NOT) EXISTS
if not exists 即如果不存在,if exists 即如果存在
a.判斷數(shù)據(jù)庫(kù)不存在時(shí) b.判斷表不存在時(shí) if not exists (select * from sysobjects where id = object_id('table_name') and OBJECTPROPERTY(id, 'IsUserTable') = 1)c.判斷列不存在 當(dāng)判斷的結(jié)果不存在時(shí),我可以執(zhí)行創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建表,增加列,可以執(zhí)行相應(yīng)的SQL語(yǔ)句; 而if exists同理判斷,首先判斷查詢結(jié)果是否存在,如果存在執(zhí)行判斷后面的語(yǔ)句,查詢的數(shù)據(jù)庫(kù),表,列的方法相同; SQL骨灰教程CONVERTconvert(clume_type(length),date,style)--示例: COUNT select count({*,colume,index}) from table_name [where 條件]--統(tǒng)計(jì)記錄數(shù)select count(distinct colume) from table_name [where 條件]--統(tǒng)計(jì)不重復(fù)的記錄數(shù)日期的計(jì)算--示例: DATEADD(dd,1,getdate()) --當(dāng)前+1天DATEADD(dd,-1,getdate()) --當(dāng)前-1天DATEADD(hh,1,getdate()) --當(dāng)前+1小時(shí)DATEADD(dd,-1,getdate()) --當(dāng)前-1小時(shí)REPLACEDELETE和TRUNCATE區(qū)別
DELETE語(yǔ)句執(zhí)行刪除的過(guò)程是每次從表中刪除一行,并且同時(shí)將該行的刪除操作作為事務(wù)記錄在日志中保存以便進(jìn)行進(jìn)行回滾操作。在刪除過(guò)程中激活與表有關(guān)的刪除觸發(fā)器。TRUNCATE TABLE 則一次性地從表中刪除所有的數(shù)據(jù)并不把單獨(dú)的刪除操作記錄記入日志保存,刪除行是不能恢復(fù)的。并且在刪除的過(guò)程中不會(huì)激活與表有關(guān)的刪除觸發(fā)器。執(zhí)行速度快。
表和索引所占空間。當(dāng)表被TRUNCATE 后,這個(gè)表和索引所占用的空間會(huì)恢復(fù)到初始大小,而DELETE操作不會(huì)減少表或索引所占用的空間。drop語(yǔ)句將表所占用的空間全釋放掉。
應(yīng)用范圍。TRUNCATE 只能對(duì)TABLE;DELETE可以是table和view。
對(duì)于由 FOREIGN KEY 約束引用的表,不能使用 TRUNCATE TABLE,而應(yīng)使用不帶 WHERE 子句的 DELETE 語(yǔ)句。
DELETE自動(dòng)編號(hào)不恢復(fù)到初始值。TRUNCATE自動(dòng)編號(hào)恢復(fù)到初始值。 查詢bak備份文件的版本restore headeronly from disk=N'D:\chl\SQL.bak'常見(jiàn)系統(tǒng)存儲(chǔ)過(guò)程查詢存儲(chǔ)過(guò)程和觸發(fā)器select * from sysobjects where xtype = 'TR'--查詢系統(tǒng)中所有的觸發(fā)器select * from sysobjects where xtype = 'p'--查詢系統(tǒng)中所有的存儲(chǔ)過(guò)程select distinct object_name(id) from syscomments where id in(select id from sysobjects where type ='p') and text like'%TableName%'--查詢表名被哪些存儲(chǔ)過(guò)程用到過(guò)希望詳細(xì)了解觸發(fā)器和存儲(chǔ)過(guò)程的童鞋請(qǐng)看這里:SQL觸發(fā)器入門 - 觸發(fā)器的原理及常用存儲(chǔ)過(guò)程 附:?jiǎn)卧~記憶 |
|
|
來(lái)自: 啟云_9137 > 《計(jì)算機(jī)及軟件應(yīng)用》