近来,操作数据库比较多,总结了一下常用的语句!!!
(1)Oracle的默认用户
用户名:scott 密码: tiger 权限:普通用户
用户名:sys 密码: change_on_install 权限:系统管理员
用户名:system 密码: manager 权限:本地管理员
若直接用 sqlplus sys/change_on_install 登录不成功,可更改为sqlplus sys as sysdba/change_on_install
(2)创建表空间
--创建永久表空间
create tablespace tablename--表空间名称
datafile '路径+文件名.dbf' --文件路径及文件名
size 20480M --表空间大小
AUTOEXTEND ON NEXT 500M --每次自动扩展500M
--创建临时表空间
create temporary tablespace tablename
tempfile '路径+文件名.dbf'
size 500M
(3)扩充表空间
alter tablespace tablename add datafile '路径+文件名.DBF' size 20000M autoextend on next 500M maxsize unlimited;
(4)临时更改表空间大小
ALTER TABLESPACE tablename ADD TEMPFILE '路径+文件名.dbf' size 20000M autoextend on next 50M maxsize unlimited;
(5)删除表空间
--删除空的表空间,但是不包含物理文件
drop tablespace tablespace_name;
--删除非空表空间,但是不包含物理文件
drop tablespace tablespace_name including contents;
--删除空表空间,包含物理文件
drop tablespace tablespace_name including datafiles;
--删除非空表空间,包含物理文件
drop tablespace tablespace_name including contents and datafiles;
--如果其他表空间中的表有外键等约束关联到了本表空间中的表的字段,就要加上CASCADE CONSTRAINTS
drop tablespace tablespace_name including contents and datafiles CASCADE CONSTRAINTS;
参考:https://blog.csdn.net/gbj890229/article/details/6623996
(6)创建用户
--创建用户
create user username identified by password
default tablespace tablename;
(7)删除用户
--删除用户
drop user username cascade;
(8)删除表空间
#删除表空间
drop tablespace tablename including contents and datafiles;
(9)给用户授权
DBA:拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构。
RESOURCE:拥有Resource权限的用户只可以创建实体,不可以创建数据库结构。
CONNECT:拥有Connect权限的用户只可以登录Oracle,不可以创建实体,不可以创建数据库结构。
对于普通用户:授予connect, resource权限。
对于DBA管理用户:授予connect,resource, dba权限。
grant connect,resource to username;
(10)imp导入sql文件
imp username/password file=路径+文件名.sql full=y
(11)查询表空间大小
selecta.a1 表空间名称,c.c2 类型,c.c3 区管理,b.b2/1024/1024 表空间大小M,(b.b2-a.a2)/1024/1024 已使用M,substr((b.b2-a.a2)/b.b2*100,1,5) 利用率from(select tablespace_name a1, sum(nvl(bytes,0)) a2 from dba_free_space group by tablespace_name) a,(select tablespace_name b1,sum(bytes) b2 from dba_data_files group by tablespace_name) b,(select tablespace_name c1,contents c2,extent_management c3 from dba_tablespaces) cwhere a.a1=b.b1 and c.c1=b.b1;
(12)查找某个字段所在的表名
--查找当前用户下某个字段所属的表
select *from user_tab_columns twhere t.COLUMN_NAME = 'CONS_ID';