一、常用
1、常用sql
1、修改密码
alter user test identified by 123456;
Windows用cmd处理下密码过期:
sqlplus 用户名/密码@localhost/orcl;
alter user 用户名 identified by 密码;
特殊字符修改密码:alter user committee identified by "Sinosoft@876"
2、用户解锁
alter user test account unlock;
3、分页查询
单表:
SELECT * FROM (SELECT t.*,ROWNUM rn FROM TABLE t WHERE ROWNUM <= pageNumber*pageSize) WHERE rn >(pageNumber)*pageSize;
两张表:
SELECT * FROM (
SELECT ROWNUM RN,XX.* FROM (
SELECT 表名.字段名, 表名.字段名, 表名.字段名... FROM TABLE1 t1, TABLE2 t2 WHERE t1.字段=t2.字段
) XX WHERE ROWNUM<=pageSize*pageNumber
) WHERE RN >(pageNumber-1)*pageSize ;
4、查询用户的用户名、默认表空间、临时表空间
select username,default_tablespace,temporary_tablespace from dba_users where username='YNGW';
查询用户的表空间和所有表名:
select owner,table_name,tablespace_name from dba_tables where owner='YNGW';
5、Linux下Oracle的启动和关闭
1、登录服务器,切换到oracle用户,或者以oracle用户登录
su - oracle
2、打开监听服务(可以通过lsnrctl status命令查看Oracle监听器运行状况)
lsnrctl status
lsnrctl start
3.以SYS用户身份登录Oracle
sqlplus /nolog
conn /as sysdba
4.通过startup命令启动实例
startup
关闭数据库实例 shutdown
关闭监听器 lsnrctl stop
(4) 启动数据库,命令:startup
(5) 如果是关闭数据库,命令:shutdown immediate
(6) 退出sqlplus控制台,命令:exit
(7) 进入监听器控制台,命令:lsnrctl
(8) 启动监听器,命令:start
(9) 退出监听器控制台,命令:exit
6、创建dblink
create database link dblinkwd connect to CEN_INTERFACE identified by "123456"
using ' (DESCRIPTION =
(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) )
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCL)
)
)';
select * from 表名@dblinkwspt
7、建用户、导数据(数据库迁移)
建表空间:
create tablespace NBZCS datafile 'E:\ORADATA\NBZCS.DBF' size 1500M;
建用户:
create user NBZCS identified by "123" default tablespace NBZCS account unlock;
修改表空间:
ALTER TABLESPACE NBZCS ADD DATAFILE 'E:\ORADATA\NBZCS2.DBF' SIZE 800M autoextend on next 10m maxsize unlimited;
--- 查询oracle数据库表空间文件位置
select username,default_tablespace from dba_users where username='用户名';
Select * FROM DBA_DATA_FILES;
--- 给表空间扩容
alter database datafile 'E:\ORADATA\YNGW01.DBF' resize 50000m;
授权:
GRANT DBA,CONNECT,RESOURCE,IMP_FULL_DATABASE,EXP_FULL_DATABASE,SELECT ANY DICTIONARY TO NBZCS ;
创建目录:
将目录授权给用户:
grant read,write on directory DATA_BAK_DIR to NBZCS;
导出命令:
expdp NBZCS/123@orcl directory=DATA_BAK_DIR dumpfile=NBZCS.dmp logfile=NBZCS.log
导入命令:
impdp NBZCS/123@orcl directory=DATA_BAK_DIR dumpfile=NBZCS_20210623.DMP logfile=NBZCS.log schemas=NBZCS
----忽略表空间导入命令
impdp COMMITTEE/123456@orcl directory=DATA_BAK_DIR dumpfile=COMMITTEE_20210619.dmp TRANSFORM=segment_attributes:n logfile=committee.log schemas=committee
----忽略表空间,用户名更改,导入命令:
impdp ele/ele@orcl directory=DATA_BAK_DIR dumpfile=ELE.DMP REMAP_SCHEMA=olduser:newuser TRANSFORM=segment_attributes:n logfile=ele.log
普通导入导出(rows=n代表不要数据,只要结构):
imp mtg_zb/embed@localhost/orcl file=C:\Users\Lenovo\Desktop\sing.dmp full=y ignore=y
exp yngw/yngw@localhost:1521/orcl file=D:\yngw.dmp rows=y owner=yngw
exp yngw/yngw@localhost:1521/orcl file=D:\yngw.dmp tables=(T_SYS_WD_YN_ORG)
exp jkfp_yngw/yngw@localhost:1521/gwqjdb file=D:\yngw.dmp tables=(PUB_BUSIRULEINFO,PUB_QUERYINPARAM)
Linux:
exp sing/embed@localhost:1521/orcl file=/home/oracle/222.dmp rows=y tables='('INDEX_STATISTICS_SOURCE')'
exp yngw/yngw@localhost/orcl tables='('EHR_HIGHRISK_ATTR,EHR_DISEASE_HISTORY,EHR_HISTORY_SHOUSHU,EHR_HISTORY_SHUXUE,EHR_HISTORY_WAISHANG,EHR_FEESOURCE_ATTR')' query=\"where flag=\'dls\'\" file=/bak/bkup/dls20190611.dmp log=/bak/bkup/dls611.log
注意:如果不使用 ignore=y 参数在进行imp 导入时,就会 对已经存在的表就不会进行导入,
如果加上ignore=y 参数就会对已经存在的表中没有的记录进行更新,但对已经存在记录不会进行覆盖修改。
8、删除某个用户、表空间下面的
drop user xxx必须是这个用户下面没有任何对象,这样才可以使用这个命令,否则就会报错;
如果用户下面有对象,就得用drop user xxx cascade来删除这个用户以及这个用户下的所有对象了。
select owner, segment_name from dba_segments where tablespace_name = 'EMR_EHR'
drop user EMR_EHR cascade;
既删除数据文件 表空间 等表或视图的记录又删除了物理文件:
drop tablespace EMR_EHR including contents and datafiles;