๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

DB์™€ SQL

SQLD : 2๊ณผ๋ชฉ SQL ๊ธฐ๋ณธ ๋ฐ ํ™œ์šฉ (SQL ๊ธฐ๋ณธ)

728x90

๐Ÿ“ DB

1. ํŠน์ • ๊ธฐ์—…์ด๋‚˜ ์กฐ์ง ๋˜๋Š” ๊ฐœ์ธ์ด ํ•„์š”์— ์˜ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ •ํ•œ ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด๋†“์€ ๊ฒƒ

2. ์—ฌ๋Ÿฌ ์‚ฌ๋žŒ์ด ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋ชฉ์ ์œผ๋กœ ํ†ตํ•ฉ, ์ €์žฅ, ์šด์˜, ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ

 

๐Ÿ’ป DBMS

1. ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๋ถ€ํ„ฐ ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์‚ฌ๊ฑด์œผ๋กœ ์ธํ•œ ๋ฐ์ดํ„ฐ ์†์ƒ์„ ํ”ผํ•˜๊ณ , ํ•„์š”์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์˜ SW

2. ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ์˜ ๋ณต์žก์„ฑ์„ ํ•ด๊ฒฐํ•ด์ฃผ๊ณ  ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€, ์ˆ˜์ •, ๊ฒ€์ƒ‰, ์‚ญ์ œ, ๋ฐฑ์—…, ๋ณด์•ˆ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” SW

 

๐Ÿ–Š SQL

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ ์ •์˜, ์กฐ์ž‘, ์ œ์–ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด

  • DML : SELECT, INSERT, UPDATE, DELETE
  • DDL : CREATE, ALTER, DROP, RENAME
  • DCL : GRANT, REVOKE
  • TCL : COMMIT, ROLLBACK

 

๐Ÿ“‹ TABLE

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ธฐ๋ณธ ๋‹จ์œ„๋กœ, ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ

  • ๊ฐ€๋กœ : ํ–‰ = ROW
  • ์„ธ๋กœ : ์—ด = COLUMN

 

๐Ÿ“Œ ์ •๊ทœํ™”

1. ๋ฐ์ดํ„ฐ์˜ ์ •ํ•ฉ์„ฑ ํ™•๋ณด์™€ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ / ์ˆ˜์ • / ์‚ญ์ œ์‹œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ด์ƒํ˜„์ƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๋ณต ์ œ๊ฑฐํ•˜๋Š” ์ž‘์—…

2. ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„์—์„œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์กฐํ™”ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค

 

๐Ÿ“Œ ๋ฐ˜์ •๊ทœํ™”

1. ์ •๊ทœํ™”๋œ Entity, ์†์„ฑ, ๊ด€๊ณ„์— ๋Œ€ํ•ด ์„ฑ๋Šฅํ–ฅ์ƒ๊ณผ ๊ฐœ๋ฐœ ๋ฐ ์šด์˜์˜ ๋‹จ์ˆœํ™”๋ฅผ ์œ„ํ•ด ์ค‘๋ณต, ํ†ตํ•ฉ, ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ๊ธฐ๋ฒ•

 

๐Ÿ”‘ ๊ธฐ๋ณธํ‚ค 

1. ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๊ฐ ํ–‰์„ ํ•œ ๊ฐ€์ง€ ์˜๋ฏธ๋กœ ํŠน์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ์นผ๋Ÿผ

2. ์œ ์ผ์„ฑ, ์ตœ์†Œ์„ฑ, ๋Œ€ํ‘œ์„ฑ์„ ๋งŒ์กฑํ•˜๋ฉฐ, null์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ํ‚ค

 

๐Ÿ”‘ ์™ธ๋ถ€ํ‚ค

1. ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์นผ๋Ÿผ


1๏ธโƒฃ DDL - CREATE, ALTER, DROP, RENAME

 

ํ…Œ์ด๋ธ” ๋ช…์€ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ” ๋ช…๊ณผ ์ค‘๋ณต๋˜์„  ์•ˆ๋ฉ๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ” ๋‚ด ์นผ๋Ÿผ ๋ช…์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๊ฐ ์นผ๋Ÿผ์€ , ๋กœ ๊ตฌ๋ถ„๋˜๊ณ  ; ๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ”๋ช…๊ณผ ์นผ๋Ÿผ๋ช…์€ ๋ฐ˜๋“œ์‹œ ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

A-Z, a-z, 0-9, _, $, #๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐ์ดํ„ฐ ์œ ํ˜•

  • CHAR(s) : ๊ณ ์ • ๊ธธ์ด ๋ฌธ์ž์—ด ์ •๋ณด ์ตœ๋Œ€, ๊ธธ์ด ๋งŒํผ ๊ณต๊ฐ„ ์ฑ„์›€  |  ‘AA’ = ‘AA ’
  • VARCHAR2(s) : ๊ฐ€๋ณ€ ๊ธธ์ด ๋ฌธ์ž์—ด ์ •๋ณด ํ• ๋‹น๋œ ๋ณ€์ˆ˜ ๊ฐ’์˜ ๋ฐ”์ดํŠธ๋งŒ ์ ์šฉ  |  ‘AA’ != ‘AA ’
  • NUMBER : ์ •์ˆ˜, ์‹ค์ˆ˜ ๋“ฑ ์ˆซ์ž ์ •๋ณด
  • DATE : ๋‚ ์งœ์™€ ์‹œ๊ฐ ์ •๋ณด

 

