SQL PLUS的部分简单命令
导出单个表
exp userid=scott/scott@myorcl tables=(emp) file=d:\emp.dmp
--快速导出数据表
exp userid=scott/scott@myorcl tables=(em) file=d:\emp.dmp
direct=y
导出方案
exp scott/scott@myorcl owner=scott file=d:\ems.dmp
导出权限问题
exp system/myorcl@myorcl owner=(system,scott,sys) file=d:\ss.dmp
导出数据库
exp userid=system/myorcl@myorcl full=y inctype=complete file=d:\fu.dmp
导入表:
imp userid=scott/scott@myorcl tables=(emp) file=d:\emp.dmp
导入表结构
imp userid=scott/scott@myorcl talbes=(emp) file=d:\emp.dmp rows=n
导入方案
imp userid=scott/scott@myorcl file=d:\scottFA.dmp
导入其它方案
要求该用户具有DBA权限
imp userid=system/myorcl@myorcl file=d:\system.dmp fromuser=system
touser=scott
导入数据库(自动创建数据库实例)
imp userid=system/myorcl full=y file=d:\sysdb.dmp
user_tables:当前用户所对应方案的所有表
all_tabls:当前用户能访问的的有表
dba_tables:显示所有方案的表,要求用户有DBA权限
查看表结构
DESC DBA_USERS
dba_sys_privs:显示用户所具有的系统权限
dba_tab_privs:显示用户具有的对象权限
dba_col_privs:显示用户具有的列权限
dba_role_privs:可以显示用户所具有的角色
显示oracle中所有的角色
select * from dba_roles;
显示oracle中所有的系统权限
select * from system_privilege_map order by name;
显示oracle中所有的对象权限
select distinct privilege from dba_tab_privs;
一个角色包含的系统权限:
select * from dba_sys_privs where grantee='CONNECT';
SELECT * FROM ROLE_SYS_PRIVS WHERE ROLE='CONNECT';
一个角色包含的对象权限:
SELECT * FROM DBA_TAB_PRIVS WHERE GRANTEE='CONNECT';
oracle有多少角色:
select * from dba_roles;
如何查看某个用户,具有什么样的角色?
select * from dba_role_privs where grantee='SCOTT';
显示当前用户可以访问的所有数据字典视图:
select * from dict where comments like '%grant%';
显示当前数据库的全称:
select * from global_name;
表空间:
create tablespace sp001 datafile 'd:\sp001.dbf' size 20m uniform size
128k;
create table mypart(deptno number(2),dname varchar2(14),loc varchar2
(13)) tablespace sp001;
表空间脱机
alter tablespace sp001 offline;
使用表空间
alter tablespace sp001 online;
--表空间只读
alter tablespace sp001 read only;
--表空间可读可写
alter tablespace sp001 read write;
表空间的所有表
select table_name from all_tables where tablespace_name='SP001';
查看该表属于那个表空间
select tablespace_name,table_name from user_tables where
table_name='MYPART';
删除空的表空间
drop tablespace sp001 including datafiles;
删除表空间及其所有对象
drop tablespace sp001 including contents and datafiles;
create table goods(
goodsId char(8) primary key,
goodsName varchar2(30),
uniprice number(10,2) check(uniprice>0),
category varchar2(8),
provider varchar2(30)
);
create table customer(customerid char(8) primary key,
name varchar2(50) not null,
addredd varchar2(50),
email varchar2(50) unique,
sex char(2) default '男' check(sex in('男','女')),
cardId char(18)
);
create table purchase(
customerId char(8) references customer(customerId),
goodsId char(8) references goods(goodsId),
nums number(10) check(nums between 1 and 30)
);
修改字段为非空
alter table goods modify goodsName not null;
给字段添加唯一约束
alter table customer add constraint cardunique unique(cardId);
给字段添加约束
alter table customer add constraint addresscheck check('海淀','朝阳');
创建索引
单列索引
create index index_name on emp(ename);
复合索引
create index index_name on emp(ename,job);
索引的缺点:
1.建立索引,系统要占用大约为表的1.2倍的硬盘和内存空间来保存索引。
2.更新数据的时候,系统必须要有额外的时间来同时对索引进行更新,以维持数
据和索引的一致性。
显示数据库的所有索引
select * from dba_indexs;
显示当前用户的所有索引
select * from user_indexs;
查询表的索引
select * from user_indexs where table_name='table_name';
显示索引列
select * from user_ind_columns where index_name='index_name';
创建用户:
create user ken identified by ken;
create user tom identified by tom;
授权系统权限:
允许用户继续下发所拥有的权限
grant create session,create table to ken with admin option;
grant create view to ken;
回收系统权限:(tom能登录)
revoke create session from ken;
授权 对象权限:
常用的对象权限有:
alter delete select insert update index references execute
授权:
grant select on emp to monkey;
grant all on emp to monkey;
允许被授权用户,将自己的权限继续传递
grant index on scott.emp to monkey with grant option;
授权表的列权限:
grant update on emp(sal) to monkey;
收回对象权限:(级联回收,与回收系统权限不一样)
revoke select on emp from monkey;
角色与权限:将权限绑定到自定义角色中
建产角色 不验证
create role roleName not identified;
建产角色 数据库验证
create role roleName identified by shunping;
给用户绑定角色
grant roleName to userName with admin option;
删除角色
drop role roleName;
删除角色后,角色对应的用户授权在不在?
删除后,角色对应的用户将没有角色对应的权限。
pl/sql:
procedural language/sql 过程化语言
输出的
dbms_output.put_line('Hello World!');
打开输出选项:
set serveroutput off;
no_data_found异常处理:
begin
v_name varchar2(40);
begin
select name into v_name from dual;
exception
when no_data_found then
dbms_output.put_line('朋友,你的编号输入有误!');
end;
过程:
create procedure sp_pro3(spName varchar2,newSal number) is
begin
update emp set sal=newSal where ename=spName;
end;
调用过程:
方式1.exec sp_pro3('SCOTT',4567);
方式2.CALL sp_pro3('SCOTT',4567);
create table users1(userNo number,userName varchar2(40));
loop循环 至少执行一次
create or replace procedure sp_pro6(spName varchar2)
is
v_num number:=1;
begin
loop
insert into users1 values(v_num,spName);
exit when v_num=10;
v_num:=v_num+1;
end loop;
end;
WHILE循环 先判断条件是否成立,成立后再循环
create or replace procedure sp_pro7(spName varchar2)
is
v_num number:=11;
begin
while v_num<=20 loop
insert into users1 values(v_num,spName);
v_num:=v_num+1;
end loop;
end;
FOR循环
begin
for i in reverse 1..10 loop
insert into users1 values(i,'NB');
end loop;
end;
顺序控制语句 goto null
declare
i int:=1;
begin
loop
dbms_output.put_line('输出i='||i);
if i=10 then
goto end_loop;
end if;
i:=i+1;
end loop;
<<end loop>>
dbms_output.put_line('循环结束');
end;
create table book
(
bookId number,
bookName varchar2(50),
publishHouse varchar2(50)
);
in 表示这是一个输入参数(默认是IN)
out 表示这是一个输出参数
create or replace procedure sp_pro8(
spBookId in number,
spBookName in varchar2,
spPublishHouse in varchar2)
is
begin
insert into book values(spBookId,spBookName,spPublishHouse);
end;
例外:
exception
when no_data_found
then
dbms_output.put_line('没有找到数据!');
exception
when too_many_rows
then
dbms_output.put_line('数据太多!');
exception
when value_error
then
dbms_output.put_line('变量的长度不足以容纳实际数据!');
logon denied 用户非法登陆
not_logged_on 没登录就执行DML操作
storage_error 超出内存空间
自定义例外:
create or replace procedure ex_test(spNo number)
is
--定义一个例外
MyEx exception;
begin
update emp set sal=sal+1000 where empno=spNo;
if sql%notfound then
raise MyEx;
end if;
exception MyEx then
dbms_output.put_line('没有更新任何用户');
end;
set linesize 120;

浙公网安备 33010602011771号