oracle数据库

在这里插入图片描述
创建表空间
create tablespace ithema
datafile ‘c:\itheima.dbf’
size 100m
autoextend on //自动扩展
next 10m;
//删除表空间
drop tablespace ithema;
//创建用户
create user ithema; //用户名
identified by ithema;//密码
default tablespace ithema;//所在表空间
//给用户授权
oracle数据库中常用角色
connect–连接角色,基本角色
resource–开发者角色
dba–超级管理员角色
//给ithema授予dba角色
grant dba to ithema;
//切换到ithema用户下

oracle数据类型
在这里插入图片描述
创建表:
create table person{
pid number(20),
pname varchar2(10)
}
修改表结构
添加一列
alter table person add (gender number(1));
修改列类型
alter table person modify gender char(1); //char长度不可变,varchar2长度可变
修改列名称
alter table person rename column gender to sex;
删除一列
alter table person drop column sex;

修改表数据
insert into person (pid,pname) values(1,"张三);
commit; //增删改之后一定要commit;
update person set pname=‘李四’ where pid=1;
commit;
三个删除:
delete from person; //删除表中全部数据,先删除数据,会受到索引的影响
drop table person; //删除表
**truncate table person; //先删除表再创建表,效果等同于删除表中全部记录。在数据量大的情况下,尤其在表中带有索引的情况下,该操作效率高。删除表的时候先将索引删除,再删除表,因此可以解决索引对增删改的影响。
用delete删除的数据占用的存储空间还在,还可以回复,而truncate删除后会立即释放存储空间,不能被恢复。
**
序列:
默认从1开始,依次递增,主要用来给主键赋值使用。
序列不真的属于任何一张表,但是可以逻辑和表做绑定
dual:虚标,只是为了补全语法,没有任何意义。
create sequence s_person;
select s_person.nextval from dual; //oracle数据库查询语句必须写from,而mysql不写from也可以。
.nextval:下一个序列 currentval当前序列
在这里插入图片描述
只需要记住第一个,每次增加几。

scott用户,默认密码tiger
解锁scott用户
alter user scott account unlock;
解锁scott用户的密码(此句也可用来重置密码)
alter user scott identified by tiger;
单行函数:作用于一行,返回一个值。
字符函数:
select upper(‘yes’ ) from dual; //小写变大写
select lower(‘YES’) from dual;//大写变小写
数值函数:
select round (26.16,1) from dual; //四舍五入,后面参数表示保留的位数,正数表示小数点向后保留,负数代表小数点向前保留。
select trunc(26.16,1) from dual; // 直接截取,不再看后面位数的数字是否大于5
select mod(10,3) from dual;//求余数
日期函数
//查询emp表中所有员工入职距离现在几天
select sysdate-e.hiredate from emp e; //sysdate系统时间
//算出明天此刻
select sysdate+1 from dual;
//查询emp表中所有员工入职距离现在几月
select month_between(sysdate,e.hiredate) from emp e;
//查询emp表中所有员工入职距离现在几年
select month_between(sysdate,e.hiredate)/12 from emp e;
//查询emp表中所有员工入职距离现在几周
select round((sysdate-e.hiredate)/7 ) from emp e;
转换函数
//日期转字符串
select to_char(sysdate," fm yyyy-mm-dd hh:mi:ss") from dual; //加上fm去掉0
//字符串转日期
select to_date(‘2018-6-7 16:39:30’," fm yyyy-mm-dd hh:mi:ss");
通用函数在这里插入图片描述
select e.sal*12 +nvl(e.comm,0) from emp e; //nvl(e.comm,0) 如果为null,则为0,不为null则值本身
条件表达式:
1.通用写法(mysql和oracle都可以)
//给emp表中员工起中文名
select e.ename,
case e.ename
when ‘ll’ then ‘张三’
when ‘mm’thrn ‘里斯’
else ‘无名’ //可省略,
end
from emp e;
在这里插入图片描述
select e.sal,
case e.sal
when e.sal>3000 then ‘高收入’
else ‘低收入’ //可省略,
end
from emp e;
2.oracle专用
oracle中除了起别名,都用单引号
在这里插入图片描述
多行函数(聚合函数):作用于多行,返回一个值。
select count(1) from emp;
select sum(sal) from emp;
select max(sal) from emp;
select min(sal) from emp;
select avg(sal) from emp;
分组查询:
聚合函数有一个特性,可以把多行记录变成一个值。
//查询出每个部门平均工资
在这里插入图片描述
//查询出平均工资高于2000的部门信息
select e.deptno,avg(e.sal)
from emp e
group by e.deptno
having avg(e.sal)>2000;
所有条件都不能使用别名来判断
在这里插入图片描述
select e.deptno,avg(e.sal)
from emp e
where e.sal>800
group by e.deptno;
where和having的区别:
|where过滤的是分组前的数据,having是过滤分组后的数据,where在group by之前,having在group by之后。|

select e.deptno,avg(e.sal)
from emp e
where e.sal>800
group by e.deptno
having avg(e.sal)>2000;
多表查询:
1.笛卡尔积
select * from emp e,dept d; //两表的乘积
2.内连接
隐式
select * from emp e,dept d where e.id=d.eid;
显示:
select * from emp e inner join dept d on e.id=d.eid;
3.外连接
左外链接
select * from emp left join dept on e.id=d.eid;
右外连接
select * from emp right outter join dept on e.id=d.eid;
内连接和外连接的区别:内连接只是查询了两个表中相匹配的数据,删除了两个表中不相匹配数据。左外链接查询出了左边表所有数据和右边表的匹配数据,如果右边表没有对应数据则用null代替。右外连接,查询出右边表所有数据,如果左边表没有对应数据,则用null对应。
4.oracle中专用外连接
select * from emp e,dept d where e.id(+)=d.eid; //dept表中所有数据,及emp中匹配数据
select * from emp e,dept d where e.id=d.eid(+);//emp中所有数据及dept中匹配数据

自连接:
在这里插入图片描述在这里插入图片描述
select e1.name,d1.dname,e2.name,d2.dname
from emp e1,dept d1,emp e2,dept d2
where e1.mgr=e2.empno and e1.deptid=d1.dnumber and e2.deptid=d2.dnumber;
在这里插入图片描述
select e1.number,e1.name,d1.dname,s1.grade,e2.name,s2.grade
from emp e1,sal s1,dept d,emp e2,sal s2
where e1.mgr=e2.empno
and e1.sal between s1.lo and s1.hi
and e1.did=d.number
and e2.sal between s2.lo and s2.hi;
子查询:
子查询返回一个值
//查询出工资和scott一样的员工信息
select * from emp where sal in(select sal from emp where ename=“scott”);
子查询返回一个集合
在这里插入图片描述
select * from emp where sal in(select sal from emp where depno=10)
子查询查询出一张表
在这里插入图片描述
select t.msal,e.ename,d.dname
from (select deptno,min(sal) msal from emp group by deptno) t,emp e,dept d
where e.deptno=t.deptno
and e.sal=t.msal
and e.deptid=d.dnumber;
分页查询
在这里插入图片描述
在这里插入图片描述
select * from (select rownum rn,t.* from (select rownum,e.* from emp e order by e.sal desc )t where rownum <11) where rn>5;
rownum行号不能写上大于一个正数。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值