一篇文章学习mysql

常见的数据库产品

1)oracle    --甲骨文
2)DB2       --IBM
3)SQL sever --微软
4)MySql     --AB->SUN->甲骨文

名词解释

字段    --表中的列
记录    --表中的行

登录远程数据库

1)打开一个终端窗口 
2)登录远程计算
                         $telnet 176.135.20.168
                         login:oracle    --输入用户名oracle
                         password:oracle --输入密码oracle 此时输入没有回显
                         [oracle@centos ~]$ --出现此提示符号表示登录成功 3)启动oracle的客户端软件sqlplus
                         [oracle@centos ~]$sqlplus openlab/open123
                         SQL>  --出现此提示符号表示登录成功

查看表结构 --查看字段信息

desc 表名; 
    如:查看员工表emp的结构
  
  desc emp;
    ID              id          --员工编号
    NAME            name        --员工姓名  
    TITLE           title       --职位
    DEPT_ID         dept_id     --部门编号
    SALARY          salary      --工资,薪水
    MANAGER_ID      manager_id  --领导的员工编号
    START_DATE      start_date  --入职日期
    COMMISSION      commission  --提成

数据类型

1)数值类型 number(p[,s])

    -- p 总共多少位

    -- s 小数部分的位数,整数可以不写

2)可变长字符串 varchar2(N)

    -- N 最大字符个数

3)定长字符串 char(N)

    -- N 字符个数不够补空格

4)日期类型 date

5)空值 null

    -- not null 不允许为空值

SQL

SQL     --结构化查询语言

1)数据查询语言 DQL --查询表格中的信息

2)数据定义语言 DDL --创建表格

3)数据操作语言 DML --对表格中的数据进行增、删、改

from 字句

单字段查询

    select  --选择 [史莱克特]

    from    --从...[富饶亩]

    select 字段名 from 表名;

    如:查询emp表中所有的员工名字
 select name from emp;
    如:查询emp表中所有的工资
   select salary from emp;
 多字段查询
   select 字段1,...,字段N from 表名;
    如:查询emp表中所有员工的名字和工资
select name,salary from emp;
    --调整字符串显示列宽(仅本次登录有效)

    col 字段名 for a字符个数;

    --如:将name字段的显示列宽调整为12
  col name for a12;
    如:查询emp表中所有员工的编号、名字、工资
 select id,name,salary from emp;
字段参与数学运算

    如:查询emp表中所有员工的名字及年薪
 select name,12*salary from emp;
给字段起别名
   select 字段 as 别名,...,字段 as 别名 from 表名;
如:查询emp表中所有员工的名字和年薪,

    分别起别名为 emp_name ,yearSal
 select name as emp_name,12*salary as yearSal from emp;
拼接字符串

    --注意 在oracle 中 字符串是用一对英文单引号扩起来的

    --拼接两个内容使用符号 ||

    select 内容1 || 内容2 from emp;

    如:将emp表中部门编号和员工名字中间拼接一个'_' 打印
select dept_id || '_' || name from emp;
 空值处理 null

    --空值参与的一切数学运算结果都是空值

    --空值参与的一切逻辑运算结果都为假

    如:列出emp表中所有员工的名字以工资(含提成)
--select name,salary+salary*commission/100 as sal from emp;
    空值处理函数 nvl(字段,值)

        -- 字段 可能会出现空值的字段

        -- 值   给定一个数值,当字段为空值时,用该数值代替

        --      如果字段不是空值保持原值不变
select name,salary+salary*nvl(commission,0)/100 from emp;

排重

distinct --排重
–位置 永远在字段列表之前

select distinct 字段列表 from 表名;
如:列出emp表中所有的职位不去重
   select title from emp;
如:列出emp表中的所有职位排除重复
  select distinct title from emp;

空值处理

空值参与的一切数学运算结果都是空值
–空值参与的一切逻辑运算结果都为假
如:列出emp表中所有员工的名字以工资(含提成)

--select name,salary+salary*commission/100 as sal from emp;

空值处理函数 nvl(字段,值)
– 字段 可能会出现空值的字段
– 值 给定一个数值,当字段为空值时,用该数值代替
– 如果字段不是空值保持原值不变

select name,salary+salary*nvl(commission,0)/100 from emp;

where 子句

where   --哪里,哪些

作用:从所有的记录中筛选出符合条件的记录
select 字段列表
    from 表名
        where 条件;
位置:如果有 where 子句,永远在 from 字句之后。

条件的写法

1)条件中可以使用比较运算符:

大于 > 大于等于 >=
小于 < 小于等于 <=
等于 = 不等于 !=
2)使用逻辑运算符

逻辑与:and 有假则假,全真为真。

    连接多个条件,所有条件都成立,最终结果才为真。比如:链条

