一文学习mysql基础知识

1. 常见的数据库产品   

   1)oracle    --甲骨文    

  2)DB2       --IBM    

  3)SQL sever --微软    

  4)MySql     --AB->SUN->甲骨文

2. 名词解释    

  字段    --表中的列    

  记录    --表中的行

3. 登录远程数据库    

1)打开一个终端窗口 2)登录远程计算 $telnet 176.135.20.168 login:oracle --输入用户名oracle password:oracle --输入密码oracle 此时输入没有回显 [oracle@centos ~]$ --出现此提示符号表示登录成功 3) 启动oracle的客户端软件sqlplus [oracle@centos ~]$sqlplus openlab/open123 SQL> --出现此提示符号表示登录成功
4. 查看表结构 --查看字段信息    
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 --提成
5. 数据类型

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

        -- p 总共多少位

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

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

        -- N 最大字符个数

    3)定长字符串 char(N)

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

    4)日期类型 date

    5)空值 null

        -- not null 不允许为空值

6. SQL

    SQL     --结构化查询语言

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

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

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

9 .from 字句

    9.1 单字段查询

        select  --选择 [史莱克特]

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

        select 字段名 from 表名;

        如:查询emp表中所有的员工名字

       

select name from emp;

        如:查询emp表中所有的工资

  

select salary from emp;

    9.2 多字段查询

        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;

    9.3 字段参与数学运算

        如:查询emp表中所有员工的名字及年薪

       

select name,12*salary from emp;

    9.4 给字段起别名

    select 字段 as 别名,...,字段 as 别名 from 表名;

    如:查询emp表中所有员工的名字和年薪,

        分别起别名为 emp_name ,yearSal

      

select name as emp_name,12*salary as yearSal from emp;

    9.5 拼接字符串

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

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

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

        如:将emp表中部门编号和员工名字中间拼接一个'_' 打印

        

select dept_id || '_' || name from emp;

    9.6 空值处理 null

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

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

        如:列出emp表中所有员工的名字以工资(含提成)

        

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

        空值处理函数 nvl(字段,值)

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

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

            --      如果字段不是空值保持原值不变

       

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

   

distinct --排重

    --位置 永远在字段列表之前

    

select distinct 字段列表 from 表名;

    如:列出emp表中所有的职位不去重

 

select title from emp;

    如:列出emp表中的所有职位排除重复

  

select distinct title from emp;
11. 空值处理--空值参与的一切数学运算结果都是空值--空值参与的一切逻辑运算结果都为假如:列出emp表中所有员工的名字以工资(含提成)
--select name,salary+salary*commission/100 as sal from emp;
空值处理函数 nvl(字段,值)    -- 字段 可能会出现空值的字段    -- 值   给定一个数值,当字段为空值时,用该数值代替    --      如果字段不是空值保持原值不变
select name,salary+salary*nvl(commission,0)/100 from emp;

 

12. where 子句

    where   --哪里,哪些

    作用:从所有的记录中筛选出符合条件的记录

select 字段列表 from 表名 where 条件; 位置:如果有 where 子句,永远在 from 字句之后。

13. 条件的写法

    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;

    2.7 模糊查询 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 表示将指定字符之后的一个字,退出通配符含义.

14. 对立面

 

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

    如:列出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;
17 模糊查询 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 表示将指定字符之后的一个字,退出通配符含义.



18. 常用的组函数    组函数:对多条记录进行处理,最终得到一个结果。组函数的特性:

        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() --平均值
19 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;
20. 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;
21. select 语句中各子句的执行顺序

 

1)from 子句 --决定从哪张表中查询数据 2)where 子句 --从所有记录中筛选出符合条件的记录 3)group by 子句 --将记录进行分组 4)having 子句 --从所有分组中筛选出符合条件的分组 5)select 子句 --选择要显示那些字段 6)order by 子句 --将要显示的字段信息进行排序 因此:在 from 子句中起的别名在所有子句中都可以使用 select 子句中起的别名 只能在 order by 子句中使用
22.子查询    子查询:将一条完整的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; 个人公众号:科技信徒

 


__EOF__

本文作者one996
本文链接https://www.cnblogs.com/one966/p/17805108.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   一半春风  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示