Oracle鏁版嵁搴撳父鐢ㄧ殑Sql璇彞鏁寸悊

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle鏁版嵁搴撳父鐢ㄧ殑Sql璇彞鏁寸悊相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%88%97%e6%93%8d%e4%bd%9c' title='鍒楁搷浣?>鍒楁搷浣?/a>   tables   count   suse   EDA   lists   宓屽   type   缁撴灉   

Oracle鏁版嵁搴撳父鐢ㄧ殑Sql璇彞鏁寸悊

鏌ョ湅褰撳墠鐢ㄦ埛鐨勭己鐪佽〃绌洪棿 锛?select username,default_tablespace from user_users;
2銆佹煡鐪嬬敤鎴蜂笅鎵€鏈夌殑琛?锛?select * from user_tables;
3銆佸垱寤鸿〃绌洪棿 锛欳REATE TABLESPACE invocie DATAFILE 鈥?data/InvoiceData/invoicebak/invocie_01.dbf鈥?Size 4096M AUTOEXTEND OFF锛?/div>

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锛?/p>

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  鈥淪6MISM鈥?br> DEFAULT TABLESPACE "MIS_DATA"
TEMPORARY TABLESPACE "TEMP"

 

CREATE USER "YYII_S6MISM"  identified by  鈥淵YII_S6MISM鈥?/p>

DEFAULT TABLESPACE "MIS_DATA"
TEMPORARY TABLESPACE "TEMP"


CREATE USER "TEAMB0"  identified by  鈥淭EAMB0鈥?br> DEFAULT TABLESPACE "USER_DATA"
TEMPORARY TABLESPACE "TEMP"

 

5銆佽В閿佺敤鎴凤細alter user scott account unlock;
SELECT * FROM dba_users WHERE username = 鈥楽COTT鈥?
ALTER USER SCOTT account LOCK; --閿佸畾鐢ㄦ埛
ALTER USER SCOTT account UNLOCK; --瑙i攣鐢ㄦ埛
COMMIT;
 
