oracle数据库表和表数据以及事物,视图

--先用用system账户登录

  

select * from scott.emp;--可以在system下查其他普通用户的表
create tablespace tb1
datafile 'c:/oracle/tb1.dbf'
size 100M
autoextend on
next 5M


create user zhangsan identified by zhangsan;
grant connect to zhangsan;
grant dba to zhangsan;
default tablespace tb1;


create table zs (
username varchar2(20) unique,
password varchar2(11) not null,
telephone number 
);


drop table zs;
select * from zs;
insert into zs values('张小三','123456',123);
---- 添加列  add
alter table zs add userId varchar2(11);
--修改列
alter table zs modify userId long; 


--重命名列
alter table zs rename column userId to userID;


--重命名表
rename zs to zhangsan;
select * from zhangsan;
select * from scott.emp;


--使用子查询创建表,相当于复制表结构和表数
create table zsemp as select * from scott.emp;
select * from zsemp;
drop table zsemp;


--使用子查询创建表,值复制其表结构c
create table zsemp2  as select * from scott.emp where 1=2;
select * from zsemp2;


--表约束 unique not null primary key check forine key
create table person(
 pid number primary key,
 pname varchar2(20) unique,
 age number not null,
 gender char(8) check(gender in ('男','女','妖'))  
);
select * from ptype;
select * from person;
--表的外键约束
create table ptype(
typeno number unique,
typeval varchar2(11)
);
alter table ptype modify typeval varchar2(20);
insert into ptype (typeno,typeval) values (1,'魔鬼中的天使');
insert into ptype (typeno,typeval) values (2,'天使中的魔鬼');


--给上面person增加字段,作为外键指向ptype表的typeno(在此要特别注意,在给主表添加外键时候从表不能有数据)
delete ptype where ptype.typeno=2;
delete ptype where ptype.typeno=1;
alter table person add foreign key (typeno) references ptype (typeno);


-- 强制删除ptype表: 先断开所有外键约束,然后再删除自己
drop table ptype cascade constraint;


--使用子查询插入已有的表中的数据(将scott表中10 号部门的员工所有信息都插入到zsemp2中)
insert into zsemp2  select * from scott.emp where deptno=10;
select * from zsemp2;


update zsemp2 set sal = sal+50;
update zsemp2 set sal=sal - 1000 where empno = 7839;


--oracle中的事物  oracle默认的隔离级别是readCommited;
-- savepoint 保存点    rollback to 保存点
create table fool(
myfool number primary key
);
declare --声明部分
begin   --业务逻辑
insert into fool values (1);
insert into fool values(2);
insert into fool values(3);
savepoint mysavepoint;
insert into fool values(4);
insert into fool values(5);
insert into fool values(5);
insert into fool values(6);
commit;
 exception --异常部分,回滚到保存点
   when others then 
  rollback to mysavepoint;
  commit;
end;
truncate table fool;
select * from fool;


--视图和同义词
--视图可以屏蔽一部分源表的数据,不显示出来.它本质上并不存储数据而是一张从其他表查询出来的表
create [or replace] view 视图名称 as 查询语句 [whith read only]; 


create or replace view newemp as select e.ename,e.deptno,e.hiredate from zsemp e ;
update newemp set ename ='史密斯' where newemp.ename='SMITH';
select * from newemp;--从视图表中查(这里是将视图表改了后显示,正常情况下我们都是设置为只读)


create or replace view newemp2 as select e.ename,e.deptno, e.hiredate from zsemp e with read only;
update newemp set ename ='史密斯2' where newemp.ename='SMITH';--设为只读后,这里的更改就无效了
select * from newemp2;
--同义词(synonym )可以简化复杂的查询语句, 并且可以增加代码被破解的难度
 create or replace view myview as select sum(cc) "TOTAL",
       max(case yy when '1987' then cc end) "1987",
       max(case yy when '1980' then cc end) "1980",
       max(case yy when '1982' then cc end) "1982",
       max(case yy when '1981' then cc end) "1981"
from
(select to_char(hiredate,'yyyy') yy,count(*) cc from emp group by to_char(hiredate,'yyyy')) tt;
create synonym aabb for myview;--给我们刚才的视图取别名, 
select * from aabb;

猜你喜欢

转载自blog.csdn.net/fzq_javaee/article/details/77897352
今日推荐