Oracle复习

基本语句
desc t; 查看表结构
drop table t; 删除表
delete table t; 删除表中的数据保留表结构
DML语句(数据操作语句)
insert(添加字段),update(用于修改已有字段),Delete ,Merge
DDL语句(数据定义语句)在执行DDL语句之前所有未提交的事物都会自动提交
Create,Alter(修改表),Drop,Truncate
DCL语句(数据控制语句)
Grant(赋值,给用户赋予角色),Revoke
事务控制语句
Commit,Rollback,Savepoint,
约束
check,not null,foreign key外键约束,Unique唯一约束,
select * from tabel(dbms_xplan.display); 查看执行计划
存储空间
表空间 -> 段->区->块
1.查询表空间的信息
select * from dba_tablespaces;
select * from dba_data_files;–表空间和数据文件关系
2.查询段信息
select * from user_segments;–注意分区表的情况
3.查询区信息
select * from user_extents;
4.查询记录和文件及数据块
select t.* ,dbms_rowid.rowid_relative_fno(rowid),dbms_rowid.rowid_block_number(rowid) from t;
5.创建表空间
create tablespace TS1 datafile ‘G:\Ts1.dat’ size 10M autoextend on;
6.增加一个文件到表空间
alter tablespace TS1 add datafile ‘G:\Ts1_ex.dat’ size 10M autoextend on next 1M;
7.修改表空间属性
alter tablespace TS1 offline;tabl
alter tablespace TS1 online;
alter tablespace TS1 read only;
alter tablespace ts1 read write;
drop tablespace ts1 including contents and datafiles;
8.修改用户的默认表空间
alter user zhx default tablespace ts1;
–可以创建表查证
9.将表创建在指定的某个表空间上
create table t(id int ) tablespace users;
10.创建分区表
–hash
create table t(id int,name varchar2(40)) partition by hash(id)(partition p1 tablespace ts1,partition p2 tablespace users);
select * from user_segments–查看segment情况
select * from t partition(p1);
-查看不同的记录在不同的文件和表空间上
select t.* ,dbms_rowid.rowid_relative_fno(rowid),dbms_rowid.rowid_block_number(rowid) from t;

