--创建表空间
create tablespace ssspace
datafile 'G:\ooo\oradata\orcl\ssspace.dbf'
size 10m
autoextend on
next 10m
maxsize unlimited
--创建对象
create role ssrole
grant create session,create table,create sequence,create view to ssrole
--创建用户xx1
create user xx1
identified by xx1
default tablespace ssspace
quota 10m on ssspace
grant ssrole to xx1
--创建用户xx2
create user xx2
identified by xx2
default tablespace ssspace
quota 10m on ssspace
grant ssrole to xx2
--创建表格zztype
create table zztype(
zid number primary key,
nei nvarchar2(40)
)
insert into zztype values(1,'文学')
insert into zztype values(2,'历史')
insert into zztype values(3,'小说')
insert into zztype values(4,'科幻')
select * from zztype
--创建表格zz
create table zz(
id number primary key,
name nvarchar2(50) not null,
age number,
sal number,
sex char(1) default 1 check (sex in(0,1)),
sf number unique,
zzid number references zztype(zid)
)
--创建序列
create sequence zzs
start with 1
increment by 1 --步长
maxvalue 2000
cycle
minvalue 1
cache 10
insert into zz values(zzs.nextval,'李白',28,1000,0,123,1)
insert into zz values(zzs.nextval,'杜甫',26,2500,1,456,3)
insert into zz values(zzs.nextval,'白居易',31,500,0,789,2)
insert into zz values(zzs.nextval,'王安石',35,null,1,234,1)
insert into zz values(zzs.nextval,'左左',23,3000,1,345,3)
insert into zz values(zzs.nextval,'星星',28,3500,0,567,2)
select * from zz
--找三组中的最高工资的人的信息
select * from zz where sal=(select max(sal)from zz where zzid=3)
--女性中的最高工资的员工
select * from zz where sal=(
select min(sal)from zz where sex=0)
--大于平均工资的人,所在组的总工资
select zzid,sum(sal)from zz where sal>(select avg(sal)from zz)group by zzid
--按sal从高到低排列,找出4-6员工信息
select t.* from(select e.*,rownum r from(
select * from zz order by zzid desc)e)t
where t.r>4 and t.r<=6
--视图只显示姓名和工资
create view zzv
as
select name,age from zz
--查看视图
select * from zzv
--全连接两个表
select * from zz full outer join zztype on zz.id = zztype.zid
select * from zz right join zztype on zz.id = zztype.zid
select * from zz left join zztype on zz.id = zztype.zid
select * from zz,zztype where zz.id = zztype.zid(+)--左
select * from zz,zztype where zz.id(+) = zztype.zid --右
--增加一个字段写入生日(用字符转型)
alter table zz add birthday date
update zz set birthday = to_date('1989-5-5','yyyy-mm-dd')where id=1
update zz set birthday= to_date('1994-7-11','yyyy-mm-dd')where id=6
--删除生日列
alter table zz drop column birthday
select * from zz
--把空值改为0(sal、birthday)
--nvl
--默认不显示
select * from zz order by nvl (sal,0)desc
--在sal的列中,判断为null时,把空值变成0
select name, nvl (sal,0)from zz
--nal2
--在sal的列中,判断为null时,把空值变成0,否则原值
select name, nvl2(sal,sal,0)from zz
--decode必须是等值判断,把左左改为左雨萌,其他值不变
select name,decode (name,'左左','左雨萌',name)from zz
--把左左改为左雨萌,其他值改为宋亮星
select name,decode(name,'左左','左雨萌','宋亮星')from zz
--把 3500改成0,把空值改为200,再返回其他值
select name,decode (sal,3500,0,null,200,sal)from zz
--case 返回原值,空值改为0
select name,case sal when 1000 --判断是不是存在1000
then 10000 --存在的话改为10000
else 0 --其他为0
end sal
from zz
--返回原值,空值改为0
select name,case sal when sal
then sal
else 0
end sal
from zz
--把左左改为xxx,把星星改为ooo,其他改为不知道
select case name when '左左' then 'xxx'
when '星星' then 'ooo'
else '不知道'
end name
from zz
--xx1把表格zz的所有权限,给用户xx2
grant all on zz to xx2
--切换用户xx2用户实现查看用户xx1中zz的表格
select * from xx1.zz