SQL

์ •ํ˜œ์ธยท2024๋…„ 8์›” 8์ผ
0

sql

๋ชฉ๋ก ๋ณด๊ธฐ
5/7

๐Ÿ’จ SQL (Structured Query Language)

๐Ÿ’ก ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ(RDBMS)์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜, ์กฐ์ž‘, ์ œ์–ด ๋ฐ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด

โ— SQL์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ์ค€ ์–ธ์–ด๋กœ, ๋‹ค์–‘ํ•œ RDBMS์—์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋จ

๐Ÿ’จ SQL์˜ ์ฃผ์š” ๊ธฐ๋Šฅ

โญ• DDL (Data Definition Language)

๐Ÿ’ก ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ตฌ์กฐ(ํ…Œ์ด๋ธ”, ์ธ๋ฑ์Šค, ๋ทฐ ๋“ฑ)๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

โ— ์ฃผ์š” ๋ช…๋ น์–ด

  • CREATE: ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด(ํ…Œ์ด๋ธ”, ์ธ๋ฑ์Šค, ๋ทฐ ๋“ฑ)๋ฅผ ์ƒ์„ฑ
  • ALTER: ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝ
  • DROP: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด๋ฅผ ์‚ญ์ œ
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    position VARCHAR(100),
    salary DECIMAL(10, 2)
);

ALTER TABLE employees ADD COLUMN hire_date DATE;

DROP TABLE employees;

โญ• DML (Data Manipulation Language)

๐Ÿ’ก ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘(์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ, ์กฐํšŒ)ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

โ— ์ฃผ์š” ๋ช…๋ น์–ด

  • SELECT: ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ
  • INSERT: ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…
  • UPDATE: ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •
  • DELETE: ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ
SELECT * FROM employees WHERE salary > 50000;

INSERT INTO employees (id, name, position, salary) VALUES (1, 'John Doe', 'Manager', 60000.00);

UPDATE employees SET salary = 65000 WHERE id = 1;

DELETE FROM employees WHERE id = 1;

โญ• DCL (Data Control Language)

๐Ÿ’ก ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ ‘๊ทผ ๊ถŒํ•œ์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

โ— ์ฃผ์š” ๋ช…๋ น์–ด

  • GRANT: ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌ
  • REVOKE: ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์„ ์ทจ์†Œ
GRANT SELECT, INSERT ON employees TO user1;

REVOKE INSERT ON employees FROM user1;

๐Ÿ’จ SQL ์ฃผ์š” ๊ฐœ๋…

โญ• ํ…Œ์ด๋ธ” (Table)

๐Ÿ’ก ํ‘œ ์ „์ฒด

โ— ๋ฐ์ดํ„ฐ๋Š” ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ์ €์žฅ

โ— ํ–‰(Row)๊ณผ ์—ด(Column)๋กœ ๊ตฌ์„ฑ

CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    position VARCHAR(100),
    salary DECIMAL(10, 2)
);

โญ• ์Šคํ‚ค๋งˆ (Schema)

๐Ÿ’ก ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ตฌ์กฐ์™€ ์ œ์•ฝ์กฐ๊ฑด์— ๊ด€ํ•ด ์ „๋ฐ˜์ ์ธ ๋ช…์„ธ

โ— ์Šคํ‚ค๋งˆ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด(ํ…Œ์ด๋ธ”, ๋ทฐ, ์ธ๋ฑ์Šค ๋“ฑ)์˜ ๋…ผ๋ฆฌ์  ๊ทธ๋ฃน์„ ์ •์˜

โ— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์—์„œ ๊ฐ์ฒด๋“ค์„ ์กฐ์งํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

โ— ๊ฐ ํ–‰์€ ๋ ˆ์ฝ”๋“œ(Record) ๋˜๋Š” ํŠœํ”Œ(Tuple)์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ๊ฐ ์—ด์€ ํ•„๋“œ(Field) ๋˜๋Š” ์†์„ฑ(Attribute)์ด๋ผ๊ณ  ๋ถ€๋ฆ„

โญ• ์ธ๋ฑ์Šค (Index)

โ— ํ…Œ์ด๋ธ”์˜ ํŠน์ • ์—ด์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

โ— ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์กฐํšŒ ์†๋„ โ†‘

CREATE INDEX idx_salary ON employees (salary);

โญ• ์ œ์•ฝ ์กฐ๊ฑด (Constraints)

โ— ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

โ— ์ฃผ์š” ์ œ์•ฝ ์กฐ๊ฑด

  • PRIMARY KEY: ์œ ์ผํ•˜๊ณ  NULL์ด ์•„๋‹Œ ์‹๋ณ„์ž
  • FOREIGN KEY: ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค์™€ ์—ฐ๊ฒฐ๋œ ์—ด
  • UNIQUE: ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๊ฐ’์„ ๊ฐ–๋Š” ์—ด
  • NOT NULL: NULL ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์—ด
  • CHECK: ์—ด์˜ ๊ฐ’์„ ํŠน์ • ์กฐ๊ฑด์— ๋งž๋„๋ก ์ œํ•œ
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    position VARCHAR(100),
    salary DECIMAL(10, 2) CHECK (salary > 0)
);

๐Ÿ’จ ํŒŒ์ผ ์‹œ์Šคํ…œ VS. DBMS

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