Oracle数据库常用的Sql语句整理
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
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"
ALTER USER SCOTT account LOCK; --锁定用户
ALTER USER SCOTT account UNLOCK; --解锁用户
COMMIT;
alter user SCOTT identified by new_password; --修改用户密码
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"
FROM MATBOM
--where level >1
START WITH sub_MAT_NO = 'A020408G01A059 '
CONNECT BY PRIOR SUB_MAT_NO = MAIN_MAT_NO
;;
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;