- ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ๋ฐฐ์์ผ ํ๋๊ฐ?
- ํ์ฌ์์ ๊ฐ์ฅ ์ค์ํ๊ฒ ์๊ฐํ๋๊ฒ ๋ญ๊น?
- ์ ํ๋ธ ์์ค์ฝ๋๋ฅผ ์ป์๋ค ์ณ๋ณด์
- SQL(Structured Query Language)
- ANSI SQL
- DBMS(Database Management System)
- MySQL ์ค์นํ๊ธฐ(PostgreSQL, MSSQL, Oracle, MariaDB, SQLite ๋ฑ๋ฑ)
- ๋ค์ด๋ก๋ ๋งํฌ
- 5๋ฒ์ 8๋ฒ์ ๋ค ๊ด์ฐฎ์
- ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ 2์ฐจ์ ๊ณต๊ฐ
- ์์ ์ด๋ผ๊ณ ์๊ฐํด๋ณด์(row, column)
- ํ ์ด๋ธ์ ์ ๊ทํ์ผ๋ก ๋ง๋๋ ๊ฑธ ์ ๊ทํ๋ผ๊ณ ํ๋ค.
- ์์ ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ์๋๋ค
- ์ฝ์ , ์์ , ์ญ์ ์ ๋ฌธ์ ๊ฐ ์๋ ์๊ฐํด๋ณด๊ธฐ
ํ ์ปฌ๋ผ์ ๊ฐ์ด ๋ ๊ฐ...?
์ด๋ฆ | ์ ํ๋ฒํธ |
---|---|
์ ๋ก์ด | 010-1234-5678 010-2345-6789 |
๊ฐ๋ก๋ก ๋ฐฐ์น..?
์ด๋ฆ | ์ ํ๋ฒํธ1 | ์ ํ๋ฒํธ2 |
---|---|---|
์ ๋ก์ด | 010-1234-5678 | 010-2345-6789 |
์ด๋ฆ | ์ ํ๋ฒํธ |
---|---|
์ ๋ก์ด | 010-1234-5678 |
์ ๋ก์ด | 010-2345-6789 |
- ๋์ค์ JOIN์ผ๋ก ํฉ์นจ
๊ด๊ณ ์๊ฐํ๊ธฐ
- 1:N(์ผ๋๋ค)
- 1:1(์ผ๋์ผ)
- M:N(๋ค๋๋ค)
- PK(๊ธฐ๋ณธํค, primary key)
- FK(์ธ๋ํค, foreign key)
- ๋ณตํฉํค(Composite Key, ๋ ๊ฐ ์ด์์ ํค๊ฐ ๊ธฐ๋ณธํค๊ฐ ๋๋ ๊ฒ)
๋๋ช ์ด์ธ์ด ์๊ฒจ๋ฒ๋ฆฌ๋ฉด...?
์์ด๋ | ์ด๋ฆ | ์ ํ๋ฒํธ |
---|---|---|
1 | ์ ๋ก์ด | 010-1234-5678 |
1 | ์ ๋ก์ด | 010-2345-6789 |
2 | ์ ๋ก์ด | 010-3456-7890 |
์์ด๋๋ ์ค๋ณต๋๋ฉด ์ ๋จ
์์ด๋(PK) | ์ด๋ฆ |
---|---|
1 | ์ ๋ก์ด |
2 | ์ ๋ก์ด |
์์ด๋(PK) | ์ฌ์์์ด๋(FK) | ์ ํ๋ฒํธ |
---|---|---|
1 | 1 | 010-1234-5678 |
2 | 1 | 010-2345-6789 |
3 | 2 | 010-3456-7890 |
- ๋ณตํฉํค ๋ชจ๋์ ์ข ์๋๋์ง ์ฒดํฌ
์ด๋ฆ | ์ธ์ด | ์ ํ๋ฒํธ |
---|---|---|
์ ๋ก์ด | JS | 010-1234-5678 |
์ ๋ก์ด | TS | 010-1234-5678 |
์ ๋ก์ด | JAVA | 010-1234-5678 |
- Key๊ฐ ์๋ ์ปฌ๋ผ๋ค์ ์ข ์๊ด๊ณ(A -> B, B -> C์ด๊ณ A -> C์ธ ๊ด๊ณ๊ฐ ์๋ค๋ฉด A -> B, B -> C ๋ ๊ฐ๋ก ๋ถ๋ฆฌ)
์์ด๋ | ์ด๋ฆ | ์์ | ์๋น์ค |
---|---|---|---|
1 | ์ ๋ก์ด | ๋ค | ๋ค์ด๋ฒ์นํฐ |
2 | ์์ด | ์นด | ์นด์นด์คํก |
3 | ํฌ์ด | ๋ฐฐ | ๋ฐฐ๋ฏผ1 |
- ์ ์์์ด ์๋๋ผ ์๋น์ค๊ฐ ํค?
์ด๋ฆ | ์์ | ์๋น์ค |
---|---|---|
์ ๋ก์ด | ๋ค | ๋ค์ด๋ฒ์นํฐ |
์์ด | ์นด | ์นด์นด์คํก |
ํฌ์ด | ๋ฐฐ | ๋ฐฐ๋ฏผ1 |
- JOIN, JOIN, JOIN
- ์ฝ์ , ์์ , ์ญ์ ๊ฐ ์ผ์ด๋์ง ์๋ ๊ฒฝ์ฐ
- ์๋น์ค ๋ฐ๋ผ ํ๋จ
DDL(Definition), DQL(Query), DML(Manipulation), DCL(Control), TCL(Transaction)
fk ์๋ ๊ฑฐ ๋จผ์ ๋ง๋ค๊ธฐ
CREATE TABLE `zerocho`.`role` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(10) NOT NULL,
`min_salary` INT UNSIGNED NOT NULL DEFAULT 2500,
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC) VISIBLE,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4;
ON UPDATE, ON DELETE ์ต์ ๋ค ๊ธฐ์ตํ๊ธฐ
CREATE TABLE `employee` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`email` varchar(100) NOT NULL,
`salary` int unsigned NOT NULL,
`team` varchar(20) NOT NULL,
`quit_date` date DEFAULT NULL,
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
`role_id` int unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email_UNIQUE` (`email`),
KEY `employee_role_fk_idx` (`role_id`),
CONSTRAINT `employee_role_fk` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='์ฌ์ํ
์ด๋ธ'
UPDATE employee SET salary = ? WHERE name = ?
INSERT INTO employee (name, salary, team) VALUES ("์์ด", 5000, "๊ฐ๋ฐ"), ("ํฌ์ด", 4000, "๋์์ธ")
- hard delete
DELETE FROM employee WHERE id = ?
- soft delete
UPDATE employee SET quit_date = NOW() WHERE id = ?
๋์ค์ ์กฐํ ์์๋
SELECT * FROM employee WHERE quit_date IS NULL
๊ฐ์ ธ์ค๊ธฐ ๋ฉ๋ด์์ ๋ค์ SQL ๋ณต๋ถ!
CREATE TABLE `role` (
`id` int NOT NULL COMMENT '์ง์ฑ
์์ด๋',
`name` varchar(10) NOT NULL COMMENT '์ง์ฑ
์ด๋ฆ',
`min_salary` int NOT NULL DEFAULT '2500' COMMENT '์ต์ ์ฐ๋ด'
);
CREATE TABLE `employee` (
`id` int NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(100) NOT NULL,
`salary` int NOT NULL,
`team` varchar(20) NOT NULL,
`quit_date` date NULL,
`created_at` datetime NULL,
`role_id` int NOT NULL COMMENT '์ง์ฑ
์์ด๋'
);
CREATE TABLE `employee_info` (
`id` int NOT NULL,
`address` varchar(100) NULL,
`etc` text NULL
);
ALTER TABLE `role` ADD CONSTRAINT `PK_ROLE` PRIMARY KEY (
`id`
);
ALTER TABLE `employee` ADD CONSTRAINT `PK_EMPLOYEE` PRIMARY KEY (
`id`
);
ALTER TABLE `employee_info` ADD CONSTRAINT `PK_EMPLOYEE_INFO` PRIMARY KEY (
`id`
);
ALTER TABLE `employee_info` ADD CONSTRAINT `FK_employee_TO_employee_info_1` FOREIGN KEY (
`id`
)
REFERENCES `employee` (
`id`
);
SELECT * FROM employee
SELECT * FROM employee WHERE name = ?
SELECT * FROM employee WHERE created_at BETWEEN '2023-05-01' AND '2023-06-30'
SELECT * FROM employee WHERE salary > 3000 OR team = '๊ธฐํํ'
SELECT id, name FROM employee WHERE name = ?
- AS
SELECT id as no, name FROM employee WHERE name = ?
SELECT id as no, name FROM employee WHERE name = ? ORDER BY salary
SELECT id as no, name FROM employee WHERE name = ? ORDER BY salary, role_id
SELECT id as no, name FROM employee WHERE name = ? ORDER BY salary LIMIT 10
SELECT id as no, name FROM employee WHERE name = ? ORDER BY salary DESC LIMIT 10 OFFSET 10
์ปค์ ๋ฐฉ์ vs OFFSET ๋ฐฉ์
SELECT id as no, name FROM employee WHERE name = ? AND id < 8 ORDER BY salary DESC LIMIT 10
SELECT AVG(salary) FROM employee WHERE name = ? GROUP BY team HAVING team = '๊ฐ๋ฐํ' OR team = '๋์์ธํ'
null ๋์ฌ ์ ์์
SELECT ... JOIN ... ON ...
null ๋์ฌ ์ ์์
SELECT ... LEFT JOIN ... ON ...
SELECT ... LEFT JOIN ... ON ...
UNION
SELECT ... RIGHT JOIN ... ON ...
SELECT ... FROM a JOIN b
SELECT ... FROM a CROSS JOIN b
SELECT ... FROM a, b
- (a, b, c) index
- a๋ฅผ ์ ์ฐ๋ฉด ๋ฌด์ฉ์ง๋ฌผ
CREATE INDEX
DROP INDEX
START TRANSACTION;
COMMIT;
ROLLBACK;
- Atomicity
- Consistency
- Isolation
- Durability
DELIMITER //
CREATE PROCEDURE increaseMinSalary(money INT, rid INT)
BEGIN
START TRANSACTION;
UPDATE zerocho.role SET min_salary = money WHERE id = rid;
UPDATE zerocho.employee SET salary = money WHERE role_id = rid AND salary < money;
COMMIT;
END //
DELIMITER ;
EXEC procedure_name;
DELIMITER //
CREATE TRIGGER updateSalaryWhenMinSalaryChange AFTER UPDATE ON zerocho.role
FOR EACH ROW
BEGIN
UPDATE zerocho.employee SET salary = NEW.min_salary WHERE role_id = NEW.id AND salary < NEW.min_salary;
END //
DELIMITER ;
SHOW TRIGGERS;
DROP TRIGGER updateSalaryWhenMinSalaryChange
CREATE OR REPLACE VIEW zerocho.ep_view
AS SELECT employee_id, project_id, role_id, e.name as employee_name, salary, team
FROM zerocho.employee e LEFT JOIN zerocho.role r ON e.role_id = r.id
LEFT JOIN zerocho.employee_project ep ON e.id = ep.employee_id;
MongoDB, Redis