CRUD : ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋์
1. CREATE
๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ง๋ค๊ณ ์ฌ์ฉํ๊ธฐ (CREATE, USE)
- a1์ด๋ผ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์์ผ๋ฉด ์ญ์
- a1์ด๋ผ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์์ฑ
- a1์ ์ฌ์ฉ
DROP DATABASE IF EXISTS a1
CREATE DATABASE a1
USE a1;
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ชฉ๋ก๊ณผ ํ ์ด๋ธ์ ๋ชฉ๋ก์ ๋ณด๊ธฐ (SHOW)
- DESC : ํ ์ด๋ธ์ ๊ตฌ์กฐ ํ์ธ
SHOW DATABASES;
SHOW TABLES;
DESC ํ
์ด๋ธ๋ช
;
dept(๋ถ์๋ช ) ํ ์ด๋ธ์ ์์ฑํ๊ธฐ (CREATE)
- id์ ๊ธฐ๋ณธ ํค๋ฅผ ๋ถ์ฌํ๊ณ ์ซ์๊ฐ ์๋์ผ๋ก ์ฆ๊ฐํจ
- ๋ชจ๋ ์ปฌ๋ผ์ NULL์ ํ์ฉํ์ง ์์
- name ์ปฌ๋ผ์ ์ค๋ณต์ ํ์ฉํ์ง ์์
CREATE TABLE dept(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
regDate DATETIME NOT NULL,
`name` CHAR(100) NOT NULL UNIQUE
);
dept ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํจ (INSERT INTO)
- reDate์ ์๊ฐ์ NOW()๋ก ์ค์
- NOW() : ํ์ฌ ์๊ฐ์ ๋ถ๋ฌ์ค๋ ํจ์
INSERT INTO dept
SET regDate = NOW(), `name` = 'ํ๋ณด';
INSERT INTO dept
SET regDate = NOW(), `name` = '๊ธฐํ';
emp ํ ์ด๋ธ์ ์์ฑํ๊ธฐ (CREATE)
CREATE TABLE emp(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
regDate DATETIME NOT NULL,
`name` CHAR(100) NOT NULL,
deptid INT UNSIGNED NOT NULL,
salary INT UNSIGNED NOT NULL
);
emp(์ฌ์์ ๋ณด) ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํจ (INSERT INTO)
- regDate(์ ์ฌ์ผ), name(์ฌ์์ด๋ฆ), deptid(๋ถ์๋ฒํธ), salary(์ฐ๋ด)
INSERT INTO emp
SET regDate = NOW(), `name` = 'ํ๊ธธ๋', deptid = 1, salary = 5000;
INSERT INTO emp
SET regDate = NOW(), `name` = 'ํ๊ธธ์', deptid = 1, salary = 6000;
INSERT INTO emp
SET regDate = NOW(), `name` = '์๊บฝ์ ', deptid = 2, salary = 4000;
2. READ
SELECT ๋ฌธ์ ๋ ผ๋ฆฌ์ ์ํ ์์
SELECT ----- 5๏ธโฃ
FROM ------ 1๏ธโฃ
WHERE ------ 2๏ธโฃ
GROUP BY --- 3๏ธโฃ
HAVING ------ 4๏ธโฃ
ORDER BY ---- 6๏ธโฃ
๋ถ์๋ณ๋ก ๋ถ์๋ช , ์ฌ์๋ฆฌ์คํธ, ํ๊ท -์ต๊ณ -์ต์ ์ฐ๋ด, ์ฌ์ ์ ์กฐํํ๊ธฐ
#v1 ์กฐ์ธ ์ฌ์ฉํ์ง ์๊ณ ์กฐํ
- IF๋ฌธ ์ฌ์ฉ
SELECT IF(deptId = 1, 'ํ๋ณด', '๊ธฐํ') AS `๋ถ์๋ช
`,
GROUP_CONCAT(`name` ORDER BY id DESC SEPARATOR ', ') AS `์ฌ์๋ฆฌ์คํธ`,
CONCAT(TRUNCATE(AVG(salary), 0), '๋ง์') AS `ํ๊ท ์ฐ๋ด`,
CONCAT(MAX(salary), '๋ง์') AS `์ต๊ณ ์ฐ๋ด`,
CONCAT(MIN(salary), '๋ง์') AS `์ต์์ฐ๋ด`,
CONCAT(COUNT(*), '๋ช
') AS `์ฌ์์`
FROM emp
GROUP BY deptid;
- CASE๋ฌธ ์ฌ์ฉ + ์ค๋ณต ์ ๊ฑฐ
SELECT CASE
WHEN deptid = 1 THEN 'ํ๋ณด'
WHEN deptid = 2 THEN '๊ธฐํ'
ELSE '๋ฌด์์' END AS `๋ถ์๋ช
`,
GROUP_CONCAT(`name` ORDER BY id DESC SEPARATOR ', ') AS `์ฌ์ ๋ฆฌ์คํธ`,
TRUNCATE(AVG(salary), 0) AS `ํ๊ท ์ฐ๋ด`, MAX(salary) AS `์ต๊ณ ์ฐ๋ด`, MIN(salary) AS `์ต์์ฐ๋ด`,
COUNT(*) AS `์ฌ์ ์`
FROM emp
GROUP BY deptid;
#v2 ์กฐ์ธํด์ ๋ถ์๋ช ๊น์ง ์กฐํ
SELECT D.name AS `๋ถ์`,
GROUP_CONCAT(E.name) AS `์ฌ์๋ฆฌ์คํธ`,
TRUNCATE(AVG(E.salary), 0) AS `ํ๊ท ์ฐ๋ด`, MAX(E.salary) AS `์ต๊ณ ์ฐ๋ด`, MIN(E.salary) AS `์ต์์ฐ๋ด`,
COUNT(*) AS `์ฌ์์`
FROM emp AS E INNER JOIN dept AS D ON E.deptid = D.id
GROUP BY E.deptid;
#v3 HAVING์ ์ฌ์ฉํ์ฌ ํ๊ท ์ฐ๋ด์ด 5000 ์ด์์ธ ๋ถ์๋ก ์ถ๋ ค์ ์กฐํ
SELECT D.name AS `๋ถ์`,
GROUP_CONCAT(E.name) AS `์ฌ์๋ฆฌ์คํธ`,
TRUNCATE(AVG(E.salary), 0) AS `ํ๊ท ์ฐ๋ด`, MAX(E.salary) AS `์ต๊ณ ์ฐ๋ด`, MIN(E.salary) AS `์ต์์ฐ๋ด`,
COUNT(*) AS ์ฌ์์
FROM emp AS E INNER JOIN dept AS D ON E.deptid = D.id
GROUP BY E.deptid
HAVING `ํ๊ท ์ฐ๋ด` >= 5000;
#v4 HAVING์ ๋์ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ํํ์ฌ ์กฐํ
SELECT *
FROM (
SELECT D.name AS `๋ถ์๋ช
`,
GROUP_CONCAT(E.`name`) AS `์ฌ์๋ฆฌ์คํธ`,
TRUNCATE(AVG(E.salary), 0) AS `ํ๊ท ์ฐ๋ด`, MAX(E.salary) AS `์ต๊ณ ์ฐ๋ด`, MIN(E.salary) AS `์ต์์ฐ๋ด`,
COUNT(*) AS `์ฌ์์`
FROM emp AS E INNER JOIN dept AS D ON E.deptid = D.id
WHERE 1
GROUP BY E.deptId
) AS D
WHERE D.`ํ๊ท ์ฐ๋ด` >= 5000;
3. UPDATE
๊ธฐ์กด ์ปฌ๋ผ์ ๊ฐ์ ์์ ํ๊ธฐ
emp ํ ์ด๋ธ์์ 'ํ๊ธธ๋'์ด๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ๋ฐ์ดํฐ์ ์ฐ๋ด์ 6000์ผ๋ก ์์
UPDATE emp
SET salary = 6000
WHERE `name` = 'ํ๊ธธ๋';
4. DELETE
ํน์ ์ปฌ๋ผ ๊ฐ์ ์ญ์ ํ๊ธฐ
emp ํ ์ด๋ธ์์ 'ํ๊ธธ๋'์ด๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ๋ฐ์ดํฐ๋ฅผ ์ญ์
DELETE FROM emp
WHERE `name` = 'ํ๊ธธ๋';