例如:工资大于900并且小于1500
  salary > 900 and salary < 1500
逻辑或:or  有真则真,全假为假。

    连接多个条件,只要有一个条件成立,最终结果就为真。比如:过桥

例如:工资大于2000 或者工资小于750
 salary > 2000 or salary < 750
逻辑非:not 非真即假,非假即真。

2.2 条件为数值

如:查询emp表中工资大于1000的员工名字及工资
  select name,salary
        from emp
            where salary > 1000;
2.3 条件为字符串

如:查询emp表中员工 Ben 的工资
  select salary
        from emp
            where name = 'Ben';
            --注意:在字符串中要区分大小写
2.4 条件为一个闭区间

如:查询工资大于等于1000小于等于1500的员工名字和工资

1)使用逻辑运算符 and --有假则假,全真为真。
select name,salary
        from emp
            where salary>=1000 and salary<=1500;
2)使用oracle特有的 between and --在...和...之间
 select name,salary
        from emp
            where salary between 1000 and 1500;
            --使用between and时 必须将小的数字写在and之前。
2.5 条件为一个列表(不一定有序)

如:列出emp表中31、32、41部门的部门编号以及员工名字

1)使用逻辑运算符 or --有真则真,全假为假
  select dept_id,name
        from emp
            where dept_id=31 or dept_id=32 or dept_id=41;
2)使用oracle 特有的 in()函数 --括号中的内容用逗号隔开
 select dept_id,name
        from emp
            where dept_id in(31,32,41);
2.6 空值处理

如:列出emp表中没有提成的员工名字
   select name
        from emp
           --错 where commission = null;
           --因为:空值参与的一切逻辑运算都为假
    select name
        from emp
            where commission is null;

模糊查询 like

通配符:_ 和 %
'_' --通配任意一个字符
'%' --通配任意个任意字符
模糊字符串举例:
以n结尾的名字       '%n'
名字中带a的         '%a%'
第二个字母是a的     '_a%'
只有三个字母的      '___'

如:列出emp表中名字带a或A的员工

select name
from emp
where lower(name) like ‘%a%’;
–where name like ‘%a%’ or name like ‘%A%’;
如:列出emp表中以n结尾的员工名字

 select name
        from emp
            where name like '%n';
如:列出emp表中名只有三个字母的员工

select name
from emp
where name like ‘___’;
如:列出数据库中所有以S_开头的表名

1)先列出数据库中所有的表名
select table_name from user_tables;
2)列出所有以S_开头的表名
   select table_name
        from user_tables
            where table_name like 'S/_%' escape '/';
        --escape 表示将指定字符之后的一个字,退出通配符含义.

对立面

   >               <=
    <               >=
    =               !=
    is null         is not null
    between and     not between and
    in()            not in()
    like            not like
如:列出emp表中 有领导的员工
  select name,manager_id
        from emp
            where manager_id is not null;
如:列出名字中不带a和A的的员工
 select name
        from emp
            where lower(name) not like '%a%';
如:列出31、32、41部门之外的员工名字以及部门编号
  select name,dept_id
        from emp
            where dept_id not in(31,32,41);

order by 子句

    order   --顺序
    by      --按照
    升序 asc  :从小到大,自然序列,字典序 --是默认顺序可以不写
    降序 desc :从大到小,反自然序列,反字母序列 --使用时必须写
    order by 子句的作用:将要显示的信息按照某些字段进行排序
    位置:永远是最后一条子句
  
 select 字段列表
        from 表名
            where 条件
                order by 条件;

单依据排序

如:列出emp表中所有员工的名字和工资并按工资降序排列
  select name,salary
        from emp
            order by salary desc;2.6 空值处理
如:列出emp表中没有提成的员工名字
  select name
        from emp
           --错 where commission = null;
       --因为:空值参与的一切逻辑运算都为假
  select name
        from emp
            where commission is null;

模糊查询 like

通配符:_ 和 %

'_' --通配任意一个字符

'%' --通配任意个任意字符

模糊字符串举例:

以n结尾的名字       '%n'

名字中带a的         '%a%'

第二个字母是a的     '_a%'

只有三个字母的      '___'

如:列出emp表中名字带a或A的员工
  select name
        from emp
            where lower(name) like '%a%';
            --where name like '%a%' or name like '%A%';
如:列出emp表中以n结尾的员工名字
select name
        from emp
            where name like '%n';
如:列出emp表中名只有三个字母的员工
   select name
        from emp
            where name like '___';
如:列出数据库中所有以S_开头的表名

1)先列出数据库中所有的表名

select table_name from user_tables;
2)列出所有以S_开头的表名

 select table_name
        from user_tables
            where table_name like 'S/_%' escape '/';
        --escape 表示将指定字符之后的一个字,退出通配符含义.

常用的组函数

