zoukankan      html  css  js  c++  java
  • Oracle学习笔记(五)

    七、查询
    1、基本查询语句
    select 列名字,列名字 from 表名字
    例如
    select user_a_id from userinfo;

    2、在SQL*PLUS中设置格式
    (1)设置新的字段名(更改查询结果的字段名)
    column column_name heading new_name
    例如:
    col username heading 用户名;
    查看更改
    select * from userinfo;
    (2)设置结果显示的格式
    例如:
    设置显示字段长度为10,字符类型的设置
    col user_username format a10;
    设置数值型字段的长度,用一个'9'表示一个数字位
    col user_a_id format 9999.9;
    注:格式不对的信息会显示########
    col user_a_id format $9999.9;
    设置日期格式同字符格式的设置

    可以设置:
    col user_a_id format 9999.9;
    col user_username format a10;
    col user_pwd format a10;
    col user_email format a10;
    col user_regdate format a10;

    删除格式:
    col user_a_id clear;

    3、查询表中的所有字段及指定字段
    (1)查询所有字段
    a、用'*'代指所有字段
    select * from userinfo;
    b、写出每个字段的名字
    select user_a_id,user_username,user_pwd,user_email,user_regdate from userinfo;
    (2)查询指定字段
    select user_a_id,user_username from userinfo;

    4、给字段设置别名(针对查询结果)
    (1)设置别名
    注意:as可以省略,用空格隔开原来的字段名和新字段名即可
    select user_a_id as 编号,user_username as 用户姓名,user_pwd as 密码,user_email as 邮箱,user_regdate as 日期 from userinfo;
    col 编号 format 9999.9;
    col 用户姓名 format a10;
    col 密码 format a10;
    col 邮箱 format a10;
    col 日期 format a10;
    (2)去重复别名
    select distinct user_username as 用户姓名 from userinfo;

    5、运算符和表达式
    表达式 = 操作数 + 运算符
    Oracle中的操作数可以有变量、常量和字段
    算数运算符(+,-,*,/)
    比较运算符(>,>=,<,<=,=,<>)
    逻辑运算符(and,or,not)

    6、在SELECT语句中使用运算符
    (1)使用算数运算符
    create table users(
    id number(5,1),
    username varchar2(20),
    salary number(5,1)
    );

    insert into users values(1,'aaa',800);
    insert into users values(2,'bbb',1800.5);
    insert into users values(3,'ccc',5000.5);

    (a)给所有员工增加200元薪水
    没加薪水前:
    select id,username,salary from users;
    增加薪水后:
    select id,username,salary+200 from users;

    (2)使用比较运算符(一般放在where当中)
    工资大于800
    select username from users where salary>800;

    (3)使用逻辑运算符
    工资大于800,并不等于1800.5
    select username from users where salary>800 and salary<>1800.5;
    工资要么大于800,要么不等于1800.5
    select username from users where salary>800 or salary<>1800.5;

    7、带条件的查询
    (1)单一条件的查询
    查询员工姓名是'aaa'
    select salary from users where username='aaa';
    查询id是3的员工薪水
    select username,salary from users where id=3;
    (2)多条查询
    查询员工姓名是'aaa'或者薪水大于2000的员工信息
    select * from users where username='aaa' or salary>2000;
    查询员工姓名是'aaa'或者薪水在800到2000之间的员工信息
    select * from users where username='aaa' or (salary>800 and salary<=2000);
    注意:
    1、逻辑运算符的优先级:按not、and、or的顺序依次递减
    2、比较运算符的优先级高于逻辑运算符
    查询员工姓名不是‘aaa’
    select * from users where not(username='aaa');

    8、模糊查询(类似于搜索引擎的查询)
    关键字like
    (1)通配符的使用(_,%)(一个下划线代替一个字符,百分号代替零到多个字符)
    查询员工姓名是'aaa'
    select * from users where username like 'a%';
    select * from users where username like 'a__';
    查询名字第二个字符是'a'的员工
    select * from users where username like '_a_';
    select * from users where username like '_a%';
    (2)使用LIKE查询
    查询用户名中含有'a'的
    select * from users where username like '%a%';

    9、范围查询
    between ... and 是[800,2000]闭合区间
    查询800到2000之间的员工工资
    select * from users where salary between 800 and 2000;
    查询不在800到2000之间的员工工资
    select * from users where salary not between 800 and 2000;

    in/not in
    查询用户姓名是aaa或者bbb的用户信息
    select * from users where username in('aaa','bbb');
    查询用户姓名不是aaa或者bbb的用户信息
    select * from users where username not in('aaa','bbb');

    10、对查询结果排序
    (1)按照id降序排列
    select * from users order by id desc;
    (2)按照多个字段排序(第一个orderby位置字段第一个需要相等)
    insert into users values(4,'aaa',1000);
    select * from users order by username desc,salary asc;

    11、case...when语句的使用
    (1)在姓名是'aaa'显示'计算机部门','bbb'显示'市场部门',剩下的是'其他部门'
    select username,case username when 'aaa' then '计算机部门'
    when 'bbb' then '市场部门' else '其他部门' end as 部门
    from users;
    (2)在姓名是'aaa'显示'计算机部门','bbb'显示'市场部门',剩下的是'其他部门'
    select username,case when username='aaa' then '计算机部门'
    when username='bbb' then '市场部门' else '其他部门' end as 部门
    from users;

    (3)显示薪水小于800的为工资低,大于5000的为工资高
    select username,case when salary<800 then '工资低'
    when salary>5000 then '工资高' end as 工资水平
    from users;

    12、decode语句使用
    对字段中的名字进行一个条件的判断
    select username,decode(username,'aaa','计算机部门','bbb','市场部门','其他') as 部门
    from users;

    总结:
    1、用户与表空间
    (1)如何查看某个用户的默认表空间和临时表空间
    (2)如何管理表空间:创建、修改、删除表空间
    2、表与约束
    (1)数据类型、字符类型(char(N)、Nchar(n)、varchar2(n)、nvarchar2(n))、数值型:NUMBER(p.s)、float(n)、日期型:DATE、TIMESTAMP、其他类型:BLOB、CLOB
    (2)对表中数据的操作
    添加数据insert、修改数据update、删除数据delete
    (3)非空约束、主键约束、外键约束、唯一约束、检查约束
    3、查询语句
    (1)查询所有字段和指定字段
    (2)为字段设置别名
    (3)在查询语句中使用运算符和表达式
    (4)在查询语句中加入条件where
    (5)范围查询
    (6)模糊查询:LIKE关键字,通配符_和%
    (7)case ...when语句和decode函数

  • 相关阅读:
    Codeforces 1485C Floor and Mod (枚举)
    CodeForces 1195D Submarine in the Rybinsk Sea (算贡献)
    CodeForces 1195C Basketball Exercise (线性DP)
    2021年初寒假训练第24场 B. 庆功会(搜索)
    任务分配(dp)
    开发工具的异常现象
    Telink MESH SDK 如何使用PWM
    Telink BLE MESH PWM波的小结
    [LeetCode] 1586. Binary Search Tree Iterator II
    [LeetCode] 1288. Remove Covered Intervals
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/6416511.html
Copyright © 2011-2022 走看看