1、创建数据库

create database 数据库名

2、删除数据库

drop  database 数据库名

3、创建表

1>、create table 表名 (col1 type1 [not null] [primary key],col2 type2 [not null]....)

2>、create table tab_new  like tab_old(使用旧表创建新表)

4、增加一个列

alert table tabname add  column col type

5、添加/删除主键

alert table tabname add/drop primary key(col)

6、创建索引

create [unique] index idxname on tabname(col...)

7、创建视图

create view viewname as select statement

8、基本sql语句

查询:select * from table1 where 范围

插入:insert into table1(field1,field2) values(value1,value2)

删除:delete from table1 where 范围

更新:update table1 set field=value where 范围

模糊查询:select * from table1 where field1 like '%value1%'

排序:select * from table1 order by field1,field2 [desc]

总数:select count as totalcount from table1

求和:select sum(field1) as sumvalue from table1

平均:select avg(field1) as avgvalue from table1

最大:select max(field1) as maxvalue from table1

最小:select min(field1) as minvalue from table1


9、几个高级查询运算符

union运算符(联合查询)

(select *from tab1where....) union (select *from where...)

except运算符(EXCEPT 返回两个结果集的差(即从左查询中返回右查询没有找到的所有非重复值))

(SELECT * FROM TableA) EXCEPT (SELECT * FROM TableB)

distinct运算符

select distinct xing, ming from B(因为Distinct是作用于多列的,也就是说必须要xing和ming 全都相同的才会被剔除)

intersect运算符(INTERSECT 返回 两个结果集的交集(即两个查询都返回的所有非重复值))

SELECT * FROM TableA INTERSECT SELECT * FROM TableB

10、多表连接

inner join

select tClassName,tSName from TblClass  inner join tblstudent on TblClass.tClassId=TblStudent.tSClassId

(只有两个表中的数据符合on条件,才会显示在结果集中)

left join(左表中的信息全部出现,右表中的信息必须能够匹配的才出现未匹配的项使用null填充)

right join(右表中的信息全部出现,左表中的信息必须能够匹配的才出现未匹配的项使用null填充)

full/cross join(所有数据都会出现,不匹配的项使用null填充只要想做连接查询,必须分析出两个表间的关系)

11、子查询

select *from tab1 where id in(select id from tab2 where....)括号中的sql语句则为子查询

12、多表查询

select tab1.*,tab2.* from tab1,tab2 where tab1.[字段]=tab2.[字段]

13、分组:group by

1>、 错误的,where中不能出现分组函数

select d.name , avg(e.salary) from s_dept d, s_emp e where d.id = e.dept_id group by d.name and avg(e.salary) > 1000;

2>、正确, having 紧跟 group by

select d.name , avg(e.salary) from s_dept d, s_emp e where d.id = e.dept_id group by d.name having avg(e.salary) > 1000;

14、as用法

select 字段 as 别名

SQL语句小结的更多相关文章

  1. MySQL 常用的sql语句小结(待续)

    mysql 常用的sql语句 1.查看数据库各个表中的记录数 USE information_schema; SELECT table_name,table_rows FROM tables WHER ...

  2. postgresql中进行备份和回滚的常用sql语句小结

    最近在项目中需要对已有的部分数据库数据进行备份,通过搜索和实践,把常用的sql以及过程记录如下, 1.常用的备份数据库思路,把需要备份的数据放到一个新表中,这个新表的记录与需要备份的表完全一样,然后备 ...

  3. 数据库 SQL语句小结(更新中)

    ################ Navicat,单条执行sql ################ Navicat,数据库管理工具, 在查询的页面有好多命令,若单条执行: 1:可选中要执行的一条sql ...

  4. 2014年小结之sql语句优化

    之前接手一个数据统计处理的小程序,本来逻辑上并没什么,只是数据量略大,某些表的数据达到了千万级别..因为是统计,所以免不了各种连接各种查询,结果这个小程序写完后运行一次要1个小时..这的确有点出乎意料 ...

  5. SQL Server 数据库部分常用语句小结(二)

    9. 查询备份还原数据库的进度. select command ,percent_complete ,est_time_to_go=convert(varchar,(estimated_complet ...

  6. SQL Server 数据库部分常用语句小结(一)

    1. 查询某存储过程的访问情况 SELECT TOP 1000 db_name(d.database_id) as DBName, s.name as 存储名字, s.type_desc as 存储类 ...

  7. MyBatis源码分析-SQL语句执行的完整流程

    MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...

  8. 数据库 数据库SQL语句三

    转换函数 to_char()字符串转换日期函数 --查询大于某个日期的员工信息 select * from emp where hiredate>to_date('1980-02-12','yy ...

  9. 化繁为简——分解复杂的SQL语句

    今天同事咨询一个SQL语句,如下所示,SQL语句本身并不复杂,但是执行效率非常糟糕,糟糕到一塌糊涂(执行计划也是相当复杂).如果查询条件中没有NOT EXISTS部分,倒是不要一秒就能查询出来. SE ...

随机推荐

  1. 原生 js 整理

    常见的事件 window.event     代表着,事件的状态,只有在事件的过程中才有效.

  2. jQuery 开始动画,停止动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 用闭包方式实现点击a标签弹也索引值

    var self = document.getElementsByTagName("a"); for(var i=0;i<self.length;i++){ self[i]. ...

  4. numpy调试

    x1 = np.arange(9.0) 结果就是: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8.]) 拿这个来初始化进行调试

  5. spring cloud Bug之was unable to refresh its cache! status = Cannot execute request on any known server

    可能原因: 1.application.yml server: port: 10001spring: application: name: microservice-consumer-movieeur ...

  6. zabbix4.2学习笔记--用自带的mysql监控模块

    这里演示监控zabbix本身用到的mysql 第一步:建立mysql监控用户 在生产环境中,出于安全考虑,建议监控客户端数据库时,单独配置一个查询权限用户做查询操作即可 # 撤掉安装时给予的分配单个数 ...

  7. oracle分析函数之ratio_to_report

    ratio_to_report主要完成对百分比的计算,语法为ratio_to_report(exp) over()也就是根据over窗口函数的作用区间,求出作用区间中的单个值在整个区间的总值的比重比如 ...

  8. left_v2.js

    $(document).ready(function(){ $(".mc_left a").each(function(){ var href = $(this).attr(&qu ...

  9. 升级openssh踩得坑

    升级背景: 项目中使用的系统为CentOS6.8,经过漏洞扫描后发现openssh高危漏洞,具体描述如下:OpenSSH 7.2p2之前版本, sshd/ session.c/ do_setup_en ...

  10. apache2 执行ab测试

    ab命令 1, cd进入目录apache bin目录 2, ·ab -n 5000 -c 200 http://admin.dzj.local/publics/login.html >> ...