day7

์ƒ์€๐Ÿ‘ธยท2023๋…„ 12์›” 5์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ์„ธ๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
7/11
post-thumbnail

๐Ÿ“– ์„œ๋ธŒ์ฟผ๋ฆฌ ์—ฐ์Šต๋ฌธ์ œ

student, department ํ…Œ์ด๋ธ” ํ™œ์šฉ
ํ•™๊ณผ ์ด๋ฆ„, ํ•™๊ณผ๋ณ„ ์ตœ๋Œ€ํ‚ค, ํ•™๊ณผ๋ณ„ ์ตœ๋Œ€ํ‚ค๋ฅผ ๊ฐ€์ง„ ํ•™์ƒ๋“ค์˜ ์ด๋ฆ„๊ณผ ํ‚ค๋ฅผ ์ถœ๋ ฅ ํ•˜์„ธ์š”.

๋ฐฉ์•ˆ 1) WHERE ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ๋„ฃ์—ˆ์„๋•Œ

SELECT 
	d.dname ํ•™๊ณผ์ด๋ฆ„, s.deptno1 ํ•™๊ณผ๋ฒˆํ˜ธ, s.height ํ•™๊ณผ๋ณ„์ตœ๋Œ€ํ‚ค, 
    s.name ํ•™์ƒ์ด๋ฆ„, s.height ํ•™์ƒํ‚ค
FROM student s, department d
WHERE (s.deptno1, s.height) IN (SELECT deptno1, MAX(height)
                                FROM student
                                GROUP BY deptno1)
    AND s.deptno1 = d.deptno;

==> ํ•™๊ณผ๋ณ„ ์ตœ๋Œ€ํ‚ค ์ฟผ๋ฆฌ ๋จผ์ € ๋งŒ๋“ค๊ณ !
==> ํ•™๊ณผ์ด๋ฆ„์ด ๋‚˜์™€์•ผํ•˜๋‹ˆ๊นŒ department ํ…Œ์ด๋ธ”์ด๋ž‘ ์กฐ์ธํ•˜๊ณ  ๊ฒน์น˜๋Š” ๊ฒƒ๋“ค๋ผ๋ฆฌ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๊ณ 
==> ํ•™๊ณผ๋ณ„๋กœ ์ตœ๋Œ€ํ‚ค ๋‚˜์˜ค๋Š”๊ฑฐ๋‹ˆ๊นŒ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ–‰์ด ๋‚˜์˜ค๊ฒ ์ง€ ๊ทธ๋ž˜์„œ IN ์œผ๋กœ ๋ฌถ์–ด์ฃผ๊ณ 
==> ํ•™๊ณผ๋ณ„ ์ตœ๋Œ€ํ‚ค ์ฟผ๋ฆฌ๋‹ˆ๊นŒ ํ•™๊ณผ๋ณ„ ์ตœ๋Œ€ํ‚ค = ์ œ์ผํฐํ‚ค๋ฅผ ๊ฐ€์ง„ ํ•™์ƒํ‚ค ๋‹ˆ๊นŒ s.height ๋ณ€์ˆ˜ ๋˜‘๊ฐ™์€๊ฑฐ๊ณ !

๋ฐฉ์•ˆ 2) WHERE ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ๋„ฃ์—ˆ์„๋•Œ + select ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ๋„ฃ์—ˆ์„๋•Œ

SELECT 
    d.dname ํ•™๊ณผ์ด๋ฆ„, 
    s.deptno1 ํ•™๊ณผ๋ฒˆํ˜ธ, 
    (SELECT MAX(height)
     FROM student s2
     WHERE s2.deptno1 = s.deptno1) ํ•™๊ณผ๋ณ„์ตœ๋Œ€ํ‚ค, 
    s.name ํ•™์ƒ์ด๋ฆ„, 
    s.height ํ•™์ƒํ‚ค
FROM student s, department d
WHERE (s.deptno1, s.height) IN (SELECT deptno1, MAX(height)
                                FROM student
                                GROUP BY deptno1)
    AND s.deptno1 = d.deptno;

==> ์—ฌ๊ธฐ์„œ๋Š” s.height๋ผ๊ณ  ํ‘œ์‹œ์•ˆํ•˜๊ณ  ํ•™๊ณผ๋ณ„ ์ตœ๋Œ€ํ‚ค๋ฅผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด์„œ ์…€ํ”„์กฐ์ธ์œผ๋กœ ํ•ด์„œ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์Œ!

๋ฐฉ์•ˆ 3) FROM ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ๋„ฃ์—ˆ์„๋•Œ name..์€ ์–ด์ผ€ ๋ถ€๋ฅด์ง€? -> ์…€ํ”„์กฐ์ธ!!!!

