[Day13] Oracle13 [10/14]
릴λ μ΄μ
= ν
μ΄λΈ νν = λ μ½λ(ν) μμ± = μ»¬λΌ |
μ κ·ν
- μ΄μνμ(μ½μ
,κ°±μ ,μμ ), ν¨μ μ’
μμ± ( X->Y ), 1NF~ 5NF
- ERD->κ΄κ³μ€ν€λ§ μμ± ν μ’
μμ± μ΄λ‘ μ μ΄μ©ν΄μ μλͺ»λ μ€κ³λ κ΄κ³ μ€ν€λ§λ₯Ό λ μμ μμ±μ λ°λμ§ν μ€ν€λ§λ‘ λ§λ€μ΄ κ°λ κ³Όμ μ μ κ·ν λΌκ³ νλ€.
- DB μ€κ³ λ¬Έμ μ ( λ°μ΄ν° μ€λ³΅ )μ΄ μμ κ²½μ° μ½μ
,κ°±μ ,μμ ν λ λ¬Έμ κ° λ°μνλ κ²μ "μ΄μνμ"μ΄λΌκ³ νλ€. μ΄λ° μ΄μνμμ μ κ±°νλ κ²μ μ κ·νλΌκ³ νλ€.
λ°μ΄ν° μ€λ³΅ μ κ±° κ³Όμ (μμ
) == μ κ·ν
ν¨μμ μ’
μμ±(FD;Functional Dependency)
- μ κ·ν ν μ΄λΈμ μμ±λ€κ°μ κ΄λ ¨μ±
DEPT(λΆμν
μ΄λΈ) PK κ²°μ μ μ’ μμ X Y [deptno] dname loc 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON |
* Y μμ±μ X μμ±μ ν¨μμ μ’
μμ μ΄λ€.
* X -> Y
1) μμ ν¨μμ μ’
μ
- μ΄λ€ 릴λ μ΄μ
Rμμ μμ±Aκ° μμ± μ§ν©μΌ λ, μμ± Bκ° μμ±Aμ ν¨μμ μ’
μνλ©΄μ, μμ± Aμ μ΄λ€ μ§λΆλΆ μ§ν©μλ ν¨μμ μΌλ‘ μ’
μνμ§ μμΌλ©°, μμ± Bκ° μμ± Aμ μμ νκ² ν¨μμ μΌλ‘ μ’
μνλ€.
- μ¬λ¬ κ°μ μμ±μ΄ λͺ¨μ¬μ νλμ κΈ°λ³Έν€λ₯Ό μ΄λ£° κ²½μ° ( 볡ν©ν€ ) 볡ν©ν€ μ 체μ μ΄λ€ μμ±μ΄ μ’
μμ μΌ λ "μμ ν¨μμ μ’
μ"μ΄λΌ νλ€.
볡ν©ν€ [κ³ κ°ID] [μ΄λ²€νΈID] A B C kenik E001 [ X + Y ] -> A [ X + Y ] -> B [ X + Y ] -> C |
2) λΆλΆ ν¨μμ μ’
μ
- μμ ν¨μμ μ’
μμ΄ μλλ©΄ λΆλΆ ν¨μμ μ’
μμ΄λ€.
볡ν©ν€ [κ³ κ°ID] [μ΄λ²€νΈID] ν©κ²©μ¬λΆ λ±κΈ kenik E001 Y gold 볡ν©ν€ -> ν©κ²©μ¬λΆ O 볡ν©ν€ -> λ±κΈ X κ³ κ°ID -> λ±κΈ O |
3) μ΄ν ν¨μμ μ’
μ
X->Y, Y->Z
κ²°μ μ
dept ν
μ΄λΈ κ²°μ μ PK X -> Y Y -> Z [deptno] dname λΆμμ₯ λΆμμ₯μ±λ³ λΆμλ²νΈ λΆμλͺ νκΈΈλ λ¨μ |
-- μ κ·ν --
μλΆμ΄κ²°λ€μ‘°
1 : μμκ°μ΄ μλ λλ©μΈ λΆν΄ 2 : λΆλΆν¨μ μ’ μμ± μ κ±° 3 : μ΄νν¨μ μ’ μμ± μ κ±° 4 : κ²°μ μμ΄λ©΄μ νλ³΄ν€ μλ κ² μ κ³ 5 : λ€μΉμ’ μ μ κ±° 6 : μ‘°μΈ μ’ μμ± μ΄μ© |
1) μ 1μ κ·ν( NOrmal Form ) 1NF
- 릴λ μ΄μ
μ μν λͺ¨λ μμ±μ λλ©μΈμ΄ μμ κ°(atomic value)μΌλ‘λ§ κ΅¬μ±λμ΄ μμΌλ©΄ μ 1μ κ·νμ μνλ€.
deptno dname loc 10 μ΄λ¬΄λΆ l_id(FK) 1 20 μμ λΆ 2 30 μμ°λΆ 1 40 μμ¬λΆ 2 : [λΆμμ§μ ν μ΄λΈ] PK l_id 1 μμΈ ... ... 2 λΆμ° 3 λꡬ |
2) μ 2μ κ·ν (2NF)
- 릴λ μ΄μ
μ΄ μ 1μ κ·νμ μνκ³ , κΈ°λ³Έν€κ° μλ λͺ¨λ μμ±μ΄ κΈ°λ³Έν€μ μμ ν¨μ μ’
μλλ©΄ μ 2μ κ·νμ μνλ€.
- λΆλΆ ν¨μμ μ’
μ -> μ κ±°
볡ν©ν€ [κ³ κ°ID + μ΄λ²€νΈID ] λΉμ²¨μ¬λΆ κ³ κ°λͺ κ³ κ°λ±κΈ |
->
μ΄λ²€νΈ ν
μ΄λΈ E001 λ ΈνΈλΆ 30% μμμΌ μ’ λ£μΌ ... E002 λ ΈνΈλΆ 20% E003 ν° 50% E001 λ ΈνΈλΆ 30% |
μ΄λ²€νΈ <μ μ²> μ μ²λ²νΈ(PK) κ³ κ°ID μ΄λ²€νΈID μ μ²μΌ λΉμ²¨μ¬λΆ 1 κ³ κ°1 E001 22.1.1 Y |
κ³ κ°ν
μ΄λΈ κ³ κ°ID κ³ κ°λͺ κ³ κ°λ±κΈ κ³ κ°1 |
3)μ 3μ κ·ν( 3NF )
- 릴λ μ΄μ
μ΄ μ 2μ κ·νμ μνκ³ , κΈ°λ³Έν€κ° μλ λͺ¨λ μμ±μ΄ κΈ°λ³Έν€μ [μ΄νμ ν¨μ μ’
μ]μ΄ λμ§ μμΌλ©΄ μ 3μ κ·νμ μνλ€.
- μ΄νμ ν¨μ μ’
μ -> μ κ±°
PK X -> Y X -> Z Y -> T deptno λΆμμ₯ λΆμλͺ λΆμμ₯μ°λ½μ² X->Y, Y->Z |
->
λΆμν
μ΄λΈ λΆμλ²νΈ λΆμμ₯μ¬μμ½λ(FK) λΆμλͺ |
μ¬μν
μ΄λΈ μ¬μμ½λ(PK) μ¬μλͺ μ§κΈλͺ μ°λ½μ² |
4) 보μ΄μ€/μ½λ μ κ·ν( BCNF )
릴λ μ΄μ
μ ν¨μ μ’
μ κ΄κ³μμ λͺ¨λ κ²°μ μ(볡ν©ν€)κ° ν보ν€μ΄λ©΄ 보μ΄μ€/μ½λ μ κ·νμ μνλ€.
볡ν©ν€
[κ³ κ°μμ΄λ μΈν°λ·κ°μ’] λ΄λΉκ°μ¬λ²νΈ [ X Y ] -> Z μμ ν¨μ μ’ μ Y <- Z |
ν
μ΄λΈ [ X Z ]
ν
μ΄λΈ [Z] Y
* μ 3μ κ·νλ₯Ό λ§μ‘±νλ©΄ BCNF μ κ·ν κ±°μ μλ€. X
* 물리μ dB λͺ¨λΈλ§
- μμ κ·ν - μ±λ₯
* EXERD DB λͺ¨λΈλ§ -> μ€λΌν΄ DB μλ² ( μ€ν€λ§ μμ± )
* μμ
, λ©λͺ¨μ₯ -> INSERT μλ
λ·°( View )
1. ν
μ΄λΈμ 보기 μν μ°½λ¬Έ μ λλ‘ μκ°.
2. κ°μν
μ΄λΈ = 1κ° μ΄μμ ν
μ΄λΈ λλ λ·°λ₯Ό μ΄μ©ν΄μ μμ±λλ€.
3. λ·°λ "λ°μ΄ν° μ¬μ "μ μ μ λΆλΆλ§ μ μ₯λκ³ , μ€μ λ°μ΄ν°λ μ μ₯ X
4. λ·°λ₯Ό μ¬μ©νλ λͺ©μ (μ΄μ )
1) 보μ
2) νΈλ¦¬μ±
5. λ·° - SELECT, INSERT, UPDATE, DELETE κ°λ₯
6. μ μ½ μ‘°κ±΄ μ€μ κ°λ₯
7. user_views : λ·° μ‘°ν.
user_tables : ν
μ΄λΈ μ 보 μ‘°ν
SELECT *
FROM user_views;
8. λ·° μ’
λ₯
1) λ¨μλ·° ( simple view ) - 1 κ°
2) 볡ν©λ·° ( complex view ) - 2κ° μ΄μ.~
9. λ·° μμ± νμ
γνμγ
CREATE [OR REPLACE] [FORCE | NOFORCE] VIEW λ·°μ΄λ¦
[(alias[,alias]...]
AS subquery
[WITH CHECK OPTION]
[WITH READ ONLY];
OR REPLACE : κ°μ μ΄λ¦μ λ·°κ° μμ κ²½μ° λ¬΄μνκ³ λ€μ μμ± FORCE : κΈ°λ³Έ ν μ΄λΈμ μ 무μ μκ΄μμ΄ λ·°λ₯Ό μμ± NOFORCE : κΈ°λ³Έ ν μ΄λΈμ΄ μμ λλ§ λ·°λ₯Ό μμ± ALIAS : κΈ°λ³Έ ν μ΄λΈμ 컬λΌμ΄λ¦κ³Ό λ€λ₯΄κ² μ§μ ν λ·°μ 컬λΌλͺ λΆμ¬ WITH CHECK OPTION : λ·°μ μν΄ accessλ μ μλ ν(row)λ§μ΄ μ½μ , μμ κ°λ₯ WITH READ ONLY : DML μμ μ μ ν(λ¨μ§ μ½λ κ²λ§ κ°λ₯) |
μ) ν맀λ νμΈ - 쿼리
SELECT b.b_id, title, price, g.g_id, g_name, p_date, p_su
FROM book b JOIN danga d ON b.b_id = d.b_id
JOIN panmai p ON b.b_id = p.b_id
JOIN gogaek g ON p.g_id = g.g_id;
-- λ·° μμ±
GRANT CREATE VIEW TO scott;
CREATE OR REPLACE FORCE VIEW panView
( bookid, booktitle, bookprice, gogaekid, gogaekname, pdate, psu )
AS (
SELECT b.b_id , title, price, g.g_id, g_name, p_date, p_su
FROM book b JOIN danga d ON b.b_id = d.b_id
JOIN panmai p ON b.b_id = p.b_id
JOIN gogaek g ON p.g_id = g.g_id
);
-- λ·°λ₯Ό μ¬μ©ν΄μ μ 체 ν맀 κΈμ‘
SELECT SUM( psu * BOOKPRICE ) μ 체ν맀κΈμ‘
FROM panview;
-- λ·° μμ± : gogaekview
-- λ
λ, μ, κ³ κ°μ½λ, κ³ κ°λͺ
, ν맀κΈμ‘μ΄ν© ( λ
λλ³ μλ³ ) μ‘°ν
-- panmai : p_date ( year, month ) , p_su
-- gogaek : g_id, gname
-- danga : price
-- ORA-00979: not a GROUP BY expression
-- ORA-00907: missing right parenthesis
CREATE OR REPLACE FORCE VIEW gogaekview
-- [alias,,,,]
AS
SELECT
-- p_date
TO_CHAR( p_date, 'YYYY') year
, TO_CHAR( p_date, 'MM') month
, g.g_id, g_name
, SUM( p_su * price ) amt
FROM panmai p JOIN gogaek g ON p.g_id = g.g_id
JOIN danga d ON d.b_id = p.b_id
GROUP BY TO_CHAR( p_date, 'YYYY')
, TO_CHAR( p_date, 'MM'), g.g_id, g_name
ORDER BY year
, month
-- λ·°λ₯Ό μ¬μ©ν΄μ DML μμ
--
CREATE TABLE tbl_view1(
aid number primary key
, name varchar2(20) not null
, tel varchar2(20) not null
, memo varchar2(100)
)
-- Table TBL_VIEW1μ΄(κ°) μμ±λμμ΅λλ€.
CREATE TABLE tbl_view2(
bid number primary key
, aid number CONSTRAINT FK_tblview2_aid
REFERENCES tbl_view1(aid) ON DELETE CASCADE -- FK
, score number(3)
)
INSERT INTO tbl_view1 (aid, NAME, tel) VALUES (1, 'a', '1');
INSERT INTO tbl_view1 (aid, name, tel) VALUES (2, 'b', '2');
INSERT INTO tbl_view1 (aid, name, tel) VALUES (3, 'c', '3');
INSERT INTO tbl_view1 (aid, name, tel) VALUES (4, 'd', '4');
INSERT INTO tbl_view2 (bid, aid, score) VALUES (1, 1, 80);
INSERT INTO tbl_view2 (bid, aid, score) VALUES (2, 2, 70);
INSERT INTO tbl_view2 (bid, aid, score) VALUES (3, 3, 90);
INSERT INTO tbl_view2 (bid, aid, score) VALUES (4, 4, 100);
COMMIT;
-- λ¨μλ·° : 1κ° ν
μ΄λΈ
CREATE OR REPLACE VIEW aView
AS
-- SELECT aid, name, memo -- , tel varchar2(20) not null X
SELECT aid, name,tel
FROM tbl_view1;
-- λ·°λ₯Ό μ¬μ©ν΄μ DML μμ
INSERT INTO AVIEW (aid, name, memo ) VALUES ( 5, 'f', null );
-- ORA-01400: cannot insert NULL into ("SCOTT"."TBL_VIEW1"."TEL")
-- μ? -> tbl_view1μ NOT NULL μ‘°κ±΄μ΄ μ μ©λκΈ° λλ¬Έμ
INSERT INTO AVIEW (aid, name, tel ) VALUES ( 5, 'f', '5' );
--λ·° : 1 ν μ΄(κ°) μ½μ
λμμ΅λλ€.
-- λ¨μλ·°
-- 볡ν©λ·°
CREATE OR REplace VIEW abview
AS
SELECT a.aid, name, tel, bid, score
FROM tbl_view1 a JOIN tbl_view2 b ON a.aid = b.aid
WITH READ ONLY; -- SELECT μ‘°νλ§ νκ² λ€, I/U/D X μ½κΈ° μ μ©
-- View ABVIEWμ΄(κ°) μμ±λμμ΅λλ€.
SELECT *
FROM abview;
-- λ·° μμ
DROP TABLE
DROP VIEW aview;
DROP VIEW abview;
-- View ABVIEWμ΄(κ°) μμ λμμ΅λλ€.
[ WITH CHECK OPTION ]
- λ·°μ μν΄ accessλ μ μλ ν(row)λ§μ΄ μ½μ
, μμ κ°λ₯
SELECT *
FROM tbl_view2;
bid aid score 1 1 80 2 2 70 3 3 90 4 4 100 |
CREATE OR REPLACE VIEW bview
AS
SELECT bid, aid, score
FROM tbl_view2
WHERE score >= 90;
UPDATE bview
SET score = 70
WHErE bid = 3;
VS
CREATE OR REPLACE VIEW bview
AS
SELECT bid, aid, score
FROM tbl_view2
WHERE score >= 90
WITH CHECK OPTION CONSTRAINT CK_BVIEW;
UPDATE bview
--SET score = 70 -- UPDATE X
SET score = 170 -- UPDATE O
WHERE bid = 3; -- 90
-- ORA-01402: view WITH CHECK OPTION where-clause violation
INSERT INTO bview ( bid, aid, score) VALUES ( 5, 4, 10);
-- ORA-01402: view WITH CHECK OPTION where-clause violation
INSERT INTO bview ( bid, aid, score) VALUES ( 5, 4, 110);
--1 ν μ΄(κ°) μ½μ
λμμ΅λλ€.
λ·° μ¬μ©μ΄μ ? -- μμ£Ό μ¬μ©λλ κΈ΄ 쿼리( SELECT ) -> λ·° μμ± , 보μ, νΈλ¦¬μ± |
[μνμ€ SEQUENCE ]
SELECT *
FROM dept;
deptno λΆμλ²νΈ μλ μ€μ ..
INSERT INTO dept VALUES ( 50, OPERATIONS BOSTON );
INSERT INTO dept VALUES ( 60, OPERATIONS BOSTON );
INSERT INTO dept VALUES ( 70, OPERATIONS BOSTON );
ex) μν μ
무 μ²λ¦¬ μ μΌ λ¨Όμ ν΄μΌ λ μΌ ? μμ - λ²νΈν λ½μμ. μνμ€ == [μν λ²νΈ μ₯μΉ] μ²μκ²½μ°° 10κ°―μ -> chache / μΊμκΈ°λ₯ (μ±λ₯ λμΌ μ μμ) |
γνμγ
CREATE SEQUENCE μνμ€λͺ
[ INCREMENT BY μ μ] 1
[ START WITH μ μ] 1
[ MAXVALUE n ¦ NOMAXVALUE] 100
[ MINVALUE n ¦ NOMINVALUE] 5
[ CYCLE ¦ NOCYCLE]
[ CACHE n ¦ NOCACHE];
-- μνμ€
CREATE SEQUENCE seq_dept
INCREMENT BY 10
START WITH 50
MAXVALUE 90
NOCYCLE
NOCACHE;
-- Sequence SEQ_DEPTμ΄(κ°) μμ±λμμ΅λλ€.
INSERT INTO dept VALUES ( seq_dept.nextval , 'QC', 'SEOUL' );
SELECT seq_dept.currval
From dual;
INSERT INTO dept VALUES ( seq_dept.nextval , 'XX', 'SEOUL' );
COMMIT;
CREATE SEQUENCE
ALTER SEQUENCE
DROP SEQUENCE seq_dept;
μ΅κ·ΌλκΈ