组函数:对多条记录进行处理,最终得到一个结果。

组函数的特性:
1)所有的组函数都自动忽略空值

    2)所有的组函数都支持排重

1.1 统计数量 count()

    如:统计emp表中的员工人数

select count(id) from emp;
如:统计41部门的人数

  select count(id)
            from emp
                where dept_id=41;
    如:统计emp表中有提成的人数
 select count(commission) from emp;--自动忽略空值
    如:统计emp表中共有多少个职位
 select count(distinct title) from emp;--组函数支持排重
1.2 计算总数、求和 sum()

    如:统计emp表中所有员工的工资总和

select sum(salary) from emp;
如:统计emp表中42部门的工资总和

   select sum(salary)
            from emp
                where dept_id=42;
1.3 求最大值 max()

    如:查询emp表中的最高工资
  select max(salary) from emp;
1.4 求最小值 min()

    如:查询emp表中副总裁的最低工资
select min(salary)
            from emp
                where title like 'VP%';
1.5 求平均值 avg()

    如:统计emp表中所有员工的平均工资
 select avg(salary) from emp;
    sum()   --求和
    count() --计数
    max()   --最大值
    min()   --最小值
    avg()   --平均值

group by 子句

group --分组
by --按照…
位置:在 from 和 where 子句之后

select 字段列表
    from 表名
        where 条件
            group by 依据
                order by 依据;

注意:含有 group by 子句的 select 语句
字段列表中只能写两种内容:
1)分组的依据
2)组函数处理过的结果
如:统计emp表中各部门的人数和平均工资,并按照部门编号排序

select dept_id,count(id),avg(salary)
    from emp
        group by dept_id
            order by dept_id;

如:统计emp表中各部门 工资大于800的人数 并按部门编号排序

select dept_id,count(id)
    from emp
        where salary > 800
            group by dept_id
                order by dept_id;

having 子句

having  --拥有...特征

作用:从所有的分组中筛选符合条件的分组

位置:永远只能在 group by 之后

select 字段列表

from 表名

where 条件

group by 分组依据

having  条件

order by 排序依据;

如:列出员工工资大于800的人数超过1人的部门编号,人数

并按人数排序
   select dept_id,count(id) as cnt
    from emp
    where salary > 800
    group by dept_id
    having count(id) > 1
    order by cnt;

select 语句中各子句的执行顺序

   1)from 子句         --决定从哪张表中查询数据
    2)where 子句        --从所有记录中筛选出符合条件的记录
    3)group by 子句     --将记录进行分组
    4)having 子句       --从所有分组中筛选出符合条件的分组
    5)select 子句       --选择要显示那些字段
    6)order by 子句     --将要显示的字段信息进行排序
    因此:在 from 子句中起的别名在所有子句中都可以使用
    在 select 子句中起的别名 只能在 order by 子句中使用

子查询

子查询:将一条完整的select语句a,嵌入到另一条select语句b中

作为其一部分,a叫做b的子查询。
执行顺序:先执行子查询,再执行外部查询。
1.1 嵌入到 where 子句中

    1)结果为单个值

    如:列出工资比Ben 高的员工姓名与工资

    先:查询Ben的工资
select salary
from emp
where name = 'Ben'; --1100
后:查询工资大于1100的员工信息
select name,salary
from emp
where salary>1100;
合并:
select name,salary
    from emp
    where salary>(
    select salary
    from emp
    where name = 'Ben'
    );
2)查询结果为多值

如:列出emp表中所有领导的名字

先:查询领导的员工编号,注意排重
 select distinct manager_id from emp;
    --结果:1,2,3,6,7,8,9,10,null
后:根据上一步的领导编号查询领导的名字
  select name
    from emp
    where id in(1,2,3,6,7,8,9,10,null);
合并:
 select name
    from emp
    where id in(
    select distinct manager_id
    from emp
    );
如:列出emp表中所有普通员工的名字

先:查询领导的员工编号,排重,并且排除空值
 select distinct manager_id
    from emp
    where manager_id is not null;
    --结果是:1,2,3,6,7,8,9,10
后:查询员工编号不在上述结果中的员工名字
 select name
    from emp
    where id not in(1,2,3,6,7,8,9,10);
合并:
 select name
    from emp
    where id not in(
    select distinct manager_id
    from emp
    where manager_id is not null
    );

1.2 嵌入到having 子句中
如:列出平均工资大于42部门的 部门编号及平均工资,
并按平均工资降序排列。

先:先统计42部门的平均工资
  select avg(salary)
    from emp
    where dept_id=42;
后:列出平均工资大于42部门的
select dept_id,avg(salary) as sal
from emp
group by dept_id
having avg(salary)>(
select avg(salary)
from emp
where dept_id=42
)
order by sal desc;

欢迎关注我的个人公众号:科技信徒

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值