1、基礎Sql語句html
一、建立數據庫:Create DataBase dbName;sql
二、刪除數據庫:Drop DataBase dbName;數據庫
三、建立新表:Create Table tabName(col1 type1 [not null] [primary key] ,col2 type2 [not null ], ........);ide
根據已有表建立新表的兩種方式:A:Create Table tab_new like tab-old;函數
B:Create Table tab_new as Select col1,col2,....from tab_old definition only;spa
四、刪除新表:Drop Table tabName;htm
五、爲表增長一列:Alter Table tabName add column col type;blog
六、爲表添加主鍵與刪除主鍵:添加主鍵:Alter Table tabName add primary key(col);排序
刪除主鍵:Alter Table tabName drop primary key(col);索引
七、爲表建立和刪除索引:建立索引:Create [unique] index indexName on tabName(col ....)
刪除索引:Drop index indexName;
八、建立和刪除視圖:建立視圖:Create view viewName as Select statement;
刪除視圖:Drop view viewName;
九、基本的sql語句: 查詢:Select * from Table where 範圍;Select * from Table where field1 like ‘%value1%'(模糊查詢)
插入:Insert into Table(field1,field2,...) values(value1,value2,.....);
刪除:Delete from Table where 範圍;
· 更新:Update Table set field1=value1 where 範圍;
排序:Select * from Table order by field1 Desc【降序】| Asc【升序】;
總數:Select count as TotalCount from Table ;
求和:Select sum(field1) as sumVaule from Table;
平均:Select avg(field1) as avgValue from Table;
最大:Select max(field1) as maxValue from Table;
最小:Select min(field1) as minVaule from Table;
十、Sql中的幾個高級查詢運算詞:
A: UNION 運算符
UNION運算符經過組合其餘兩個結果表(例如 TABLE1 和 TABLE2)並消去表中任何重複行而派生出一個結果表。當 ALL 隨 UNION 一塊兒使用時(即 UNION ALL),不消除重複行。兩種狀況下,派生表的每一行不是來自 TABLE1 就是來自 TABLE2。
B: EXCEPT 運算符
EXCEPT 運算符經過包括全部在 TABLE1 中但不在 TABLE2 中的行並消除全部重複行而派生出一個結果表。當 ALL 隨 EXCEPT 一塊兒使用時 (EXCEPT ALL),不消除重複行。
C: INTERSECT 運算符
INTERSECT 符經過只包括 TABLE1 和 TABLE2 中都有的行並消除全部重複行而派生出一個運算結果表。當 ALL 隨 INTERSECT 一塊兒使用時 (INTERSECT ALL),不消除重複行。
注:使用運算詞的幾個查詢結果行必須是一致的。
十一、使用錶鏈接:
(1)內鏈接(Inner Join):Inner Join TableName ON condition;
(2)不等值鏈接:不等值鏈接即爲在鏈接的條件中可使用小於(<)、大於(>)、不等於(<>)等運算符,並且還可使用LIKE、BETWEEN AND等運算符,甚至還可使用函數。示例:Select field1,field2 from table1 Inner Jion table2 on table1.field1=table2.field1 where table1.field3<table2.field4
(3)交叉鏈接:隱式:Select t1.field1,t2.field3 from table1 as t1,table2 as t2;
顯式:Select t1.field1,t2.field3 from table1 as t1 cross Jion table2 as t2;
(4)外鏈接:A:Left outer Join (左外鏈接(左鏈接):結果集既包括鏈接表的匹配行,也包括左鏈接表的全部行);
B:Right outer Join (右外鏈接(右鏈接):結果集既包括鏈接表的匹配鏈接行,也包括右鏈接表的全部行);
C:Full outer Join(全外鏈接:不只包括符號鏈接表的匹配行,還包括兩個鏈接表中的全部記錄);
十二、使用分組Group by:
示例:Select 類別,摘要,sum(field2) as sumValue from tableName Group by 類別;
注:一張表,一旦分組完成後,查詢後只能獲得組相關的信息。組相關的信息:(統計信息) count,sum,max,min,avg 分組的標準);在SQLServer中分組時:不能以text,ntext,image類型的字段做爲分組依據;在select統計函數中的字段,不能和普通的字段放在一塊兒。在使用Group by實現分組時,若是有where過濾條件,必須寫在Group by以前。
1三、Having的使用:
如對部分分組進行過濾,就須要用到Having,由於聚合函數不能再Where語句中使用,因此得使用Having來代替。
注:使用Having子句時,其應位於Group by以後,而且Having語句中不能包含未分組的列名。
2、複雜Sql語句
一、複製表(僅複製表結構):Select * into b from a where 1<>1;Select top 0 * into b from a;
二、拷貝表(拷貝數據):Insert into b(a,b,c) Select d,e,f from a;
三、子查詢:
SELECT 語句能夠嵌套在其餘語句中,好比 SELECT,INSERT,UPDATE 以及 DELETE 等,這些被嵌套的 SELECT 語句就稱爲子查詢,能夠這麼說當一個查詢依賴於另一個查詢結果時就可使用子查詢。子查詢有兩種類型,一種是隻返回一個單值的子查詢,這時它能夠用在一個單值可使用的地方,這時子查詢能夠看做是一個擁有返回值的函數;另一種是返回一列值的子查詢,這時子查詢能夠看做是一個在內存中臨時存在的數據表。
(1)單值子查詢:
IN運算符示例:
四、between的用法:Select * from tableName where time between time1 and time2;
Select a,b,c from tableName where a not between 數值1 and 數據2;
注:between限制查詢數據範圍時包括了邊界值,not between不包括;
五、兩張關聯表,刪除主表中已經在副表中沒有的信息:Delete from table1 where not exists (Select * from table2 where table1.field1=table2.field2);
六、四張表聯合查詢:select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where ....
七、查詢前十條數據:Select top 10 * from TableName where 範圍;
八、隨機取出十條數據:Select top 10 * from TableName order by newid();
九、選擇在每一組b值相同的數據中對應的a最大的記錄的全部信息:Select a,b,c from table1 as t1 where a=(Select max(a) from table2 as t2 where t1.b=t2.b);
十、包括全部在 TableA 中但不在 TableB和TableC 中的行並消除全部重複行而派生出一個結果表:(select a from tableA ) except (select a from tableB) except (select a from tableC)
十一、列出數據庫裏全部的表名:select name from sysobjects where type='U' // U表明用
十二、列出表裏的全部的列名:select name from syscolumns where id=object_id('TableName')
1三、刪除表中的重複記錄:(1)Delete from TableName where id not in (Select max(id) from TableName group by col1,col2 ......);
(2)Select distinct * into temp from TableName
delete from TableName
insert into Tablename seletct * from temp
(3)alter table tablename
--添加一個自增列
add column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b) from tablename group by column1,column2,...)
alter table tablename drop column column_b
1四、一條sql語句實現數據庫分頁:(1)Select top num * from TableName
where field1>(Select max(field1) from (Select totalNum field1 from TableName order by fireld1) A)
order by field1
(2)Select * from (Select row_number() over (order by field1) rownumber,* from TableName) A
Where rownumber between startNum And [start+count-1];
注:有關sql查詢分頁的內容能夠參考博客(https://www.cnblogs.com/zcttxs/archive/2012/04/01/2429151.html),這裏再也不贅述。