๐Ÿ’ป

Oracle DBMS ๋ฐ SQL - ์„ค์น˜ / SELECT๋ฌธ ๋ณธ๋ฌธ

KITRI/ORACLE

Oracle DBMS ๋ฐ SQL - ์„ค์น˜ / SELECT๋ฌธ

๋˜ํšจ๋‹ˆ 2020. 4. 7. 09:51

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ž€? ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ ์†ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. 

 

๋ฐ์ดํ„ฐ

  • ์ •ํ˜• ๋ฐ์ดํ„ฐ : ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ๊ฒƒ. ์˜ˆ) RDB, ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ, CSV ๋“ฑ
  • ๋ฐ˜์ •ํ˜• ๋ฐ์ดํ„ฐ : ํ˜•ํƒœ๊ฐ€ ์žˆ์œผ๋ฉฐ ์—ฐ์‚ฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ. ์˜ˆ) XML, HTML, JSON, ๋กœ๊ทธ ๋“ฑ
  • ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ : ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š์€ ๊ฒƒ. ์˜ˆ) ์†Œ์…œ๋ฐ์ดํ„ฐ(ํŽ˜์ด์Šค๋ถ, ํŠธ์œ„ํ„ฐ), ์ด๋ฏธ์ง€, ์˜์ƒ, ์Œํ–ฅ ๋“ฑ

RDBMS(Relational Database Management System) : ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ˆ˜์ •ํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์†Œํ”„ํŠธ์›จ์–ด.

  1. ์ •ํ˜•ํ™” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ๋›ฐ์–ด๋‚˜๋‹ค.
  2. Oracle, MSSQL, MYSQL ๋“ฑ์ด ์žˆ๋‹ค. 

SQL ๋ฌธ๋ฒ• 3๊ฐ€์ง€

  1. ๋ฐ์ดํ„ฐ ์ •์˜ ์–ธ์–ด(DDL=Data Definition Language) : ํ…Œ์ด๋ธ” ์ƒ์„ฑ, ๋ณ€๊ฒฝ, ์‚ญ์ œ, ์ˆ˜์ • (CREATE, DROP, ALTER, TRUNCATE)
  2. ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด(DML=Data Manipulation Language) : ๋ฐ์ดํ„ฐ(๋ ˆ์ฝ”๋“œ) ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ, ์„ ํƒ (SELECT, INSERT, UPDATE, DELETE, COMMIT, ROLLBACK)
  3. ๋ฐ์ดํ„ฐ ์ œ์–ด ์–ธ์–ด(DCL=Data Control Language) : ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์šฉ ๊ถŒํ•œ ๋ฐ ์ทจ์†Œ ์‹œ ์‚ฌ์šฉ (GRANT, REVOKE)

 

<์‹ค์Šต>

 

1. ์˜ค๋ผํด ์„ค์น˜(Oracle Database 11g Release 2 XE)
https://www.oracle.com/database/technologies/xe-prior-releases.html

 

XE Prior Release Archive

Support Oracle Database Express Edition (XE) is a community supported edition of the Oracle Database family. Please go to the Oracle Database XE Community Support Forum for help, feedback, and enhancement requests. Note: Oracle Support Services only provid

www.oracle.com

 

2. cmd ์ฐฝ์—์„œ

 

 

3. hr ๊ณ„์ • lock ํ•ด์ œ

> alter user hr acount unlock;

> alter user hr identified by 1234;(1234๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ. ์›ํ•˜๋Š”๋Œ€๋กœ ์ง€์ •)

> conn hr/1234;

 

 

4. SQL developer ์„ค์น˜(Windows 64-bit with JDK 8 included ๋‹ค์šด๋กœ๋“œ)

https://www.oracle.com/tools/downloads/sqldev-downloads.html

 

Oracle SQL Developer Downloads

This archive. will work on a 32 or 64 bit Windows OS. The bit level of the JDK you install will determine if it runs as a 32 or 64 bit application. This download does not include the required Oracle Java JDK. You will need to install it if it's not already

www.oracle.com

์ƒˆ๋กœ๋งŒ๋“ค๊ธฐ๋กœ 'hr' ์ ‘์† ์„ค์ •. ์‚ฌ์šฉ์ž์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ ‘์†์‹œ์— ๊ณ„์ • ์„ค์ •.