create table y(
sid int primary key, 主键约束(主键约束=空值约束+唯一约束)
sname varchar(10) not null,
sage int check(sage between 12 and 20),检查约束
s int unique 唯一约束
);
create table s as select * from emp; 复制表
create table d as select * from emp where empno=0000; 复制表结构,没有数据。
TRUNCATE TABLE Y;截断Y表;
Rename Y to T;把表Y的名字改成T;
Comment on table Y is ‘添加注释’;向表中添加注释。Comments on Y.id is’添加注释’; 向Y表中id的列中的注释;
select * from user_tab_cooments where table_name=‘Y’; 查询表的注释
修改表可以添加新列和添加一个虚拟列,删除列,添加和删除约束
Alter table Y ADD sname varchar(12); 添加列;
Alter table Y Drop Column(列的关键字) sname ; 删除列
Alter table Y add (avsal as (lowsal+hightsal)); 添加一个虚拟列,虚拟列只能引用表中已有的列,
Alter table Y Add constraint(约束关键字) y_sage_nn not null; 添加约束
Alter table Y Drop constring Y_sid; 删除约束
Alter table Y disable constrtion not null; 禁用约束
Alter table Y enable constrtion not null; 启用约束
user_constrtion 查询约束相关信息,
修改列-可以修改列的长度,数值列的精度,数据的类型,默认值等。
Alter table Y Modify(修改关键字) id number; 修改列
序列
create sequence(序列关键字) tt(序列名) start with 0 increment by 2 minvalue 1 maxvalue 20 cycle cache 5;
序列不可以修改初值,
索引
create index z on a(id); 建立索引(可以加快表中行的访问速度,但是也是要付出代价的),
视图
简单视图:包含一个子查询,她是从一个基本表中检索数据。
复杂视图:包含一个子查询,从多个基本表中检索数据,包含函数,group by或者distinct子句对行进行分组。
create or replace view ss as select id,price from t where price<30 with check option constraint ; 创建已具有约束的视图
试图约束:Check Option约束(必须满足子查询条件),Read Onl约束(指定视图是只读)
grant create materialized view to zhx; 物化视图需要授权
create materialized view mvi_stu_dept_avg refresh force on commit as select count(sage) cnt,deptno,avg(sage) as v_age from stu group by deptno; 创建一个聚集物化视图
Describe SS;获取视图的信息
块结构
declare(变量的关键字)
a int :=(赋值的写法)0;
begin
for a in 0…5 loop
dbms_output.put_line(‘a=’||a); 在这个里面可以写语句,结构是无名的所以无法保存。
end loop;
end;
declare
type emp_record_type is record(nama emp.ename%type,salary emp.sal%type);自定义数据类型
type emp_table_type is table of emp_record_type;自动数据类型数组
emp_table emp_table_type;
begin
select ename,sal bulk collect into emp_table from emp; 把查询的结果批量放入数组中
for i in 1…emp_table.count loop 遍历数组
dbms_output.put_line('Name: ’ || emp_table(i).nama || ’ Salary: ’ || emp_table(i).salary);
end loop;
end;
过程和函数
过程和函数这两者有很大的相似之处,都是存储的是一段子程序,唯一区别就是函数必须调用其返回语句
过程:
create or replace procedure aa(n number,res out number)as out 是输出变量
begin
res:=n2;
end;
函数:
create function ee(n number)return number as
m number;
begin
m:=n
4;
return m;
end;
游标
–通过游标修改数据
1.声明时要加for update
2.更改数据时要加where current of cursor_name
–根据部门编号给低于100的人增加工资
create or replace procedure updateSalByDeptno(dno emp.deptno%type,addSal emp.sal%type) is 创建一个过程
cursor emp_cursor is select sal from emp where deptno = dno for update; 声明一个游标
v_sal emp.sal%type; 声明一个变量
begin
open emp_cursor; 打开游标
loop 循环
fetch emp_cursor into v_sal; 从游标中获取行
exit when emp_cursor%NOTFOUND ; 条件退出循环
if v_sal < 100 then
update emp set sal = v_sal + addSal where current of emp_cursor; 使用游标更新数据
end if;
end loop;
close emp_cursor; 关闭游标
end;
–使用基于游标定义的记录变量
–用游标方式显示姓名和工资(批量提取)
declare
cursor emp_cur_ename_sal is select ename,sal from emp;
type emp_table_ename_sal is table of emp_cur_ename_sal%rowtype;
emp_table emp_table_ename_sal;
begin
open emp_cur_ename_sal;
fetch emp_cur_ename_sal bulk collect into emp_table;
for i in 1…emp_table.count loop
dbms_output.put_line('name: ’ || emp_table(i).ename || ’ sal: ’ || emp_table(i).sal);
end loop;
close emp_cur_ename_sal;
end;
–游标for 循环
declare
cursor emp_cursor is select empno,ename,sal from emp;
begin
for emp_record in emp_cursor loop–自动打开,且不用定义变量
dbms_output.put_line('Name: ’ || emp_record.ename || ’ Sal: ’ || emp_record.sal );
end loop; --自动关闭
end;
——————————————————————————————————————————————————————————————————————————————————————————————————————————————— Oracle笔试复习
在这里插入图片描述

* Oracle服务器

	* 实列   

		* 内存
		* 后台进程
	* 数据库


* 

Oracle文件

	* 

数据文件
*
配置文件
*
参数文件
*
控制文件
*
存储空间

	* 

表空间 -> 段->区->块 I/O单位是块 分配单位是块
*
闪回

	* 

怎么闪回
*
表删除
*
做DML闪回
*
索引

	* 

索引的创建
*
索引的的种类,有什么用,适用于什么场合
*
角色

	* 

安全,创建,授权,查看角色,
*
对象的权限,系统的权限
*
临时表

	* 

创建,作用,临时表占不占空间
*
序列

	* 

创建,
*
同义词——别名

	* 

共有同义词,私有同义词
*
SQL块,函数,存储过程等创建,使用,作用。
*
游标

	* 

批量处理,更改,删除,DML
*
触发器
*
异常

	* 

预定义异常
*
非预定义异常
*
自定义异常
*
脚本
*
创建用户

	* 

为其授权
*
指定表空间(创建,指定存储大小)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值