6锛庨噸璁剧敤鎴峰瘑鐮侊細scott/tiger涓洪粯璁ょ敤鎴凤紝alter user scott identified by tiger;
SELECT password FROM dba_users WHERE username = 鈥楽COTT鈥?
alter user SCOTT identified by new_password; --淇敼鐢ㄦ埛瀵嗙爜
7銆佸垱寤鸿〃锛歝reate table t1(c1 type 绾︽潫,c2 type 绾︽潫锛坣ot 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 (鈥榊鈥? 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 鈥楴鈥?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 (鈥榊鈥?鈥楴鈥?)) 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 鈥?鈥?
"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. 鏌ヨ锛歴elect distinct c1 from t1 where 鏉′欢 group by c1 having by 瀛愭潯浠秓rder by c1; 
 SELECT MAIN_MAT_NO,sub_mat_no,level 

FROM MATBOM
--where level >1
START WITH sub_MAT_NO = 鈥楢020408G01A059 鈥?br>CONNECT BY PRIOR SUB_MAT_NO = MAIN_MAT_NO
;;

9. 杩炴帴瀛楃涓诧細select c1 ||c2 from t1;
10.鏌ョ湅褰撳墠绯荤粺鏃堕棿 锛歴elect sysdate from dual;
11.鏇存柊鏁版嵁鎿嶄綔锛氭彃鍏ヨ褰曪細insert into t1(c1,c2)values(鈥樷€?鈥欌€?锛?nbsp;
鎻掑叆涓€瀛楁:insert into t1(c1,c2) select c3,c4 from t2; 
鏇存柊璁板綍锛歶pdate t1  set c1=鈥欌€?nbsp;where  c1 = 鈥樷€?/div>
鍒犻櫎璁板綍锛歞elete 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(鈥榟ello鈥? from dual;
灏嗗ぇ鍐欏瓧姣嶅彉涓哄皬鍐欏瓧姣?: select lower(鈥楬ELLO WORLD鈥? from dual;
灏嗙涓€涓瓧姣嶅ぇ鍐?: select initcap(鈥榟ello world鈥? from dual;
16. 灏嗕竴涓瓧绗︿覆杞崲鎴愭棩鏈熺被鍨?: select to_date(鈥?009-01-01鈥? 鈥榶yyy-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=鈥楩MIS9999鈥?select serial#, sid from v$session; 鏉€鎺夛細alter system kill session 鈥榮erial#, sid 鈥?)
19. select count(*) from v$process --褰撳墠鐨勮繛鎺ユ暟
select value from v$parameter where name = 鈥榩rocesses鈥?--鏁版嵁搴撳厑璁哥殑鏈€澶ц繛鎺ユ暟
 
淇敼鏈€澶ц繛鎺ユ暟:
alter system set processes = 300 scope = spfile;
 
閲嶅惎鏁版嵁搴?
shutdown immediate;
startup;
 
--鏌ョ湅褰撳墠鏈夊摢浜涚敤鎴锋鍦ㄤ娇鐢ㄦ暟鎹?/div>
SELECT osuser, a.username,cpu_time/executions/1000000||鈥榮鈥? sql_fulltext,machine
from v$session a, v$sqlarea b
where a.sql_address =b.address order by cpu_time/executions desc;
 
20.淇敼琛ㄧ┖闂村悕绉?/div>
1銆?nbsp;浣跨敤oracle鐢ㄦ埛鐧诲綍鎵ц
    $sqlplus / as sysdba
2銆?nbsp;鎵ц淇敼琛ㄧ┖闂村懡浠ゅ涓?/div>
    SQL> alter tablespace  TEST rename to TEST1;
 
21銆佸浠借〃鏁版嵁
create table xtyhxx_bak1013 as select * from xtyhxx;
 
22.淇敼oracle涓唴瀛樺崰鐢ㄥぇ灏?/div>
show parameter sga; --鏄剧ず鍐呭瓨鍒嗛厤鎯呭喌
alter system set sga_max_size=200m scope=spfile; --淇敼鍗犵敤鍐呭瓨鐨勫ぇ灏?/div>
 
23.oracle鎬庝箞鎶婁竴鍒楁暟鎹彃鍏ュ埌鍙︿竴鍒?/div>
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銆佷粠婧愭暟鎹簱瀵煎嚭锛?/div>
exp user1/[email protected] file=c: empexp.dmp tables=(table1, table2)
2銆佸鍏ュ埌鐩爣鏁版嵁搴擄細
imp user2/[email protected] file=c: empexp.dmp tables=(table1, table2)
3銆乮mp,鍔犱笂fromuser,touser,ignore=Y閫夐」
imp 鈥楩MIS9999/[email protected]/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灏辨槸瀵瑰簲鐨凷ID銆?br>-- oracle 2548 1 0 Aug17 ? 00:00:00 ora_pmon_xxxx
-- 鍦╳indows鐜涓?oracle鏄互鍚庡彴鏈嶅姟鐨勬柟寮忚绠$悊鐨?鎵€浠ョ湅"鎺у埗闈㈡澘->绠$悊宸ュ叿->鏈嶅姟 閲岄潰鐨勫悕绉?"OracleServiceORCL",鍒橭RCL灏辨槸sid;
SELECT * FROM v$instance; --鏌ョ湅鏁版嵁搴撳疄渚嬪悕 show parameter instance_name;
select instance from v$thread;

-- show parameter鏄痮racle鐨勫懡浠わ紝涓嶆槸鏍囧噯SQL璇彞
-- 鍙互鍦╯qlplus鎴栬€卲l/sql dev鐨勫懡浠ょ獥鍙f墽琛?br>-- show parameter aaaa;绛変环浜嶴ELECT * FROM v$parameter WHERE name like 鈥?aaaa%鈥?
SELECT * FROM v$parameter WHERE name like 鈥?name%鈥? --绛変环浜巗how parameter name;
select * from v$parameter where name like 鈥?db_domain%鈥? --鏌ヨ鏁版嵁搴撳煙鍚?/p>

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 鈥楢LTER SYSTEM KILL SESSION 鈥榺|鈥樷€樷€樷€榺|SID||鈥?鈥榺|SERIAL#||鈥樷€樷€樷€榺|鈥?鈥?as KILLER from v$session where username=鈥楽COTT鈥?
-- KILLER
-- ALTER SYSTEM KILL SESSION 鈥?63,35鈥?
-- ALTER SYSTEM KILL SESSION 鈥?64,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 鈥楨XAMPLE鈥?

drop tablespace EXAMPLE including contents and datafiles cascade constraints ;
-- including contents 鍒犻櫎琛ㄧ┖闂翠腑鐨勫唴瀹癸紝濡傛灉鍒犻櫎琛ㄧ┖闂翠箣鍓嶈〃绌洪棿涓湁鍐呭锛岃€屾湭鍔犳鍙傛暟锛岃〃绌洪棿鍒犱笉鎺夛紝鎵€浠ヤ範鎯€х殑鍔犳鍙傛暟銆?br>-- including datafiles 鍒犻櫎琛ㄧ┖闂翠腑鐨勬暟鎹枃浠躲€?br>-- cascade constraints 鍚屾椂鍒犻櫎 tablespace 涓〃鐨勫閿弬鐓с€?/em>

33 濡備綍鍒涘缓dblink鍜岃鍥?/em>

-- 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銆?br>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涓婂垱寤鸿鍥?br>create or replace view cptp as (select SJDH from [email protected]); 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. 瑙i櫎閿佽〃
alter system kill session 鈥榮id, serial#鈥?

36. 澶囦唤鏌愪釜琛?br>create table new_table as select * from old_table;

37 鏌ョ湅鏁版嵁搴撴槸鍚﹀湪rac鐜鐨勯泦缇や腑鐨?br>show parameter cluster_database;
select * from v$parameter where name = 鈥榗luster_database鈥?

38.鍒楁搷浣?br>-- 澧炲姞鍜屼慨鏀瑰垪涓嶉渶瑕佸姞鍏抽敭瀛桟OLUMN
-- 鍒犻櫎鍗曞垪鐨勮瘽锛屼竴瀹氳鍔燙OLUMN锛屽垹闄ゅ鍒楃殑鏃跺€欙紝涓嶈兘鍔燙OLUMN鍏抽敭瀛?/p>

-- 澧炲姞涓€鍒?br>alter table emp4 add test varchar2(10);
-- 淇敼涓€鍒?br>alter table emp4 modify test varchar2(20);
-- 鍒犻櫎涓€鍒?br>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);

 

以上是关于Oracle鏁版嵁搴撳父鐢ㄧ殑Sql璇彞鏁寸悊的主要内容,如果未能解决你的问题,请参考以下文章

R璇█涓嚑涓父鐢ㄧ殑鏁板鍔熻兘

鏁版嵁搴撳垎鐗?Sharding)

鏁版嵁搴撳涔犲綊绾虫€荤粨

杈炬ⅵ鏁版嵁搴撳洖椤惧強鍩烘湰浠嬬粛

Qt 鏁版嵁搴撳熀鏈娇鐢?-QSQLITE

Java鏂版墜甯歌閿欒鏁寸悊

(c)2006-2024 SYSTEM All Rights Reserved IT常识