ํ…Œ์ด๋ธ” ์ƒ์„ฑ 

CREATE TABLE PLAYER (
PLAYER_ID CHAER(7) NOT NULL,
PLAYER_NAME VARCHAR2(20) NOT NULL
);

CREATE TABLE VIP_PLAYER AS SELECT * FROM PLAYER;

CREATE TABLE VIP_PLAYER AS SELECT PLAYER_NAME FROM PLAYER;

 

ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ๋ณ€๊ฒฝ

์ปฌ๋Ÿผ ์ถ”๊ฐ€
ALTER TABLE PLAYER ADD(ADDRESS VARCHAR2(80));

์ปฌ๋Ÿผ ์‚ญ์ œ
ALTER TABLE PLAYER DROP COLUMN ADDRESS;

์ปฌ๋Ÿผ ์ˆ˜์ •
ALTER TABLE TEAM_TEMP MODIFY(ORIG_YYYY VARCHAR2(8) DEFAULT '20020129' NOT NULL);

 

์ œ์•ฝ์กฐ๊ฑด ์‚ญ์ œ & ์ถ”๊ฐ€

DROP CONSTRAINT ์กฐ๊ฑด๋ช…;

ADD CONSTRAINT ์กฐ๊ฑด๋ช… ์กฐ๊ฑด (์นผ๋Ÿผ๋ช…);

 

ํ…Œ์ด๋ธ” ๋ช… ๋ณ€๊ฒฝ & ํ…Œ์ด๋ธ” ์‚ญ์ œ & ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ์‚ญ์ œ & ์ปฌ๋Ÿผ ๋ช… ๋ณ€๊ฒฝ

RENAME PLAYER TO PLAYER_BACKUP;

DROP TABLE PLAYER;

TRUNCATE TABLE PLAYER;

RENAME COLUMN TEAM_ID TO T_ID;

 

 

2๏ธโƒฃ DML - INSERT, UPDATE, DELETE, SELECT

๊ธฐ๋ณธ ์˜ˆ์ œ

INSERT INTO PLAYER (PLAYER_NAME, PLAYER_NO) VALUES ('LSG', 54);

UPDATE PLAYER SET PLAYER_RANK = '์ผ๋ฐ˜ํšŒ์›';

DELETE FROM PLAYER;

SELECT PLAYER_ID FROM PLAYER;

SELECT DISTINCT PLAYER_NAME AS "์„ ์ˆ˜๋ช…" FROM PLAYER;

/*
DISTINCT : ์ค‘๋ณต ์‹œ 1ํšŒ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
AS : ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์ปฌ๋Ÿผ๋ช…์„ ์ž„์‹œ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
*/

 

์™€์ผ๋“œ ์นด๋“œ๋Š” ๊ฒ€์ƒ‰ํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๋•์Šต๋‹ˆ๋‹ค.

*, %, _

SELECT PLAYER_NO FROM PLAYER WHERE PLAYER_NAME LIKE 'L%';

SELECT PLAYER_NO FROM PLAYER WHERE PLAYER_NAME LIKE '%L%';

SELECT PLAYER_NO FROM PLAYER WHERE PLAYER_NICKNAME LIKE 'COLL%' AND PLAYER_NAME = '%L';

SELECT PLAYER_NO FROM PLAYER WHERE PLAYER_NAME LIKE 'L_';

SELECT PLAYER_NO FROM PLAYER WHERE PLAYER_NAME LIKE '_L___';

 

ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์™€ ๋ฌธ์ž๋ฅผ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

SELECT PLAYER_NAME || '๋‹˜์˜ ๋ฒˆํ˜ธ๋Š” ' || PLAYER_NO || ' ์ž…๋‹ˆ๋‹ค.' FROM PLAYER;

 

์‚ฐ์ˆ ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์น™์—ฐ์‚ฐ๊ณผ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋กœ, ๋”ํ•˜๊ธฐ / ๋นผ๊ธฐ / ๊ณฑํ•˜๊ธฐ / ๋‚˜๋ˆ„๊ธฐ / ์šฐ์„ ์ˆœ์œ„ ์„ค์ •์ด ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT NAME AS "์ด๋ฆ„", WEIGHT/((HEIGHT/100)*(HEIGHT/100)) AS "BMI ๋น„๋งŒ์ง€์ˆ˜" FROM FAMILY;

์ด๋ฆ„           BMI ๋น„๋งŒ์ง€์ˆ˜
---------      -----------
LSG            20
MIN	       25

 

 

3๏ธโƒฃ TCL

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํŠธ๋žœ์žญ์…˜์€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์˜ ํ•œ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. ์˜ค๋ผํด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ SQL ๋ช…๋ น๋ฌธ๋“ค์„ ํ•˜๋‚˜์˜ ๋…ผ๋ฆฌ์ ์ธ ์ž‘์—… ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์ด๋ฅผ ํŠธ๋žœ์žญ์…˜์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ช…๋ น์–ด ์ค‘์—์„œ ํ•˜๋‚˜์˜ ๋ช…๋ น์–ด๋ผ๋„ ์ž˜๋ชป๋˜์—ˆ๋‹ค๋ฉด ์ „์ฒด๋ฅผ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.

 

COMMIT;
์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ฐ˜์˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.

ROLLBACK; 
ROLLBACK TO SVPT1;
ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘ ์ด์ „์˜ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค. 

SAVEPOINT;
SAVEPOINT SVPT1;
์ €์žฅ ์ง€์ ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.