SELECT 
	d.dname ํ•™๊ณผ์ด๋ฆ„, s1.max_height ํ•™๊ณผ๋ณ„์ตœ๋Œ€ํ‚ค, 
    s2.name ํ•™์ƒ์ด๋ฆ„, s2.height ํ•™์ƒํ‚ค
FROM (SELECT deptno1, MAX(height) max_height
        FROM student
        GROUP BY deptno1) s1, student s2, department d
WHERE s1.deptno1 = d.deptno
    AND s1.deptno1 = s2.deptno1
    AND s1.max_height = s2.height;

==> ํ…Œ์ด๋ธ” ์ž์ฒด๋ฅผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ s1์— ์žˆ๋Š” ์ปฌ๋Ÿผ์„ ํ•™๊ณผ๋ณ„ ์ตœ๋Œ€ํ‚ค๊ฐ€ ๋‚˜์˜ค๊ฒŒ ํ•˜๊ณ 
==> ์…€ํ”„์กฐ์ธ์œผ๋กœ ํ•ด์„œ s1.deptno1 = s2.deptno1, s1.max_height = s2.height๋กœ s1๊ณผ s2๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์Œ!

๋ฐฉ์•ˆ 3) FROM ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ๋„ฃ์—ˆ์„๋•Œ + select ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ๋„ฃ์—ˆ์„๋•Œ

SELECT 
    (SELECT dname
     FROM department d
     WHERE d.deptno = s1.deptno1) ํ•™๊ณผ์ด๋ฆ„, 
    s1.max_height ํ•™๊ณผ๋ณ„์ตœ๋Œ€ํ‚ค, 
    s2.name ํ•™์ƒ์ด๋ฆ„, 
    s2.height ํ•™์ƒํ‚ค
FROM (SELECT deptno1, MAX(height) max_height
        FROM student
        GROUP BY deptno1) s1, student s2
WHERE s1.deptno1 = s2.deptno1
    AND s1.max_height = s2.height;

==> department ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ select์ ˆ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด์„œ student ํ…Œ์ด๋ธ”๊ณผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์Œ!

๐Ÿ’ป

๐Ÿ“– SQL ์ฟผ๋ฆฌ ๊ตฌ๋ถ„

(1) DDL(์ •์˜์–ด-Data Definition Language)

: ํ…Œ์ด๋ธ” ๊ด€๋ จ
CREATE, ALTER, TRUNCATE(ํ…Œ์ด๋ธ”์•ˆ์˜ ๋‚ด์šฉ์‚ญ์ œ), DROP(ํ…Œ์ด๋ธ”์ž์ฒด์‚ญ์ œ)

ํ…Œ์ด๋ธ” ์ƒ์„ฑ - CREATE
CREATE TABLE SAMPLE01
(
    no NUMBER(4),
    id VARCHAR2(12),
    name VARCHAR2(30), --ํ•œ๊ธ€10๊ธ€์ž
    st_date DATE    
);

==> SAMPLE01 ํ…Œ์ด๋ธ” ์ƒ์„ฑ!
==> no, id, name, st_date ์ปฌ๋Ÿผ

ํ…Œ์ด๋ธ” ๋ณต์‚ฌ - CREATE
CREATE TABLE ํ…Œ์ด๋ธ”๋ช…
AS
SELECT * FROM ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ”;
ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌ(์กฐ๊ฑด์— ์ผ๋ถ€๋Ÿฌ ๊ฑฐ์ง“์„ ์ค˜์„œ ๋‚ด์šฉ์€ ์•„๋ฌด๊ฒƒ๋„ ๋ณต์‚ฌ ์•ˆ๋˜๊ฒŒ๋”!)
CREATE TABLE ํ…Œ์ด๋ธ”๋ช…
AS
SELECT * FROM ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ” WHERE ์กฐ๊ฑด๋ชจ๋“ ๊ฒŒ๊ฑฐ์ง“(1=2);

--์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ ์ถ”๊ฐ€

*** ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ - ALTER
ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
ADD (์ปฌ๋Ÿผ๋ช… ํƒ€์ž…์‚ฌ์ด์ฆˆ);

ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
ADD (์ปฌ๋Ÿผ๋ช… ํƒ€์ž…์‚ฌ์ด์ฆˆ DEFAULT ๊ฐ’);
--์ปฌ๋Ÿผ๋ช… ๋ณ€๊ฒฝ -> ๊ฑฐ์˜์•ˆ์“ฐ๊ธฐ๋•Œ๋ฌธ์— ์“ธ๋•Œ ๊ตฌ๊ธ€๋งํ•ด์„œ ์•Œ๋ฉด ๋จ~!
loc -> location
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… RENAME COLUMN loc TO location;
--ํ…Œ์ด๋ธ” ์ด๋ฆ„ ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝ
RENAME ์›๋ž˜ํ…Œ์ด๋ธ”๋ช… TO ๋ฐ”๊ฟ€ํ…Œ์ด๋ธ”๋ช…;
--์ปฌ๋Ÿผ ์‚ฌ์ด์ฆˆ ์กฐ์ •
ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
MODIFY (name VARCHAR2(60));
--์ปฌ๋Ÿผ ์‚ญ์ œ
ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
DROP COLUMN ์ปฌ๋Ÿผ๋ช…;
ํ…Œ์ด๋ธ” ์‚ญ์ œ - DROP
--DROP TABLE SAMPLE01; --์‹ค๋ฌด์—์„  ์กฐ์‹ฌํ•ด์•ผํ•œ๋‹ค!! ํ•จ๋ถ€๋กœ ์ง€์šฐ๋ฉด ํฐ์ผ

(2) DML(์กฐ์ž‘์–ด-Data Manipulation Language)

: ํ…Œ์ด๋ธ” ๋‚ด๋ถ€๋ฐ์ดํ„ฐ ์กฐ์ž‘
INSERT, UPDATE, DELETE, SELECT, MERGE

๋ฐ์ดํ„ฐ ์ถ”๊ฐ€(์‚ฝ์ž…) - INSERT
INSERT INTO ํ…Œ์ด๋ธ”๋ช… (์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช…)
VALUES (๊ฐ’, ๊ฐ’, ๊ฐ’);
--๋ชจ๋“  ์ปฌ๋Ÿผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒฝ์šฐ, ์ปฌ๋Ÿผ๋ช… ์ƒ๋žต ๊ฐ€๋Šฅ
INSERT INTO ํ…Œ์ด๋ธ”๋ช… 
VALUES (๊ฐ’, ๊ฐ’, ๊ฐ’);
๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ์ €์žฅ
INSERT INTO ํ…Œ์ด๋ธ”๋ช…
SELECT ์ปฌ๋Ÿผ๋ช… FROM ๊ฐ€์ ธ์˜ฌํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด;
๋ฐ์ดํ„ฐ ์ˆ˜์ •(์—…๋ฐ์ดํŠธ) - UPDATE
UPDATE ํ…Œ์ด๋ธ”๋ช…
SET ์ปฌ๋Ÿผ๋ช… = ๊ฐ’
WHERE ์กฐ๊ฑด;

๐ŸŸ ์—ฐ์Šต๋ฌธ์ œ

temp_professor ํ…Œ์ด๋ธ”์—์„œ
'Sharon Stone' ๊ต์ˆ˜์˜ ์ง๊ธ‰๊ณผ ๋™์ผํ•œ ์ง๊ธ‰์„ ๊ฐ€์ง„ ๊ต์ˆ˜๋“ค ์ค‘ ํ˜„์žฌ ๊ธ‰์—ฌ๊ฐ€ 250 ๋งŒ์›์ด ์•ˆ ๋˜๋Š” ๊ต์ˆ˜๋“ค์˜ ๊ธ‰์—ฌ๋ฅผ 15% ์ธ์ƒํ•˜์„ธ์š”.

UPDATE temp_professor
SET pay = pay * 1.15
WHERE position = (SELECT position
                  FROM temp_professor
                  WHERE name = 'Sharon Stone')
AND pay < 250;

==> pay๊ฐ€ 253์œผ๋กœ ๋ฐ”๋€Œ๊ธฐ ์ „์—๋Š” 220 ์ด์˜€๋‹ค!

๐Ÿ’ป

๋ฐ์ดํ„ฐ ์‚ญ์ œ - DELETE
DELETE FROM ํ…Œ์ด๋ธ”๋ช…
WHERE ์กฐ๊ฑด;

DML ๋ฐ์ดํ„ฐ ์กฐ์ž‘์„ ํ•˜๋Š” ๊ฒฝ์šฐ -> COMMIT ๊นŒ์ง€ ์™„๋ฃŒํ•ด์•ผ ์ตœ์ข…์ ์œผ๋กœ ์ ์šฉ!

(3) DCL(์ œ์–ด์–ด-Data Control Language)

: ์กฐ์ž‘ ๊ถŒํ•œ
GRANT(๊ถŒํ•œ๋ถ€์—ฌ), REVOKE(๊ถŒํ•œํšŒ์ˆ˜)

(4) TCL(ํŠธ๋žœ์žญ์…˜ ์ œ์–ด-Transaction Control Language)

: ํŠธ๋žœ์žญ์…˜ ์ œ์–ด
COMMIT, ROLLBACK

profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€