๐Ÿ“‚๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค/๐Ÿ“ŠDBMS

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ SQL - SELECT๋กœ ๋ฐ์ดํ„ฐ ์กฐํšŒํ•˜๊ธฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์น˜, DBeaver, WHERE ์ ˆ

๐Ÿ‘ฉ‍๐ŸŽ“์ธํ…”๋ฆฌ๊ฐ์ž๐Ÿฅ” 2023. 5. 11. 17:53

DBMS์˜ ์ •์˜

DataBase Managemaent System 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์šด์˜ํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” SQL

๋ฐ์ดํ„ฐ์— ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ ‘๊ทผ ๋ฐ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์•ผ ํ•จ

์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…, ์‚ญ์ œ, ๊ฐฑ์‹ ์œผ๋กœ ํ•ญ์ƒ ์ตœ์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•จ

์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๊ฐ€ ๋™์‹œ์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ

๋ฐ์ดํ„ฐ ์ฃผ์†Œ๋‚˜ ์œ„์น˜์— ์˜ํ•ด์„œ๊ฐ€ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋‚ด์šฉ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์Œ

 

DBMS์˜ ์ข…๋ฅ˜

Oracle, MySQL์ด ์••๋„์ 

 

๊ด€๊ณ„ํ˜• DBMS(Relational DBMS)

ํ…Œ์ด๋ธ”(Table) = ๊ด€๊ณ„(Relation)

๋‚˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋ฌด์ˆ˜ํžˆ ๋งŽ๋‹ค. ํ‚ค, ์ง€์—ญ, ์ธ์ข…, ๋“ฑ๋“ฑ.. ๊ทธ ์ค‘์—์„œ ์ด๋ฆ„, ์•„์ด๋”” ๋‘ ๊ฐ€์ง€๋งŒ ์ˆ˜์ง‘์„ ํ•˜๊ฒ ๋‹ค.

์‚ฌ๋žŒ์ด๋ผ๋Š” ๊ฐ์ฒด์—์„œ ๊ทธ ์ด๋ฆ„๊ณผ ์•„์ด๋””๋ผ๋Š” ๋‘ ๊ฐ€์ง€๋ฅผ ์ˆ˜์ง‘์„ ํ•˜๊ฒ ๋‹ค. ๊ทธ๊ฒƒ์„ ์†์„ฑ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

MySQL ์„ค์น˜

์ธ์ฆ ๋ฐฉ๋ฒ• ๊ณ ๋ฅด๊ธฐ

๋น„๋ฐ€๋ฒˆํ˜ธ ์„ค์ •

 

์•„๊นŒ ์ „์— ์ž…๋ ฅํ–ˆ๋˜ ๋น„๋ฐ€๋ฒˆํ˜ธ

์ฒดํฌํ•ด์ œํ•˜๊ณ  finish

์„ค์น˜์™„๋ฃŒ!!

 

 

 

๐Ÿ’กdatagrip์ด๋ผ๋Š” ๊ฒƒ์„ ์“ฐ๋ฉด ์ข‹๋‹ค.

์ž๋™์™„์„ฑ ๊ธฐ๋Šฅ์ด ์•„์ฃผ ๊ฐ•๋ ฅํ•˜๋‹ค.

join ๊ฐ™์€ ๊ฑฐ ํ•  ๋•Œ ์ข‹๊ณ  ์‹คํ–‰์†๋„๋„ ๋น ๋ฅด๋‹ค.

DataGrip: The Cross-Platform IDE for Databases & SQL by JetBrains

 

DataGrip: The Cross-Platform IDE for Databases & SQL by JetBrains

A powerful IDE from JetBrains for SQL on macOS, Windows, and Linux.

www.jetbrains.com

๊ทธ๋Ÿฌ๋‚˜ ์œ ๋ฃŒ....

๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” DBeaver๋ฅผ ์„ค์น˜ํ•  ๊ฒƒ์ด๋‹ค.

 

DBeaver ์„ค์น˜ ๋ฐ MySQL๊ณผ ์—ฐ๊ฒฐ

DBeaver Community | Free Universal Database Tool

 

DBeaver Community | Free Universal Database Tool

DBeaver Universal Database Tool Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases. Supports all popular databases: MySQL, PostgreSQL, SQLite, Oracle, DB2, SQL Server, Sybase,

