当前位置:网站首页>常用SQL语句总结
常用SQL语句总结
2020-11-06 21:19:46 【程序猿欧文】
一、基础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
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World