当前位置:网站首页>常用SQL语句总结
常用SQL语句总结
2020-11-06 21:19:00 【程序猿欧文】
一、基础Sql语句
1、创建数据库:Create DataBase dbName;
2、删除数据库:Drop DataBase dbName;
3、创建新表:Create Table tabName(col1 type1 [not null] [primary key] ,col2 type2 [not null ], ........);
根据已有表创建新表的两种方式:A:Create Table tab_new like tab-old;
B:Create Table tab_new as Select col1,col2,....from tab_old definition only;
4、删除新表:Drop Table tabName;
5、为表增加一列:Alter Table tabName add column col type;
6、为表添加主键与删除主键:添加主键:Alter Table tabName add primary key(col);
删除主键:Alter Table tabName drop primary key(col);
7、为表创建和删除索引:创建索引:Create [unique] index indexName on tabName(col ....)
删除索引:Drop index indexName;
8、创建和删除视图:创建视图:Create view viewName as Select statement;
删除视图:Drop view viewName;
9、基本的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;
10、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),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
11、使用表连接:
(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(全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录);
12、使用分组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之前。
13、Having的使用:
如对部分分组进行过滤,就需要用到Having,因为聚合函数不能再Where语句中使用,所以得使用Having来代替。
注:使用Having子句时,其应位于Group by之后,并且Having语句中不能包含未分组的列名。
二、复杂Sql语句
1、复制表(仅复制表结构):Select * into b from a where 1<>1;Select top 0 * into b from a;
2、拷贝表(拷贝数据):Insert into b(a,b,c) Select d,e,f from a;
3、子查询:
SELECT 语句可以嵌套在其他语句中,比如 SELECT,INSERT,UPDATE 以及 DELETE 等,这些被嵌套的 SELECT 语句就称为子查询,可以这么说当一个查询依赖于另外一个查询结果时就可以使用子查询。子查询有两种类型,一种是只返回一个单值的子查询,这时它可以用在一个单值可以使用的地方,这时子查询可以看作是一个拥有返回值的函数;另外一种是返回一列值的子查询,这时子查询可以看作是一个在内存中临时存在的数据表。
(1)单值子查询:
单值子查询的语法和普通的 SELECT 语句没有什么不同,唯一的限制就是子查询的返回值必须只有一行记录,而且只能有一个列。这样的子查询又被称为标量子查询,标量子查询可以用在 SELECT 语句的列表中、表达式中、WHERE 语句中等很多场合。 (2).........版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4556923
边栏推荐
- Azure data factory (3) integrate azure Devops to realize CI / CD
- Python saves the list data
- vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
- Wow, elasticsearch multi field weight sorting can play like this
- How to demote domain controllers and later in Windows Server 2012
- use Asponse.Words Working with word templates
- C#和C/C++混合编程系列5-内存管理之GC协同
- Natural language processing - wrong word recognition (based on Python) kenlm, pycorrector
- 前端工程师需要懂的前端面试题(c s s方面)总结(二)
- 【自学unity2d传奇游戏开发】地图编辑器
猜你喜欢

Python filtering sensitive word records

NLP model Bert: from introduction to mastery (1)
![[Xinge education] poor learning host computer series -- building step 7 Simulation Environment](/img/f8/4bb6f887d56a7a18eb55cbec579204.jpg)
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment

Music generation through deep neural network

一篇文章带你了解CSS3 背景知识

From zero learning artificial intelligence, open the road of career planning!

StickEngine-架构12-通信协议

What are Devops

Individual annual work summary and 2019 work plan (Internet)

零基础打造一款属于自己的网页搜索引擎
随机推荐
How to use Python 2.7 after installing anaconda3?
Flink的DataSource三部曲之一:直接API
Gather in Beijing! The countdown to openi 2020
一篇文章带你了解CSS 渐变知识
Interpretation of Cocos creator source code: engine start and main loop
Natural language processing - BM25 commonly used in search
What are PLC Analog input and digital input
The importance of big data application is reflected in all aspects
Recommendation system based on deep learning
Mongodb (from 0 to 1), 11 days mongodb primary to intermediate advanced secret
Multi classification of unbalanced text using AWS sagemaker blazingtext
Music generation through deep neural network
只有1个字节的文件实际占用多少磁盘空间
Electron application uses electronic builder and electronic updater to realize automatic update
Lane change detection
Using NLP and ml to extract and construct web data
From zero learning artificial intelligence, open the road of career planning!
小游戏云开发入门
[C / C + + 1] clion configuration and running C language
Brief introduction and advantages and disadvantages of deepwalk model