OCP SQL 예 - 2
10238 단어 sql
--external table
-- , Oracle , sqlldr
-- , external table oracle 。
-- Oracle , Oracle
CREATE DIRECTORY ORCL_LOG as 'F:\oracle\product\10.2.0\db_1\admin\orcl\bdump';
--
CREATE TABLE ALERT_LOG(
LOG_TEXT VARCHAR2(4000)
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ORCL_LOG
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE--
NOBADFILE
NODISCARDFILE
NOLOGFILE
)
LOCATION('ALERT_ORCL.LOG')
)
REJECT LIMIT UNLIMITED;
--
SELECT * FROM ALERT_LOG WHERE ROWNUM <= 10;
LOG_TEXT
-----------------------------------------------------------------------
Dump file f:\oracle\product\10.2.0\db_1/admin/orcl/bdump\alert_orcl.log
Sun Oct 21 09:39:32 2012
ORACLE V10.2.0.1.0 - Production vsnsta=0
vsnsql=14 vsnxtr=3
Windows NT Version V6.1 Service Pack 1
CPU : 4 - type 586
Process Affinity : 0x00000000
Memory (Avail/Total): Ph:285M/1991M
Sun Oct 21 09:39:32 2012
Starting ORACLE instance (normal)
------------------------------------------------------------------------------------------------------------------------------------
--MERGE
-- Merge
/*
MERGE INTO TABLE
USING TABLE|SUBQUERY
ON CONDITION
WHEN MATCHED THEN UPDATE SET COL = EXPRESSION | DEFAULT
WHERE_CLAUSE
DELETE WHERE_CLAUSE
WHEN NOT MATCHED THEN INSERT(COL,COL2)
VALUES(EXPR1,EXPR2,|DEFAULT)
WHERE_CLAUSE
WHERE CONDITION;*/
DROP TABLE TAB1;
DROP TABLE TAB2;
CREATE TABLE TAB1(ID INT, NAME VARCHAR2(10),STATUS VARCHAR2(10));
CREATE TABLE TAB2(ID INT, NAME VARCHAR2(10));
INSERT INTO TAB1 VALUES(1,'ANKER','N');
INSERT INTO TAB1 VALUES(2,'MIN','U');
INSERT INTO TAB1 VALUES(3,'MING','D');
INSERT INTO TAB1 VALUES(4,'XIAO','U');
INSERT INTO TAB2 VALUES(2,'A');
INSERT INTO TAB2 VALUES(3,'MING');
INSERT INTO TAB2 VALUES(4,'B');
COMMIT;
SELECT * FROM TAB1;
ID NAME STATUS
--------- ---------- ----------
1 ANKER N
2 MIN U
3 MING D
4 XIAO U
SELECT * FROM TAB2;
ID NAME
---------- ----------
2 A
3 MING
4 B
MERGE INTO TAB2
USING TAB1 ON (TAB2.ID = TAB1.ID)
WHEN MATCHED THEN UPDATE SET NAME = TAB1.NAME
DELETE WHERE (TAB1.STATUS = 'D')
WHEN NOT MATCHED THEN INSERT(ID,NAME) VALUES(TAB1.ID,TAB1.NAME)
WHERE 1=1;
SELECT * FROM TAB2;--
ID NAME
---------- ----------
2 A
3 MING
4 B
------------------------------------------------------------------------------------------------------------------------------------------
--connect by
DROP TABLE TT;
CREATE TABLE TT
(
ID INT,
PARENT_ID INT,
NAME VARCHAR2(10)
);
INSERT INTO TT VALUES(1,NULL,'CEO');
INSERT INTO TT VALUES(2,1,'VP');
INSERT INTO TT VALUES(3,1,'SVP');
INSERT INTO TT VALUES(4,1,'CFO');
INSERT INTO TT VALUES(5,2,'DIRECTOR 1');
INSERT INTO TT VALUES(6,2,'DIRECTOR 2');
INSERT INTO TT VALUES(7,3,'DIRECTOR 3');
INSERT INTO TT VALUES(8,3,'DIRECTOR 4');
INSERT INTO TT VALUES(9,6,'MANAGER 1');
COMMIT;
SELECT * FROM TT;
ID PARENT_ID NAME
---------- ---------- ----------
1 CEO
2 1 VP
3 1 SVP
4 1 CFO
5 2 DIRECTOR 1
6 2 DIRECTOR 2
7 3 DIRECTOR 3
8 3 DIRECTOR 4
9 6 MANAGER 1
SELECT * FROM TT START WITH ID = 2 CONNECT BY PRIOR ID = PARENT_ID;
ID PARENT_ID NAME
---------- ---------- ----------
2 1 VP
5 2 DIRECTOR 1
6 2 DIRECTOR 2
9 6 MANAGER 1
--START WITH
-- CONNECT BY
--PIROR ID = PARENT_ID, , ( ) ID PARENT ID。
SELECT * FROM TT START WITH ID = 2 CONNECT BY ID = PRIOR PARENT_ID;
-- ( ) PARENT_ID ID
ID PARENT_ID NAME
---------- ---------- ----------
2 1 VP
1 CEO
--
SELECT
LEVEL, --
LPAD(' ', 2 * LEVEL) || NAME,
SYS_CONNECT_BY_PATH(NAME, '/'),--
CONNECT_BY_ISLEAF,-- ,
CONNECT_BY_ROOT(NAME)-- ROOT
FROM TT
START WITH ID = 2
CONNECT BY PRIOR ID = PARENT_ID
ORDER SIBLINGS BY ID DESC;-- ,
------------------------------------------------------------------------------------------------------------------------------------
--NATRUAL JOIN
CREATE TABLE TT (ID INT,CODE VARCHAR2(10));
CREATE TABLE TT1 (CODE VARCHAR2(10),NAME VARCHAR2(10));
INSERT INTO TT VALUES(1,'A');
INSERT INTO TT VALUES(2,'B');
INSERT INTO TT VALUES(3,'C');
INSERT INTO TT1 VALUES('A','JSP');
INSERT INTO TT1 VALUES('B','JAVA');
SELECT * FROM TT;
ID CODE
--------- ----------
1 A
2 B
3 C
SELECT * FROM TT1;
CODE NAME
---------- ----------
A JSP
B JAVA
D ORACLE
SELECT TT.ID, CODE, TT1.NAME FROM TT NATURAL JOIN TT1;
ID CODE NAME
---------- ---------- ----------
1 A JSP
2 B JAVA
--NATRUAL JOIN 。 ON
-- , , :
admin@ORCL> SELECT TT.ID, TT.CODE, TT1.NAME FROM TT NATURAL JOIN TT1;
SELECT TT.ID, TT.CODE, TT1.NAME FROM TT NATURAL JOIN TT1
*
1 :
ORA-25155: NATURAL
--------------------------------------------------------------------------------------------------------------------------------------
--USING
--USING INNER JOIN OUTER JOIN ( FULL OUTER JOIN)。
-- ,
SELECT TT.ID, CODE, TT1.NAME FROM TT INNER JOIN TT1 USING (CODE);
ID CODE NAME
---------- ---------- ----------
1 A JSP
2 B JAVA
SELECT TT.ID, CODE, TT1.NAME FROM TT LEFT JOIN TT1 USING (CODE);
ID CODE NAME
---------- ---------- ----------
1 A JSP
2 B JAVA
3 C
-----------------------------------------------------------------------------------------------------------------------------------
--ANY SOME ALL
SELECT CODE FROM TT;
ID CODE
---------- ----------
1 A
2 B
3 C
SELECT CODE FROM TT1;
CODE NAME
---------- ----------
A JSP
B JAVA
D ORACLE
--ANY
SELECT * FROM TT WHERE CODE > ANY(SELECT CODE FROM TT1);
ID CODE
--------- ----------
3 C
2 B
-- ,ANY 。 TT TT1 CODE 。
-- SQL
SELECT * FROM TT WHERE CODE > (SELECT MIN(CODE) FROM TT1);
-- SOME, ANY 。
SELECT * FROM TT WHERE CODE > SOME(SELECT CODE FROM TT1);
ID CODE
---------- ----------
3 C
2 B
-- ALL
SELECT * FROM TT1 WHERE CODE > ALL (SELECT CODE FROM TT);
CODE NAME
---------- ----------
D ORACLE
-- TT1 , TT CODE 。 SQL
SELECT * FROM TT1 WHERE CODE > (SELECT MAX(CODE) FROM TT);
------------------------------------------------------------------------------------------------------------------------------------
--WITH AS
--WITH AS SQL , , SQL 。
-- SQL
WITH TAB_TT AS
(SELECT * FROM TT)
SELECT * FROM TAB_TT;
ID CODE
---------- ----------
1 A
2 B
3 C
--listagg
WITH TEMP AS(
SELECT 500 POPULATION, 'CHINA' NATION ,'GUANGZHOU' CITY FROM DUAL UNION ALL
SELECT 1500 POPULATION, 'CHINA' NATION ,'SHANGHAI' CITY FROM DUAL UNION ALL
SELECT 500 POPULATION, 'CHINA' NATION ,'BEIJING' CITY FROM DUAL UNION ALL
SELECT 1000 POPULATION, 'USA' NATION ,'NEW YORK' CITY FROM DUAL UNION ALL
SELECT 500 POPULATION, 'USA' NATION ,'BOSTOM' CITY FROM DUAL UNION ALL
SELECT 500 POPULATION, 'JAPAN' NATION ,'TOKYO' CITY FROM DUAL
)
SELECT POPULATION,
NATION,
CITY,
LISTAGG(CITY,',') WITHIN GROUP (ORDER BY CITY DESC) OVER (PARTITION BY NATION) RANK
FROM TEMP;
POPULATION NATIO CITY RANK
---------- ----- --------- ------------------------------
1500 CHINA SHANGHAI SHANGHAI,GUANGZHOU,BEIJING
500 CHINA GUANGZHOU SHANGHAI,GUANGZHOU,BEIJING
500 CHINA BEIJING SHANGHAI,GUANGZHOU,BEIJING
500 JAPAN TOKYO TOKYO
1000 USA NEW YORK NEW YORK,BOSTOM
500 USA BOSTOM NEW YORK,BOST
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
깊이 중첩된 객체를 정확히 일치 검색 - PostgreSQL목차 * 🚀 * 🎯 * 🏁 * 🙏 JSON 객체 예시 따라서 우리의 현재 목표는 "고용주"사용자가 입력한 검색어(이 경우에는 '요리')를 얻고 이 용어와 정확히 일치하는 모든 사용자 프로필을 찾는 것입니다. 즐거운 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.