Oracle数据库常用的Sql语句整理

Oracle数据库常用的Sql语句整理

查看当前用户的缺省表空间 : select username,default_tablespace from user_users;
2、查看用户下所有的表 : select * from user_tables;
3、创建表空间 :CREATE TABLESPACE invocie DATAFILE '/data/InvoiceData/invoicebak/invocie_01.dbf' Size 4096M AUTOEXTEND OFF;

CREATE TABLESPACE "MIS_DATA" DATAFILE
'/opt/oracle/devdb/oradata/mis_data01.dbf' SIZE 20971520000,
'/opt/oracle/devdb/oradata/mis_data02.dbf' SIZE 20971520000,
'/opt/oracle/devdb/oradata/mis_data03.dbf' SIZE 20971520000,
'/opt/oracle/devdb/oradata/mis_data04.dbf' SIZE 20971520000
LOGGING ONLINE PERMANENT BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT SPACE MANAGEMENT AUTO;

REATE TABLESPACE "MIS_IDX" DATAFILE
'/opt/oracle/devdb/oradata/mis_idx01.dbf' SIZE 20971520000,
'/opt/oracle/devdb/oradata/mis_idx02.dbf' SIZE 20971520000
LOGGING ONLINE PERMANENT BLOCKSIZE 8192
extent management local autoallocate segment space management auto

CREATE TABLESPACE "USER_DATA" DATAFILE
'/opt/oracle/devdb/oradata/user_data01.dbf' SIZE 15728640000,
'/opt/oracle/devdb/oradata/user_data02.dbf' SIZE 20971520000
LOGGING ONLINE PERMANENT BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT SPACE MANAGEMENT AUTO
ALTER DATABASE DATAFILE
'/opt/oracle/devdb/oradata/user_data01.dbf' RESIZE 31457280000
ALTER DATABASE DATAFILE
'/opt/oracle/devdb/oradata/user_data02.dbf' resize 31457280000

CREATE TABLESPACE "USER_IDX" DATAFILE
'/opt/oracle/devdb/oradata/user_idx01.dbf' SIZE 10485760000
LOGGING ONLINE PERMANENT BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT SPACE MANAGEMENT AUTO
ALTER DATABASE DATAFILE
'/opt/oracle/devdb/oradata/user_idx01.dbf' resize 26214400000

 
4、创建用户并赋权限 : CREATE USER invocie_dev identified by invocie_dev default tablespace invocie; grant connect, resource to invocie_dev;

CREATE USER "S6MISM" identified by  “S6MISM”
DEFAULT TABLESPACE "MIS_DATA"
TEMPORARY TABLESPACE "TEMP"

CREATE USER "YYII_S6MISM"  identified by  “YYII_S6MISM”

DEFAULT TABLESPACE "MIS_DATA"
TEMPORARY TABLESPACE "TEMP"

CREATE USER "TEAMB0"  identified by  “TEAMB0”
DEFAULT TABLESPACE "USER_DATA"
TEMPORARY TABLESPACE "TEMP"

5、解锁用户:alter user scott account unlock;
SELECT * FROM dba_users WHERE username = 'SCOTT';
ALTER USER SCOTT account LOCK; --锁定用户
ALTER USER SCOTT account UNLOCK; --解锁用户
COMMIT;
 
6.重设用户密码:scott/tiger为默认用户,alter user scott identified by tiger;
SELECT password FROM dba_users WHERE username = 'SCOTT';
alter user SCOTT identified by new_password; --修改用户密码
7、创建表:create table t1(c1 type 约束,c2 type 约束(not null,unique,check,primary key));