๋„๊ตฌ > ํ™˜๊ฒฝ์„ค์ • > ์ฝ”๋“œ ํŽธ์ง‘๊ธฐ > ๊ธ€๊ผด ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ, ํ–‰๋ฒˆํ˜ธ ์ถ”๊ฐ€, ์˜คํ† ์ปค๋ฐ‹ ์„ค์ •

 

 

5. ํ…Œ์ด๋ธ”์€ ํ•„๋“œ์™€ ๋ ˆ์ฝ”๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค. 

ํ–‰ = ROW = ๋ ˆ์ฝ”๋“œ(Record) 

์—ด = COLUMN = ํ•„๋“œ(Field)

 

๊ฐœ์ฒด(Entitiy) : ๋ ˆ์ฝ”๋“œ

์†์„ฑ(Attribute) : ํ•„๋“œ

 

 

6. ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ๋ณด๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” describe ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์ž.

์˜ค๋ผํด์˜ ์ž๋ฃŒํ˜•์€ ํฌ๊ฒŒ ๋ฌธ์ž, ์ˆซ์ž, DATE ๊ฐ€ ์žˆ๋‹ค. 

 

 

7. SELECT ๊ฒ€์ƒ‰(์„ ํƒ)๋ฌธ


--ํ˜•์‹) select [์ปฌ๋Ÿผ๋ช… ๋˜๋Š” ํ‘œํ˜„์‹]
--       from [ํ…Œ์ด๋ธ”๋ช… ๋˜๋Š” ๋ทฐ๋ช…]
--       where ์กฐ๊ฑด์ ˆ
--       group by [์ปฌ๋Ÿผ๋ช…] having ์กฐ๊ฑด์ ˆ
--       order by [์ปฌ๋Ÿผ๋ช…]

--1. ๋‹จ์ˆœ๊ฒ€์ƒ‰ > select [์ปฌ๋Ÿผ๋ช… ๋˜๋Š” ํ‘œํ˜„์‹] from [ํ…Œ์ด๋ธ”๋ช… ๋˜๋Š” ๋ทฐ๋ช…]

 

--2. ๋ชจ๋“  ํ•„๋“œ(์—ด) ์กฐํšŒ > *

 

--3. ์ค‘๋ณต ํ–‰ ์ œ๊ฑฐ  > DISTINCT ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ

 

 

--์—ฐ์‚ฐ(์‚ฐ์ˆ ์—ฐ์‚ฐ) +, -, *, /

AS ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ปฌ๋Ÿผ๋ช…์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

--5. NULL์€ ๋ฏธํ™•์ • ๋„๋Š” ์•Œ ์ˆ˜ ์—†๋Š” ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์—ฐ์‚ฐ, ํ• ๋‹น, ๋น„๊ต๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. 

NVL(์นผ๋Ÿผ, ์น˜ํ™˜ํ•  ๊ฐ’) : null ๊ฐ’์„ ์น˜ํ™˜ํ•  ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ

 

 

--6. ๋ณ„์นญ (Alias) 
--๊ทœ์น™ : ๊ณต๋ฐฑ, ํŠน์ˆ˜๋ฌธ์ž, ์†Œ๋ฌธ์ž ์ถœ๋ ฅ์‹œ "์Œ๋”ฐ์˜ดํ‘œ"
--         salary as ์—ฐ๋ด‰ : as ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

--7. concatnation(ํ•ฉ์„ฑ๋ฌธ์ž, ์—ฐ๊ฒฐ์—ฐ์‚ฐ์ž) 

 

 

โ€ป ์ถ”๊ฐ€ ์ •๋ฆฌ

 

- ์Šคํ‚ค๋งˆ(Schema) ๋Š” ํ…Œ์ด๋ธ”, ๋ทฐ, ์ธ๋ฑ์Šค์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋…ผ๋ฆฌ์ ์ธ ๋ฐ์ดํ„ฐ ์ €์žฅ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.

   (์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” hr ์Šคํ‚ค๋งˆ๋ฅผ ์ด์šฉํ•ด์„œ ์‹ค์Šตํ•˜์˜€๋‹ค.)

 

- ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋„์‹ํ™”ํ•œ ๊ฒƒ์„ ERD๋ผ๊ณ  ํ•œ๋‹ค.

 

- ์ง‘ํ•ฉ์  ์–ธ์–ด์ธ SQL์„ ํ™•์žฅํ•œ ์ ˆ์ฐจ์  ์–ธ์–ด๊ฐ€ PL/SQL์ด๋ฉฐ, DB ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ PL/SQL์„ ์ด์šฉํ•ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. 

๋ฐ˜์‘ํ˜•
Comments