dbeaver.io

๊ฐ์ž ์šด์˜์ฒด์ œ์—  ๋งž๋Š” ๊ฒƒ ์„ค์น˜

 

ํ•œ๊ตญ์–ด๋กœ, 

Include Java

 

์ด์ œ ๋””๋น„๋ฒ„๋กœ My SQL ์ ‘์†

 

sample database no

MySQL ์„ ํƒ

๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋„ฃ๊ณ  

์™ผ์ชฝ ํ•˜๋‹จ test connection

์—ฐ๊ฒฐ๋จ

์™„๋ฃŒ

 

DBeaver ์‹ค์Šต

 

ํŒŒ์ผ > ๋‹ค์Œ ์ด๋ฆ„ ์ง€์ •๋œ ํŒŒ์ผ ์ฐพ๊ธฐ > ์›ํ•˜๋Š” ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ด

 

์ปจํŠธ๋กค + A , ์•ŒํŠธ + X

์ฐฝ์ด ์ด๋ ‡๊ฒŒ ์ •๋ ฌ ๋˜์—ˆ๋‹ค.

์ƒˆ๋กœ ๊ณ ์นจ

market_db ์ƒ์„ฑ

 

์„ค๋ช…

SQL ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ƒˆ๋กœ์šด script๊ฐ€ ์—ด๋ฆฐ๋‹ค.

 

DB ๋ฐ์ดํ„ฐ ์กฐํšŒ๋ฐฉ๋ฒ•

SELECT๋ฌธ 

**DB๋Š” ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค!!

 

SELECT๋ฌธ์˜ ๊ธฐ๋ณธ ํ˜•์‹

select (์—ด ์ด๋ฆ„)

from (ํ…Œ์ด๋ธ” ์ด๋ฆ„)

where (์กฐ๊ฑด์‹)

group by (์—ด ์ด๋ฆ„)

having (์กฐ๊ฑด์‹)

order by (์—ด ์ด๋ฆ„)

limit ์ˆซ์ž; << ;์ด ๊ผญ ๋“ค์–ด๊ฐ€์•ผ ํ•จ

 

 

์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ง€์ •

use (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„);

use market_db;

 

 

SELECT ~ FROM ~

select ์ ˆ ๊ตฌ์กฐ

select * from member;

-- select : ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์•ฝ์–ด

-- * asterisk. ์ผ๋ฐ˜์ ์œผ๋กœ "๋ชจ๋“  ๊ฒƒ"์„ ์˜๋ฏธํ•จ. ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” ์—ด ์ด๋ฆ„์ด ๋“ค์–ด๊ฐˆ ์ž๋ฆฌ์— ์‚ฌ์šฉ๋˜์–ด "๋ชจ๋“  ์—ด"์„ ๋œปํ•จ]

-- member : ์กฐํšŒํ•  ํ…Œ์ด๋ธ” ์ด๋ฆ„

-- > member ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ์—ด์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์™€๋ผ

-- ์›์น™์ ์œผ๋กœ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค.ํ…Œ์ด๋ธ”์˜ ํ˜•์‹์œผ๋กœ ํ‘œํ˜„ํ•ด์•ผ ํ•จ

select * from market_db.member;

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์ž˜๊ฒŒ ์ชผ๊ฐœ์„œ ์—ฐ๋™ํ•œ๋‹ค.

 

ํ•„์š”ํ•œ ์—ด๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

select mem_name from member;

 

์—ฌ๋Ÿฌ ์—ด์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์œผ๋ฉด ์ฝค๋งˆ๋กœ ์—ฐ๊ฒฐ

-- ๊ธฐ์กด ํ…Œ์ด๋ธ”์˜ ์—ด ์ˆœ์„œ๋Š” ๊ด€๊ณ„ ์—†์ด ๋ณด๊ณ  ์‹ถ์€ ์ˆœ์„œ๋Œ€๋กœ ์—ด์„ ๋‚˜์—ด

select addr, debut_date, mem_name from member;

 

์—ด ์ด๋ฆ„์— ๋ณ„์นญ ์ง€์ •

-- ์—ด ์ด๋ฆ„ ๋‹ค์Œ์— ์ง€์ •ํ•˜๊ณ  ์‹ถ์€ ๋ณ„์นญ ์ž…๋ ฅ