CREATE TABLE "S6MISM"."CUSTOM"
( "FACT_NO" CHAR(4) NOT NULL ENABLE,
"CUSTOM_NO" VARCHAR2(15) NOT NULL ENABLE,
"HKCUSTOM_NO" CHAR(6),
"AREA_CODE" VARCHAR2(5),
"CUSTOM_NAME" VARCHAR2(32) NOT NULL ENABLE,
"TRAN_COUNTRY" VARCHAR2(30),
"DIST_COUNTRY" VARCHAR2(30),
"SIZEMARK_DESC" VARCHAR2(10),
"CHINA_RM" CHAR(1),
"SHIP_TO_ADDR" VARCHAR2(270),
"MARK_NO" CHAR(3),
"BRAND_NO" CHAR(2),
"SPEC_PACKAGE" CHAR(1),
"SIZEMARK1_DESC" VARCHAR2(20),
"AGENT_NAME" VARCHAR2(22),
"APACK_MK" CHAR(1),
"CUNT_CODE" CHAR(3),
"PRINT_MK" CHAR(1) DEFAULT ('Y') NOT NULL ENABLE,
"CUSTOM_CODE" CHAR(12),
"MODIFY_USER" VARCHAR2(60),
"MODIFY_DT" CHAR(14),
"CONSIGNEE" VARCHAR2(400),
"NOTIFY" VARCHAR2(200),
"DIV_CODE" CHAR(4),
"ISGROUP_RMK" CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
"GROUP_FACT_NO" CHAR(4),
"SHIPPER" VARCHAR2(300),
CONSTRAINT "PK_CUSTOM" PRIMARY KEY ("FACT_NO", "CUSTOM_NO", "BRAND_NO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "MIS_IDX" ENABLE,
CONSTRAINT "CKC_CHINA_RM_CUSTOM" CHECK ( CHINA_RM is null or (CHINA_RM in ('Y','N'))) ENABLE NOVALIDATE,
CONSTRAINT "FK_CUSTOM_FK_AREAM__AREAM" FOREIGN KEY ("FACT_NO", "AREA_CODE")
REFERENCES "S6MISM"."AREAM" ("FACT_NO", "AREA_CODE") ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "MIS_DATA"

CREATE TABLE "S6MISM"."BRAND"
( "FACT_NO" CHAR(4) NOT NULL ENABLE,
"BRAND_NO" CHAR(2) NOT NULL ENABLE,
"BRAND_NAME" CHAR(20) NOT NULL ENABLE,
"EDI_CODE" CHAR(1),
"CTN_PAIR" NUMBER(2,0),
"SIZE_KIND" CHAR(1) NOT NULL ENABLE,
"VOUM_NO" VARCHAR2(2) NOT NULL ENABLE,
"TRANHK_CODE" CHAR(4),
"BRAND_CODE" CHAR(4),
"INIT_STYLE_NO" CHAR(7),
"INIT_SIZE_KIND" CHAR(1) DEFAULT '2',
"BRAND_CODE_ADT" VARCHAR2(4),
CONSTRAINT "FK_BRAND" PRIMARY KEY ("FACT_NO", "BRAND_NO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "MIS_IDX" ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "MIS_DATA"

8. 查询:select distinct c1 from t1 where 条件 group by c1 having by 子条件order by c1; 
 SELECT MAIN_MAT_NO,sub_mat_no,level

FROM MATBOM
--where level >1
START WITH sub_MAT_NO = 'A020408G01A059 '
CONNECT BY PRIOR SUB_MAT_NO = MAIN_MAT_NO
;;

9. 连接字符串:select c1 ||c2 from t1;
10.查看当前系统时间 :select sysdate from dual;
11.更新数据操作:插入记录:insert into t1(c1,c2)values(‘’,’’); 
插入一字段:insert into t1(c1,c2) select c3,c4 from t2; 
更新记录:update t1  set c1=’’ where  c1 = ''
删除记录:delete from t1 where;truncate table t1;drop table t1;
12.创建外键: create table t1(dept_no varchar2(4) not null, constraint fk_emp foreign key (dept_no) references t2(dept_no);
 对已经存在表创建外键: alter table t1 add constraint foreign_work_emp foreign key(c1) references t2(c1); 
   删除一个外键: alter table t1  drop constraint foreign_work_emp;
增加一个字段: alter table t1 add c1 varchar2(10);
13.多表查询:select * from t1,t2;(笛卡尔集c1行*c2行)
14.嵌套查询select c1 from t1 where c2 in(select c2 from t2 where c3=(select c3 from t3));
15. 将小写字母变为大写字母 : select upper('hello') from dual;
将大写字母变为小写字母 : select lower('HELLO WORLD') from dual;
将第一个字母大写 : select initcap('hello world') from dual;
16. 将一个字符串转换成日期类型 : select to_date('2009-01-01', 'yyyy-mm-dd') from dual;
17.联合查询 : select e.empno, e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno = d.deptno;
18.查看当前有哪些用户连接到数据库 : select * from v$session where username='FMIS9999'(select serial#, sid from v$session; 杀掉:alter system kill session 'serial#, sid ';)
19. select count(*) from v$process --当前的连接数
select value from v$parameter where name = 'processes' --数据库允许的最大连接数
 
修改最大连接数:
alter system set processes = 300 scope = spfile;
 
重启数据库:
shutdown immediate;
startup;
 
--查看当前有哪些用户正在使用数据
SELECT osuser, a.username,cpu_time/executions/1000000||'s', sql_fulltext,machine
from v$session a, v$sqlarea b
where a.sql_address =b.address order by cpu_time/executions desc;
 
20.修改表空间名称
1、 使用oracle用户登录执行
    $sqlplus / as sysdba
2、 执行修改表空间命令如下
    SQL> alter tablespace  TEST rename to TEST1;
 
21、备份表数据
create table xtyhxx_bak1013 as select * from xtyhxx;
 
22.修改oracle中内存占用大小
show parameter sga; --显示内存分配情况
alter system set sga_max_size=200m scope=spfile; --修改占用内存的大小
 
23.oracle怎么把一列数据插入到另一列
update [表名] set [另外一列]=[前一列]
24.修改字段名称
alter table [表名] rename column [oldCName] to [newCName];
添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….);
修改字段的语法:alter table tablename modify (column datatype [default value][null/not null],….);
删除字段的语法:alter table tablename drop (column);
25、导入导出指定表数据
1、从源数据库导出:
exp user1/pwd@server1 file=c:\temp\exp.dmp tables=(table1, table2)
2、导入到目标数据库:
imp user2/pwd@server2 file=c:\temp\exp.dmp tables=(table1, table2)
3、imp,加上fromuser,touser,ignore=Y选项
imp \'FMIS9999/1234.abcd@10.121.15.3/invoiceone\' file=/home/oracle/dump/PDF_QUEUE_20180505.DMP ignore=Y FULL=Y
 
26、查询表空间是否自动扩展
select file_name,autoextensible,increment_by from dba_data_files;
 
27.VICE_NAMES: 
SELECT * FROM global_name; -- 查看oracle的全局数据库名
SELECT * FROM v$database; -- 查看数据库名 show parameter db_name;
 

28.数据库实例名对应着SID
-- SID: http://docs.oracle.com/database/121/LADBI/glossary.htm#LADBI8021
-- linux下在配置oracle环境变量的情况可以使用 echo $ORACLE_SID,如果没有可以使用ps -ef |grep oracle 来查询,结果中的xxxx就是对应的SID。
-- oracle 2548 1 0 Aug17 ? 00:00:00 ora_pmon_xxxx
-- 在windows环境下,oracle是以后台服务的方式被管理的,所以看"控制面板->管理工具->服务 里面的名称:"OracleServiceORCL",则ORCL就是sid;
SELECT * FROM v$instance; --查看数据库实例名 show parameter instance_name;
select instance from v$thread;

-- show parameter是oracle的命令,不是标准SQL语句
-- 可以在sqlplus或者pl/sql dev的命令窗口执行
-- show parameter aaaa;等价于SELECT * FROM v$parameter WHERE name like '%aaaa%';
SELECT * FROM v$parameter WHERE name like '%name%'; --等价于show parameter name;
select * from v$parameter where name like '%db_domain%'; --查询数据库域名

29. drop user

select username from all_users where username like '%SCOTT%';
drop user SCOTT cascade;
commit;

-- ERROR at line 1:
-- ORA-01940: cannot drop a user that is currently connected

30.KILL SESSION

select 'ALTER SYSTEM KILL SESSION '||''''||SID||','||SERIAL#||''''||';' as KILLER from v$session where username='SCOTT';
-- KILLER
-- ALTER SYSTEM KILL SESSION '363,35';
-- ALTER SYSTEM KILL SESSION '364,51';
commit;

31.drop role

select * from dba_roles where role like '%CONNECT%';
drop role CONNECT;
commit;

32.drop tablespace

select * from dba_tablespaces where tablespace_name like 'EXAMPLE';

drop tablespace EXAMPLE including contents and datafiles cascade constraints ;
-- including contents 删除表空间中的内容,如果删除表空间之前表空间中有内容,而未加此参数,表空间删不掉,所以习惯性的加此参数。
-- including datafiles 删除表空间中的数据文件。
-- cascade constraints 同时删除 tablespace 中表的外键参照。

33 如何创建dblink和视图

-- http://docs.oracle.com/database/121/SQLRF/statements_5006.htm#i2061505
 如果需要创建全局 DBLink,则需要先确定用户有创建 dblink 的权限:
select * from user_sys_privs where privilege like upper('%DATABASE LINK%');

-- 如果没有,则需要使用 sysdba 角色给用户赋权:
grant create public database link to dbusername;

-- 如果创建全局 dblink,必须使用 systm 或 sys 用户,在 database 前加 public。
create /* public */ database link dblink1
connect to dbusername identified by dbpassword
using '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)))';

-- 创建dblink后,就可以直接在dblink上创建视图
create or replace view cptp as (select SJDH from dbusername.cptp@dblink1); drop view cptp;

34.锁表查询SQL
SELECT object_name, machine, s.sid, s.serial#
FROM gv$locked_object l, dba_objects o, gv$session s
WHERE l.object_id = o.object_id
AND l.session_id = s.sid;

35. 解除锁表
alter system kill session 'sid, serial#';

36. 备份某个表
create table new_table as select * from old_table;

37 查看数据库是否在rac环境的集群中的
show parameter cluster_database;
select * from v$parameter where name = 'cluster_database';

38.列操作
-- 增加和修改列不需要加关键字COLUMN
-- 删除单列的话,一定要加COLUMN,删除多列的时候,不能加COLUMN关键字

-- 增加一列
alter table emp4 add test varchar2(10);
-- 修改一列
alter table emp4 modify test varchar2(20);
-- 删除一列
alter table emp4 drop column test;
-- 增加多列
alter table emp4 add (test varchar2(10),test2 number);
-- 修改多列
alter table emp4 modify (test varchar2(20),test2 varchar2(20));
-- 删除多列
alter table emp4 drop (test,test2);

39.修改ORACLE数据库密码有效期的方法

39.1.查看用户的PROIFLE是哪个,一般是DEFAULT

  SELECT username,PROFILE FROM dba_users;

39.2.查看指定概要文件(如default)的密码有效期设置:  

SELECT * FROM dba_profiles s WHERE s.profile='DEFAULT' AND resource_name='PASSWORD_LIFE_TIME';

39.3.将密码有效期由默认的180天修改成“无限制”:

alter profile default limit password_life_time unlimited;

上一篇:.Net 中读写Oracle数据库常用两种方式


下一篇:【Leetcode】Jewels and Stones