sqlplus常用命令

1.登陆系统用户
sqlplus 然后输入系统用户名和密码
登陆别的用户
conn 用户名/密码;
2.创建表空间
create tablespace 空间名
datafile 'c:"空间名' size 15M --表空间的存放路径,初始值为15M
autoExtend on next 10M --空间的自动增长的值是10M
permanent online; --永久使用
3.创建用户
create user shi --创建用户名为shi
identified by scj --创建密码为scj
default tablespace 表空间名 --默认表空间名
temporary tablespace temp --临时表空间为temp
profile default --profile文件的限制
quota unlimited on 表空间名; --在表空间下面建表不受限制
4.创建角色
create role 角色名 identified by 密码;
5.给角色授权
grant create session to 角色名;--给角色授予创建会话的权限
grant 角色名 to 用户名; --把角色授予用户
6.给用户授予权限
grant create session,resource to shi;--shi用户授予所有权限
grant create table to shi; --shi用户授予创建表的权限
7.select table_name from user_tables; 察看当前用户下的所有表
8.select tablespace_name from user_tablespaces; 察看当前用户下的 表空间
9.select username from dba_users;察看所有用户名称命令 必须用sys as sysdba登陆
10.创建表
create table 表名
(
id int not null,
name varchar2(20) not null
)tablespace 表空间名 --所属的表空间
storage
(
initial 64K --表的初始值
minextents 1 --最小扩展值
maxextents unlimited --最大扩展值
);
11.--usrs表添加主键和索引
alter table users
add constraint pk primary key (ID);
12.为已经创建users表添加外键
alter table users
add constraint fk_roleid foreign key (roleid)
references role(role_id) on delete cascad; --下边写主表的列
on delete cascad是创建级联
13.把两个列连接起来
select concat(name,id) from 表名; --nameid连接起来
14.截取字符串
select column(name,'') from 表名; --name中的去掉
15.运行事务之前必须写
set serveroutput on; --打开输入输出(不写的话,打印不出信息)
16.while的应用
declare --声明部分
ccc number:=1; --复职
a number:=0;
begin --事务的开始
while ccc<=100 loop --循环
if((ccc mod 3)=0) then --条件
dbms_output.put_line(ccc||','); --打印显示
a:=a+ccc;
end if; --结束if
ccc:=ccc+1;
end loop; --结束循环
dbms_output.put_line(a);
end; --结束事务
/
17.select into 的用法 --只能处理一行结果集
declare
name varchar(30);
begin
select username into name
from users
where id=2;
dbms_output.put_line('姓名为:'||name);
end;
/
18.利用%rowtype属性可以在运行时方便的声明记录变量和其他结构
Set serveroutput on;
Declare
utype users%rowtype;
Begin
Select * into utype from users where id=20;
Dbms_output.put_line('姓名'|| utype.username);
Dbms_output.put_line('生日'|| utype.brithday);
end;
/ --%rowtype想当于复制一个表
19.游标的定义和使用
Declare
Cursor ucur is select * from users; --声明游标
Us users%rowtype;--定义与游标想匹配的变量
Begin
Open ucur;--打开游标
Fetch ucur into us;
While ucur %found loop --使用循环遍历游标的查询结果
Dbms_output.put_line('姓名:'||us.username||'生日'||us.brithday);
Fetch ucur into us;
End loop;
Close ucur; --关闭游标
End;
=======================================
%found在前一条的fetch语句至少对应数据库的一行时,%found属性值为true,否则为false;
% notfound 在前一条fetch语句没有对应的数据库行时,%notfound属性值为true,否则为false;
%isopen 在游标打开时%isopen属性值为true;否则为false;
%rowcount显示迄今为止从显示游标中取出的行数

20.
删除
drop tablespace 空间名 including contents; --删除表空间和里面的内容
drop table 表名 --删除表

drop user 用户名 --删除用户

-------------------------------------

使用Oracle用户
# su - oracle

创建表空间 -&gt; 创建新用户 -&gt; 用户授权

一 创建表空间
SQL&gt; CREATE TABLESPACE test01
DATAFILE '/data/oracle/oradata/db/test01.dbf' SIZE 1024M UNIFORM SIZE 128k;

1) DATAFILE: 表空间数据文件存放路径
2) SIZE: 起初设置为1G就可以
3) UNIFORM: 指定区尺寸为128k,如不指定,区尺寸默认为64k
4) 空间名称 test01 与 数据文件名称 test01.dbf 不要求相同,可随意命名.


