zoukankan      html  css  js  c++  java
  • sql语法

    谈一下sql语法

    sql为Structured Query Language的缩写,意思为结构化查询语言,具有定义、查询、更新和控制等多种功能,是关系数据库的基本语言。

    sql主要分为4大类:

    数据操纵语言  DML:insert update delete

    数据查询语言  DQL:select

    数据定义语言  DDL:create alter drop rename truncate

    数据控制语言  DCL:grant revoke

    1.数据定义语言  DDL

    创建表 create table user(id varchar(10),name varchar(10),sex number(1) not null,age number(3),constraint pk_user primary key(id));

    创建主键和约束条件为唯一性的时候 数据库会自动为该字段创建索引

    用子查询创建表  create table user as select * from person;(表结构必须一致)

    2.数据操纵语言 DML

    插入语句  insert into user values(1,'lufei',1,16);这种插入语句values中必须要包含表中的所有字段,

    如果对固定的几个字段进行添加,要用  insert into user(id,name,sex) values(2,'namei',0)

    通过子查询进行插入  insert into user select * from person;(表结构必须一致)

    更新语句  update user set age=18 where id=1;

    删除语句  delete from user where id=2;

    delete与truncate都为删除,他们有什么区别呢?

    区别有一篇博客写的很好,可以作为参考

    引自:http://www.cnblogs.com/luowende2012/archive/2012/07/22/2604049.html

    3.数据查询语言 DQL

    首先看一下查询语句的基本框架

    select sex,max(age) from user where id=2 group by sex having sex=1 order by age;

          需要查询的字段              表名           筛选条件         分组字段        分组筛选       排序字段

    执行循序  

    1.where 

    2.group by

    3.having

    4.select

    5.order by

    下面写了一些sql语句 并对一些sql做出解释

    1)查询当前时间

    select NOW() from DUAL; 

    dual为数据库自带的一个虚表,可以通过这个表查询一些数据,或者进行一些操作

    2)通过as关键字(可以省略)对字段或者表取别名 

    select id,name as username from user as u;

    3)在user表中查询age大于16且name为空的人的id

    select id from user u where age>16 and name is null;

    4)查询年龄大于16小于18的人

    select * from user where age>16 and age<18;

    等价于 select * from user where age between 16 and 18;

    5)查询年龄为16,17,18的海贼成员,且为女生(sex=0)

    select * from user where age in (16,17,18) and sex=0;

    等价于 select * from where (age=16 or age=17 or age=18) and sex=0; 还是用函数方便吧

    6)查询name以lu开头(不区分大小写)的海贼成员

    select * from user where substr(lower(name),1,2) like 'lu';

       查询name以ei结尾的海贼成员

    select * from user where substr(lower(name),length(name)-1,2) like 'ei';

    7)查询年龄在平均年龄以上的海贼成员

    首先第一步 你要知道平均年龄是多少

    select avg(age) from user;

    第二步查询年龄在平均年龄以上

    select * from user u where u.age>(select avg(age) from user);

    此语句为通过子查询 来查询结果的方式,子查询效率很低,如果有其他方式,尽量不要用子查询

    8)查询男同胞里的平均年龄

    select avg(age) from user where sex=1;

    9)求每个年龄中的海贼成员人数

    select count(*) from user group by age;

    10)求每个性别中的海贼成员中平均年龄大于10的人的个数,性别,平均年龄

    1.首先对每个性别进行分组

    select sex from user group by sex;

    2.求每个性别中平均年龄大于10的人的人数,性别,平均年龄

    select count(*),sex,avg(age) from user group by sex having avg(age)>10

    11)求每个性别中年龄都大于16的人的性别

    select sex from user group by sex having min(age)>16;

    换一种方式:

    1.先求出年龄含有小于16的人

    select distinct sex from user where age<16;

    2.除了小于16的人都是大于16的人

    select sex from user group by sex having sex not in(select distinct sex from user where age<16);

    12)删除每个名字中重复的数据

    思考:要删除重复的数据,不能全部删除,要保留一个,例如只保留id最大的数据

    1.先求出每个年龄中id最大的数据,对于重复的数据,最大的id只有一个,而对于不重复的数据最大的一条就是他本身

    所以只需求出最大的数据不用对重复的数据进行限制

    select max(id) from user group by name;

    3.删除重复的数据

    delete from user where id not in(select max(id) from user group by name)

  • 相关阅读:
    Clean Code(三):注释
    Clean Code(二):函数
    mysql中查询某字段所在的表方法
    对于POI的XSSFCell 类型问题
    Clean Code 笔记 (一):命名
    java 注解
    搭建Eureka服务时报Cannot execute request on any known server 错误
    Jquery获取子父类方法
    Oracle 查询id相同多个数据取一条
    Ajax的使用及后台如何传参
  • 原文地址:https://www.cnblogs.com/qingtianyu/p/4651738.html
Copyright © 2011-2022 走看看