Skip to content

ZeroCho/cs-database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

20 Commits
ย 
ย 

Repository files navigation

Database(DB)

  1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์™œ ๋ฐฐ์›Œ์•ผ ํ•˜๋Š”๊ฐ€?
  • ํšŒ์‚ฌ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š”๊ฒŒ ๋ญ˜๊นŒ?
  • ์œ ํŠœ๋ธŒ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์–ป์—ˆ๋‹ค ์ณ๋ณด์ž
  1. SQL(Structured Query Language)
  • ANSI SQL
  1. DBMS(Database Management System)

ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” 2์ฐจ์› ๊ณต๊ฐ„
  • ์—‘์…€์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž(row, column)

์ •๊ทœํ™”(Normalization)

  • ํ…Œ์ด๋ธ”์„ ์ •๊ทœํ˜•์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฑธ ์ •๊ทœํ™”๋ผ๊ณ  ํ•œ๋‹ค.
  • ์—‘์…€์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์•ˆ๋œ๋‹ค
  • ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ ์‹œ ๋ฌธ์ œ๊ฐ€ ์—†๋‚˜ ์ƒ๊ฐํ•ด๋ณด๊ธฐ

1NF

ํ•œ ์ปฌ๋Ÿผ์— ๊ฐ’์ด ๋‘ ๊ฐœ...?

์ด๋ฆ„ ์ „ํ™”๋ฒˆํ˜ธ
์ œ๋กœ์ดˆ 010-1234-5678
010-2345-6789

๊ฐ€๋กœ๋กœ ๋ฐฐ์น˜..?
์ด๋ฆ„ ์ „ํ™”๋ฒˆํ˜ธ1 ์ „ํ™”๋ฒˆํ˜ธ2
์ œ๋กœ์ดˆ 010-1234-5678 010-2345-6789

์ด๋ฆ„ ์ „ํ™”๋ฒˆํ˜ธ
์ œ๋กœ์ดˆ 010-1234-5678
์ œ๋กœ์ดˆ 010-2345-6789
  • ๋‚˜์ค‘์— JOIN์œผ๋กœ ํ•ฉ์นจ

ERD

๊ด€๊ณ„ ์ƒ๊ฐํ•˜๊ธฐ

  1. 1:N(์ผ๋Œ€๋‹ค)
  2. 1:1(์ผ๋Œ€์ผ)
  3. M:N(๋‹ค๋Œ€๋‹ค)

์‹ค์ „๋งํฌ

Key

  • 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

2NF

  • ๋ณตํ•ฉํ‚ค ๋ชจ๋‘์— ์ข…์†๋˜๋Š”์ง€ ์ฒดํฌ
์ด๋ฆ„ ์–ธ์–ด ์ „ํ™”๋ฒˆํ˜ธ
์ œ๋กœ์ดˆ JS 010-1234-5678
์ œ๋กœ์ดˆ TS 010-1234-5678
์ œ๋กœ์ดˆ JAVA 010-1234-5678

3NF

  • Key๊ฐ€ ์•„๋‹Œ ์ปฌ๋Ÿผ๋“ค์˜ ์ข…์†๊ด€๊ณ„(A -> B, B -> C์ด๊ณ  A -> C์ธ ๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๋ฉด A -> B, B -> C ๋‘ ๊ฐœ๋กœ ๋ถ„๋ฆฌ)
์•„์ด๋”” ์ด๋ฆ„ ์†Œ์† ์„œ๋น„์Šค
1 ์ œ๋กœ์ดˆ ๋„ค ๋„ค์ด๋ฒ„์›นํˆฐ
2 ์›์ดˆ ์นด ์นด์นด์˜คํ†ก
3 ํˆฌ์ดˆ ๋ฐฐ ๋ฐฐ๋ฏผ1
  • ์™œ ์†Œ์†์ด ์•„๋‹ˆ๋ผ ์„œ๋น„์Šค๊ฐ€ ํ‚ค?

์ด๋ฆ„ ์†Œ์† ์„œ๋น„์Šค
์ œ๋กœ์ดˆ ๋„ค ๋„ค์ด๋ฒ„์›นํˆฐ
์›์ดˆ ์นด ์นด์นด์˜คํ†ก
ํˆฌ์ดˆ ๋ฐฐ ๋ฐฐ๋ฏผ1

์—ญ์ •๊ทœํ™”(Denormalization)

  • JOIN, JOIN, JOIN
  • ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
  • ์„œ๋น„์Šค ๋”ฐ๋ผ ํŒ๋‹จ

CREATE TABLE

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='์‚ฌ์›ํ…Œ์ด๋ธ”'

ALTER, DROP, TRUNCATE

UPDATE

UPDATE employee SET salary = ? WHERE name = ?

INSERT INTO

INSERT INTO employee (name, salary, team) VALUES ("์›์ดˆ", 5000, "๊ฐœ๋ฐœ"), ("ํˆฌ์ดˆ", 4000, "๋””์ž์ธ")

DELETE

  • 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

ERDCloud

๊ฐ€์ ธ์˜ค๊ธฐ ๋ฉ”๋‰ด์—์„œ ๋‹ค์Œ 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

SELECT * FROM employee

where

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 = '๊ธฐํšํŒ€'

projection

SELECT id, name FROM employee WHERE name = ?
  • AS
SELECT id as no, name FROM employee WHERE name = ?

order

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

limit

SELECT id as no, name FROM employee WHERE name = ? ORDER BY salary LIMIT 10

offset

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

group by, having

SELECT AVG(salary) FROM employee WHERE name = ? GROUP BY team HAVING team = '๊ฐœ๋ฐœํŒ€' OR team = '๋””์ž์ธํŒ€'

count, avg, min, max, sum

JOIN

INNER JOIN

null ๋‚˜์˜ฌ ์ˆ˜ ์—†์Œ

SELECT ... JOIN ... ON ...

LEFT/RIGHT JOIN

null ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Œ

SELECT ... LEFT JOIN ... ON ...

FULL OUTER JOIN

SELECT ... LEFT JOIN ... ON ...
UNION
SELECT ... RIGHT JOIN ... ON ...

CROSS JOIN

SELECT ... FROM a JOIN b
SELECT ... FROM a CROSS JOIN b
SELECT ... FROM a, b

INDEX

  • (a, b, c) index
  • a๋ฅผ ์•ˆ ์“ฐ๋ฉด ๋ฌด์šฉ์ง€๋ฌผ
CREATE INDEX
DROP INDEX

TRANSACTION

START TRANSACTION;

COMMIT;
ROLLBACK;

ACID

  • Atomicity
  • Consistency
  • Isolation
  • Durability

LOCK

Stored Procedure

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;

Trigger

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

View

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;

ETC

NoSQL

MongoDB, Redis

replication

sharding

CAP theorem

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published