当前位置:网站首页>常用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
边栏推荐
- Python download module to accelerate the implementation of recording
- vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
- Jmeter——ForEach Controller&Loop Controller
- 游戏主题音乐对游戏的作用
- [Xinge education] poor learning host computer series -- building step 7 Simulation Environment
- DRF JWT authentication module and self customization
- Unity性能优化整理
- Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
- 6.1.2 handlermapping mapping processor (2) (in-depth analysis of SSM and project practice)
- 游戏开发中的新手引导与事件管理系统
猜你喜欢
Python download module to accelerate the implementation of recording
一篇文章教会你使用Python网络爬虫下载酷狗音乐
If PPT is drawn like this, can the defense of work report be passed?
Introduction to the structure of PDF417 bar code system
Construction of encoder decoder model with keras LSTM
Named entity recognition in natural language processing: tanford core LP ner (1)
游戏主题音乐对游戏的作用
A brief history of neural networks
ES6学习笔记(二):教你玩转类的继承和类的对象
这个项目可以让你在几分钟快速了解某个编程语言
随机推荐
Introduction to Google software testing
游戏开发中的新手引导与事件管理系统
给字节的学姐讲如何准备“系统设计面试”
Building and visualizing decision tree with Python
使用 Iceberg on Kubernetes 打造新一代雲原生資料湖
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
Analysis of etcd core mechanism
For a while, a dynamic thread pool was created, and the source code was put into GitHub
JNI-Thread中start方法的呼叫與run方法的回撥分析
每个大火的“线上狼人杀”平台,都离不开这个新功能
Get twice the result with half the effort: automation without cabinet
How to turn data into assets? Attracting data scientists
Advanced Vue component pattern (3)
A course on word embedding
快速排序为什么这么快?
一篇文章教会你使用HTML5 SVG 标签
Who says cat can't do link tracking? Stand up for me
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
Behind the first lane level navigation in the industry
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers