DatabaseTwo

Day 02

Oracle的数据类型:

字符型:
varchar2(20)      可变类型    0-4000字节
char(10)          固定类型    0-2000字节
数值型:
number(5)   
number(5,1)  
日期型:
date
timestamp  包含毫秒数

创建表:

create table student(
    id number(5),
    name varchar2(20),
    birthday date,
    sal number(5,1)
);

练习:
创建teacher表,字段有老师工号,老师姓名,老师年薪(整数五位数以上,有一位小数),老师入职时间\

修改表:
建表以后,修改表名:
alter table stu rename to student;
建表以后,新增字段:
alter table student add email varchar2(30);
建表以后,修改列名:
alter table student rename column email to youxiang;
建表以后,修改数据类型:
alter table student modify youxiang varchar2(50);
建表以后,删除字段:
alter table student drop column youxiang;
建表以后,删除表:
drop table student2;           23
删除表会删除表数据和表结构 但表空间依然存在
drop table student purge;   23
建表以后,截断表:
truncate table student;       12
截断表会删除表空间和表数据 但是表结构会保留 在进行sql优化的时候 删除全表的数据 建议使用truncate

1.表空间
2.表数据
3.表结构

查看表结构:
desc student;
查看当前用户有哪些表:
select table_name from user_tables;

增删改查:

新增数据:insert
insert into student values(1,'zs',sysdate,5000);
insert into student(id,name,sal) values(2,'ls',6000);

删除数据:delete
delete from student;
delete from student where id = 1;

修改数据:update
update student set sal = sal + 500;
update student set sal = sal + 500 where id = 1;
update student set sal = sal + 500,name = 'zsf' where id = 1;

查询数据:select
select * from student;
select name,sal from student;
select name,sal from student where name = 'zs';
select name,sal from student where name = 'zs' and sal = 5000;

别名:

select name,sal * 1.3 as sal from student;
select name,sal * 1.3 sal from student;
select s.name,s.sal * 1.3 sal from student s;

模糊查询:like

%:代表任意位的任意字符
_:代表一位上的任意字符
select name,sal from student where name like 'ET%';
select name,sal from student where name like 'ET_%';
select name,sal from student where name like '%s%';

not likeselect name,sal from student where name not like '%s%';

escape:逃离符
通过指定一个字符位进行逃离,来保证like之后的特殊字符看作是普通字符
select name,sal from student where name like 'ET,_' escape ',';
select name,sal from student where name like 'ET._.%' escape '.';

运算符:

+ - * /

比较符:

>  <  >=  <=  !=
>  <>  不等于

条件:
and    并且   两个条件必须都成立
or       或者   有一个条件成立即可
between and 闭合区间

select name,sal from student where sal between 5000 and 5500;
select name,sal from student where sal >= 5000 and sal <= 5500;



is null:是空
is not null:非空
select name,birthday from student where birthday is not null;

函数:

*:经过函数修饰的值一定要起别名
聚组函数:组函数:聚簇函数:多行函数      分组:group by
max() 最大值  min() 最小值  avg() 平均值  sum() 求和  count() 求记录数
select max(sal) maxsal,min(sal) minsal,avg(sal) avgsal,sum(sal) sumsal
from student;

select count(*) num from student;
select count(birthday) num from student;
select count(0) num from student;

单行函数:

ceil():向上取整
floor():向下取整
select floor(12.3) num from dual;

abs():求绝对值
select abs(-666) num from dual;

power(a,b):求a的b次方
select power(2,3) num from dual;

sqrt():求正平方根
select sqrt(16) num from dual;

sign():求符号位   正数返回1,负数返回-1,零返回0
select sign(-666) num from dual;

mod():取余
select mod(12,7) num from dual;

round():求四舍五入
select round(3.141592654) num from dual;
select round(3.141592654,3) num from dual;

trunc():求直接截断
select trunc(3.141592654) num from dual;
select trunc(3.141592654,3) num from dual;

日期函数:

两个日期之间可以相减,单位是天

sqlplus中修改日期展示格式:
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
永久修改方法:详见环境变量

yyyy   年   year  
mm   月    month'月'的月份
ddd   日    年中的日
dd     日    月中的日
d       日    周中的日
hh24  24小时制
hh      12小时制
mi      分 
ss       秒
xff      毫秒
ff3      毫秒保留三位   

add_months():在某个日期添加多少个月
select add_months(sysdate,3) time from dual;

months_between():两个日期之间有多少个月
select months_between(sysdate,to_date('20220928','yyyy-mm-dd')) time from dual;

next_day():下一个周几是哪天
select next_day(sysdate,'星期三') time from dual;

last_day():给定日期所在月份的最后一天
select last_day(sysdate) time from dual;

练习:
--10查询各月倒数第3天入职的员工信息

字符函数:

upper():转换成大写
lower():转换成小写
initcap():首字母大写
length():求长度

select name,upper(name) uname,lower(name) lname,initcap(name) iname,
length(name) lenname from student;

substr(a1,a2,a3):截取字符串
a1:原字符串
a2:从哪开始
a3:截取长度
select substr('woshizhizhuxia',3) str from dual;
select substr('woshizhizhuxia',3,9) str from dual;

replace(a1,a2,a3):替换字符串
a1:原字符串
a2:要替换的字符
a3:替换成的字符
select replace('woshizhizhuxia','h') str from dual;
select replace('woshizhizhuxia','h','000') str from dual;

instr(a1,a2,a3,a4):索引字符串
a1:原字符串
a2:想要找到的字符
a3:从哪开始
a4:第几次出现
select instr('woshizhizhuxia','h') str from dual;
select instr('woshizhizhuxia','h',5) str from dual;
select instr('woshizhizhuxia','h',5,2) str from dual;

concat(a1,a2):拼接字符串
select concat(name,sal) str from student;
select concat(concat(name,birthday),sal) str from student;

lpad(a1,a2,a3):左侧补全
rpad(a1,a2,a3):右侧补全
a1:原字符
a2:想要补全到多少位
a3:拿什么来补全

trim():默认去除两侧空格
trim(a1 from a2):把a2的两侧去除a1

ltrim():左侧去除
rtrim():右侧去除


转换函数:

to_number():将一个字符类型的数值转换成数值类型

to_char():
1.将一个数值类型转换成字符类型
select sal from student where to_char(sal) = '6500';
2.将一个日期类型转换为字符类型
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')  time from dual;
3.格式化字符串 常用在货币单位
select to_char('10000000','999,999,999,999.999') from dual

to_date():将一个字符类型的日期转换为日期类型
select to_date('20221030172742','yyyy-mm-dd hh24:mi:ss') time from dual;

通用函数:

通用函数
nvl(原字符串,是空展示什么) : 空值处理
nvl2(原字符串,不是空展示什么,是空展示什么):空值处理2

查询关键字的优先级:
select 列名 – 优先级最低
from 表名 – 优先级最高
where 条件 – 优先级次高
group by 分组 – 优先级次于where

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DMr.Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值