通过ALTER DATABASE修改空间扩展大小


SQL &gt; ALTER DATABASE DATAFILE '/data/oracle/oradata/db/test01.dbf' ' AUTOEXTEND ON;


1)AUTOEXTEND ON:在Oracle10g中,需要手动启动分区自动扩展功能.
二 创建新用户
SQL&gt; CREATE USER test IDENTIFIED BY 123456 DEFAULT TABLESPACE test01;

1) 创建用户同时分配表空间

三 用户授权
1)用户角色授权
SQL&gt; CRANT CONNECT,RESOURCE TO test;

a) 我这里将CONNECT和RESOURCE角色赋给新用户test,test将拥有这两个角色的操作权限.

2)直接授权
多用户
SQL&gt; CRANT SELECT,INSERT,UPDATE,DELETE,ON USERS TO test,test1;

下面我们可以使用两种方式登录Oracle db
一 SQLPLUS
1)本地登录
SQL&gt; sqlplus test/123456

2)远程登录
假设db在另一台服务器
SQL&gt; sqlplus test/123456@db

二 PLSQL Developer
上篇中也有讲述,输入账号口令就可以了.


下面说些和上面相关,且工作中会需要用到的.
一 删除用户
SQL&gt; DROP USER test CASCADE;

二 删除表空间
SQL&gt; DROP TABLESPACE test01 INCLUDING CONTENTS AND DATAFILES;

1) 删除表空间内容和数据文件.
2) 一般无效表空间占用磁盘空间,所以这个应该很常用.




查看用户角色权限


select * from user_role_privs;


查看用户表权限


select * from user_tab_privs;

-------

/*   Formatted   on   2001/12/03   09:00   (Formatter   Plus   v4.5.2)   */  
  CREATE   TABLESPACE   sini_data   DATAFILE  
  'D:\oracle\oradata\sini_data.dbf'   SIZE   500m  
  AUTOEXTEND   ON   NEXT   1m   MAXSIZE   UNLIMITED  
  DEFAULT   STORAGE  
      (  
          INITIAL   128k  
          NEXT   1m  
          MINEXTENTS   1  
          MAXEXTENTS   UNLIMITED  
          PCTINCREASE   0  
      );  
  /  
  CREATE   TABLESPACE   sini_index   DATAFILE  
  'D:\oracle\oradata\sini_index.dbf'   SIZE   200m  
  AUTOEXTEND   ON   NEXT   1m   MAXSIZE   UNLIMITED  
  DEFAULT   STORAGE  
      (  
          INITIAL   128k  
          NEXT   1m  
          MINEXTENTS   1  
          MAXEXTENTS   UNLIMITED  
          PCTINCREASE   0  
      );  
  /  
  CREATE   TABLESPACE   sini_temp   DATAFILE  
  'D:\oracle\oradata\sini_temp.dbf'   SIZE   200m  
  AUTOEXTEND   ON   NEXT   1m   MAXSIZE   UNLIMITED  
  DEFAULT   STORAGE  
      (  
          INITIAL   128k  
          NEXT   1m  
          MINEXTENTS   1  
          MAXEXTENTS   UNLIMITED  
          PCTINCREASE   0  
      );  
  /  
  ALTER   TABLESPACE   sini_temp   TEMPORARY;  
  /  
  CREATE   USER   bingo   IDENTIFIED   BY   bingo   DEFAULT   TABLESPACE   sini_data   TEMPORARY   TABLESPACE   sini_temp;  
  /  
  GRANT   CONNECT,RESOURCE   TO   bingo;  
  /  
  GRANT   CREATE   TABLE   TO   bingo;  
  /  
  ALTER   USER   bingo   QUOTA   UNLIMITED   ON   sini_data;  
  /  
  ALTER   USER   bingo   QUOTA   UNLIMITED   ON   sini_index;  
  /  
  ALTER   USER   bingo   QUOTA   UNLIMITED   ON   sini_temp;  
  /  

下面两句语句1.生成一个名为TbsUserData的表空间,   2.授权用户User1无限制使用该表空间  
   
  CREATE   TABLESPACE   TbsUserData  
          DATAFILE   'd:\data\UserData.dbf'   50   M  
          DEFAULT   STORAGE   (INITIAL                           2   M    
                                            NEXT                                 2   M    
                                            PCTINCREASE                   10)  
  /  
   
  ALTER   USER   User1   Quota   Unlimited   on   TbsUserData  
  /    

 
原文地址:https://www.cnblogs.com/blsong/p/1598884.html