-- ๋ณ„์นญ์— ๊ณต๋ฐฑ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์Œ

select addr ์ฃผ์†Œ, debut_date "๋ฐ๋ท” ์ผ์ž", mem_name from member;

 

select ~ from ~ where ~

ํŠน์ • ์กฐ๊ฑด๋งŒ ์กฐํšŒํ•˜๊ธฐ

-- ๋ถ€ํ•˜ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ์‹ค๋ฌด์—์„œ๋Š” ๋ณดํ†ต where๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ limit์„ ์‚ฌ์šฉํ•จ

 

๊ธฐ๋ณธ์ ์ธ where์ ˆ ๊ตฌ์กฐ

select (์—ด ์ด๋ฆ„) from (ํ…Œ์ด๋ธ” ์ด๋ฆ„) where ์กฐ๊ฑด์‹;

 

select * from member where mem_name = '๋ธ”๋ž™ํ•‘ํฌ';

select * from member where mem_number = 4;

ํ…Œ์ด๋ธ”์—์„œ ์ผ์ผ์ด True or False๋ฅผ ๋Œ€์กฐํ•จ

 

๊ด€๊ณ„ ์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

-- ์ˆซ์ž๋กœ ํ‘œํ˜„๋œ ๋ฐ์ดํ„ฐ๋ผ๋ฉด ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ

select mem_id, mem_name, height from member where height <= 162;

-- ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์Œ

select mem_name, height, mem_number from member where height >= 165 and mem_number > 6;

select mem_nameheightmem_number from member where height >= 165 or mem_number > 6;

 

ํŠน์ • ๋ฒ”์œ„์— ์žˆ๋Š” ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ

select mem_name, height from member where 163 <= height and height <=165; <<๋ถˆํŽธ

select mem_name, height from member where height between 163 and 165;

-- in

select mem_name, addr from member where addr = '๊ฒฝ๊ธฐ' or addr = '์ „๋‚จ' or addr ='๊ฒฝ๋‚จ'; << ๊ฐ€๋…์„ฑ ๋–จ์–ด์ง€๊ณ  ์œ ์ง€ ๋ณด์ˆ˜๋„ ๋–จ์–ด์ ธ์„œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋นผ๋Š” ๊ฒŒ ํž˜๋“ฆ!!

select mem_name, addr from member where addr in ('๊ฒฝ๊ธฐ', '์ „๋‚จ', '๊ฒฝ๋‚จ');

 

๋ฌธ์ž์—ด ์ผ๋ถ€ ๊ฒ€์ƒ‰

select * from member where mem_name like '์šฐ%';

์šฐ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ์กฐํšŒํ•œ๋‹ค!! 0๊ฐœ๊ฐ€ ์™€๋„ ์ƒ๊ด€ ์—†์Œ!!

์šฐ๋กœ ๋๋‚˜๋ ค๋ฉด 

select * from member where mem_name like '%์šฐ';

์ด๋‹ค. 

select * from member where mem_name like '%์šฐ%';

์šฐ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋‹จ์–ด

select * from member where mem_name like '%์ด%';

 

๊ธ€์ž ์ˆ˜ ๋งค์น˜

select * from member where mem_name like '__ํ•‘ํฌ';

์–ธ๋”๋ฐ” 2๊ฐœ. ๋ฌด์–ธ๊ฐ€๊ฐ€ 2 ์นธ ์˜ค๊ณ , ๊ทธ๋Ÿฐ ๋‹ค์Œ์— ํ•‘ํฌ๊ฐ€ ์˜ค๋Š” ๋‹จ์–ด๋ฅผ ์ฐพ์•„ ์ฃผ์„ธ์š”.

select * from member where mem_name like '__ํ•‘ํฌ_';๋Š” ๊ฒ€์ƒ‰ ์•ˆ๋˜๊ณ , 

select * from member where mem_name like '__ํ•‘ํฌ%';๋Š” ์—์ดํ•‘ํฌ ๋ธ”๋ž™ํ•‘ํฌ๊ฐ€ ๋‚˜์˜ด.

 

 

์ปจํŠธ๋กค s๋กœ ์ €์žฅ, 

Open Folder in Explorer ํ•˜๋ฉด ์ €์žฅ๋˜์–ด ์žˆ๋‹ค. 

 

์˜ค๋Š˜ ์‹ค์Šต ๋!!