๐Ÿ—„๏ธ mysql ๐Ÿ—„๏ธ

๋ฐ•์ƒ์€ยท2021๋…„ 8์›” 26์ผ
0

๐Ÿ—ƒ๏ธ database ๐Ÿ—ƒ๏ธ

๋ชฉ๋ก ๋ณด๊ธฐ
1/2

  • []์ƒ๋žต๊ฐ€๋Šฅ
  • ๋Œ€๋ฌธ์ž => ๋ช…๋ น์–ด
  • <> => ๊ฐœ์ธ์ ์œผ๋กœ ์ž…๋ ฅํ• ๊ฑฐ
  • '' => ์‚ฌ์šฉํ•œ๊ณณ์€ ''์ƒ๋žตํ•˜์ง€๋ง๊ณ  ๊ทธ๋ƒฅ ''๋กœ ๊ฐ์‹ธ์„œ ์ ๊ธฐ

0. ํŒ

mysql์—์„œ๋Š” DB์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ์œ ์ €์—๊ฒŒ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋ผ์„œ
DB๋จผ์ € ์ƒ์„ฑ ํ›„ ์œ ์ € ์ƒ์„ฑํ•˜๊ณ  ๊ถŒํ•œ๋ถ€์—ฌํ•˜๋Š” ์ˆœ์„œ๋กœ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

0.1 time_zone

0.1.1 time_zone ๊ฒ€์ƒ‰

SHOW VARIABLES LIKE '%time_zone%';

0.1.2 time_zone ์ˆ˜์ •

SET time_zone = 'system';`... `'Asia/Seoul'

0.2 ์„ธ์…˜ ์•„์ด๋”” ๋ณด๊ธฐ

๊ธฐ๋ณธ์ ์œผ๋กœ DB์— ์ ‘์†ํ•˜๋ฉด DB์—์„œ ์„ธ์…˜์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•จ, ์ฆ‰ ์—ฐ๊ฒฐ ํ•˜๋‚˜๋‹น ์„ธ์…˜์ด ํ•˜๋‚˜์”ฉ ์ƒ๊ธด๋‹ค.

// ์„ธ์…˜ ์•„์ด๋”” ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅํ•˜๊ธฐ
SHOW processlist;

1. SQL

1.1 DDL ( Data Definition Language )

๋ฐ์ดํ„ฐ ์ •์˜ ์–ธ์–ด
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค or ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋‚˜ ๊ด€๊ณ„๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

  1. CREATE
  2. DROP
  3. TRUNCATE
  4. ALTER

1.2 DCL ( Data Control Language )

๋ฐ์ดํ„ฐ ์ œ์–ด ์–ธ์–ด
์œ ์ €์—๊ฒŒ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•  ๋•Œ ์‚ฌ์šฉ

  1. GRANT
  2. REVOKE

1.3 TCL (Transaction Control Language)

์ถ”ํ›„์— ๊ณต๋ถ€ ํ›„ ์ •๋ฆฌ

1.4 DML ( Data Manipulation Language )

๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด
ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์กฐํšŒ, ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ์— ์‚ฌ์šฉ

  1. SELECT
  2. INSERT
  3. UPDATE
  4. DELETE

2. DataBase ๊ด€๋ จ ๋ช…๋ น์–ด

2.1 DataBase ์ƒ์„ฑ + (๋ฌธ์ž์—ด์„ธํŒ…)

CREATE DATABASE <database-name> DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

๊ธฐ๋ณธ์ ์œผ๋กœ column๋“ค์€ table์„ค์ •์„ ๋”ฐ๋ผ๊ฐ€๊ณ  ํ…Œ์ด๋ธ”์€ DataBase์„ค์ •์„ ๋”ฐ๋ผ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์—
์• ์ดˆ์— DataBase์—์„œ ๋ฌธ์ž ์„ค์ •์„ ํ•ด์ฃผ๋ฉด table๊ณผ column์„ ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค

2.2 DataBase ์ œ๊ฑฐ

DROP DATABASE <database-name>;

3. User ๊ด€๋ จ ๋ช…๋ น์–ด

3.1 ์œ ์ € ์ƒ์„ฑ

// ํ˜•์‹.. host์ž๋ฆฌ์—” ํ˜ธ์ŠคํŠธ ์ ์–ด์ค˜์•ผํ•จ ( ex)'localhost', '%' )
CREATE user <user-name>[@'<host>' IDENTIFIED BY <db-password>];

// ์‚ฌ์šฉ
CREATE user testUser@'localhost' IDENTIFIED BY '1234';

testUser๋ž€ ์ด๋ฆ„์€ ๊ฐ€์ง„ ์œ ์ €๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ IP์€ localhost์ด๋ฉฐ, ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 1234์ž„

3.2 ์œ ์ € ์‚ญ์ œ

// ํ˜•์‹
drop user <user-name>@'<host>';

// ์‚ฌ์šฉ
DROP user testUser@'localhost';

3.3 ์œ ์ € ์กฐํšŒ

use mysql;
SELECT host, user FROM user;
  • host: ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ํด๋ผ์ด์–ธํŠธ์˜ IP๋ฅผ ์˜๋ฏธ ( %, localhost )
  • user: ํ•ด๋‹น ์œ ์ €๋ฅผ ์˜๋ฏธ

3.4 ์œ ์ € ๊ถŒํ•œ ๋ถ€์—ฌ

// ํ˜•์‹
GRANT ALL PRIVILEGES ON <db-name>.<table-name> TO <user-name>@'<host-name>' [IDENTIFIED BY '<password>'];

// ์‚ฌ์šฉ
GRANT ALL PRIVILEGES ON testDB.* TO 'testUser'@'localhost' [IDENTIFIED BY '๋น„๋ฐ€๋ฒˆํ˜ธ'];

localhost๋กœ ์ ‘๊ทผํ•œtestUser์—๊ฒŒ testDB๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ชจ๋“  ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ถŒํ•œ์„ ์คŒ

  • *์‚ฌ์šฉ์‹œ ๋ชจ๋“ ๊ฒƒ์— ๊ถŒํ•œ๋ถ€์—ฌ
  • ๊ฐ™์€ ์œ ์ €๋ผ๋„ host๋งˆ๋‹ค ๋‹ค๋ฅธ ๊ถŒํ•œ์„ ๋ถ€์—ฌ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

3.5 ์œ ์ € ๊ถŒํ•œ ์ ์šฉ

flush privileges;

3.6 ๊ถŒํ•œ ์‚ญ์ œ

// ํ˜•์‹
REVOKE ALL PRIVILEGES ON <db-name>.* FROM <user-name>@'<host-name>';

// ์‚ฌ์šฉ
REVOKE ALL PRIVILEGES ON testDB.* FROM testUser@'%';

3.7 ์œ ์ € ๊ถŒํ•œ ํ™•์ธ

// ํ˜•์‹
SHOW GRANTS FOR <user-name>[@'<host-name>'];

// ์‚ฌ์šฉ
SHOW GRANTS FOR testUser@'localhost';	// localhost๋กœ ์ ‘๊ทผํ–ˆ์„ ๋•Œ ๊ถŒํ•œ ๋ณด๊ธฐ
SHOW GRANTS FOR testUser;				// ๋ชจ๋“  ํ˜ธ์ŠคํŠธ์— ๋Œ€ํ•œ ๊ถŒํ•œ ๋ณด๊ธฐ

3.8 ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถ€์—ฌ ๋ฐ ๋ณ€๊ฒฝ ๋ฐ ํ™•์ธ

// ํ˜„์žฌ์•”ํ˜ธ ํ™•์ธ์ธ๋ฐ ์•”ํ˜ธํ™”๋˜์–ด์žˆ์–ด์„œ ํ•ด์„๋ถˆ๊ฐ€๋Šฅ...
SELECT host, user, authentication_string FROM user;

// ๋น„๋ฐ€๋ฒˆํ˜ธ๋ณ€๊ฒฝํ˜•์‹
ALTER user <user-name>@'<host-name>' IDENTIFIED WITH mysql_native_password BY '๋ณ€๊ฒฝํ• ๋น„๋ฐ€๋ฒˆํ˜ธ';

// ๋น„๋ฐ€๋ฒˆํ˜ธ๋ณ€๊ฒฝ์ธ๋ฐ ์•ˆ๋จ
ALTER user 'testUser'@'host' IDENTIFIED WITH mysql_native_password BY '12345';

4. ์ž๋ฃŒํ˜•

4.1 ์ˆซ์ž

4.2 ๋ฌธ์ž

VARCHAR()๋กœ ์ง€์ •ํ•˜๋ฉด ํฌ๊ธฐ์— ๋งž๊ฒŒ ์•Œ์•„์„œ TINYTEXTํ˜•์‹๋“ค ์ค‘์— ํ•˜๋‚˜๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

4.2.1 CHAR(n)

๊ณ ์ •ํ˜• ๋ฌธ์ž

  • nbyte

4.2.2 VARCHAR(n)

๊ฐ€๋ณ€ํ˜• ๋ฌธ์ž

  • nbyte

4.2.3 TINYTEXT

๊ฐ€๋ณ€ํ˜• ๋ฌธ์ž

  • ์ตœ๋Œ€ 256byte

4.2.4 TEXT

๊ฐ€๋ณ€ํ˜• ๋ฌธ์ž

  • ์ตœ๋Œ€ 65635byte

...๋‚˜๋จธ์ง€ ๋” ์žˆ์ง€๋งŒ ๋„ˆ๋ฌด ์ปค์„œ ์ƒ๋žต

4.3 ์‹œ๊ฐ„

4.3.1 DATETIME

4byte
์‹œ๊ฐ„ ๊ฐ’์ด ์ˆซ์ž๋กœ ๋ณ€ํ™˜๋ผ์„œ ๋“ค์–ด๊ฐ„๋‹ค. ( 1970/01/01 ์ดํ›„ )

4.3.2 TIMESTAMP

8byte
์ž…๋ ฅ๋œ ์‹œ๊ฐ„์—์„œ time_zone์— ์˜์กดํ•ด์„œ ์‹œ๊ฐ„์ด ๋งž์ถฐ์ง

4.3.3 ํ˜„์žฌ์‹œ๊ฐ„ / ์—…๋ฐ์ดํŠธ์‹œ๊ฐ„

DEFAULT CURRENT_TIMESTAMP [ON UPDATE CURRENT_TIMESTAMP]

์ƒ์„ฑ๋œ ํ˜„์žฌ์‹œ๊ฐ„์„ ํ…Œ์ด๋ธ”์— ๋„ฃ๊ณ 
ON UPDATE๋Š” ์—…๋ฐ์ดํŠธ ๋  ๋•Œ ๋งˆ๋‹ค ์‹œ๊ฐ„์„ ๋„ฃ์Œ

5. ํ…Œ์ด๋ธ” ๋ช…๋ น์–ด

5.1 ํ…Œ์ด๋ธ” ์ƒ์„ฑ

// ํ˜•์‹
CREATE TABLE ํ…Œ์ด๋ธ”๋ช… [IF NOT EXISTS] ( column์ •์˜ );

// ์‚ฌ์šฉ
CREATE TABLE testTable (
  _id INTEGER NOT NULL COMMENT '์‹๋ณ„์ž',
  name VARCHAR(20) NOT NULL COMMENT '์ด๋ฆ„',
  createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '์ƒ์„ฑ์‹œ๊ฐ„',
  updatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '์ˆ˜์ •์‹œ๊ฐ„',
  PRIMARY KEY (id),
  UNIQUE KEY uk_id_name (id, name)
)ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=utf8;

5.2 ํ…Œ์ด๋ธ” ํ˜•ํƒœ ๊ฒ€์ƒ‰

// ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋งŒ ๊ฒ€์ƒ‰ ํ˜•์‹
DESC <table-name>;

// ์‚ฌ์šฉ
DESC testTable;

// ํ…Œ์ด๋ธ” ์ •์˜ ๊ฒ€์ƒ‰
SHOW CREATE TABLE <table-name>;

// ์‚ฌ์šฉ
SHOW CREATE TABLE testTable;

5.3 ํ…Œ์ด๋ธ” ๋ฌธ์ž์—ด ์„ธํŒ…๊ฐ’ ๋ณ€๊ฒฝ

// ์˜ˆ์‹œ
ALTER TABLE `<database-name>`.`<table-name>` CHARACTER SET = <๋ฌธ์žํ˜•ํƒœ>, COLLATE = <๋ฌธ์žํ˜•ํƒœ>

// ์‚ฌ์šฉ
ALTER TABLE `testDB`.`testTable` CHARACTER SET = utf8, COLLATE = utf8_general_ci

5.4 ํ…Œ์ด๋ธ” ์ด๋ฆ„ ๋ณ€๊ฒฝ

// ํ˜•์‹
ALTER TABLE <current-table-name> RENAME <change-table-name>;

// ์‚ฌ์šฉ
ALTER TABLE testTable RENAME test;

5.5 ํ…Œ์ด๋ธ”์ •๋ณด๋ณด๊ธฐ

show table status;

5.6 ํ…Œ์ด๋ธ” ๋ฐฑ์—…

create table ๋ฐฑ์—…ํ…Œ์ด๋ธ”๋ช… like ๋ฐฑ์—…ํ• ํ…Œ์ด๋ธ”๋ช…;          // ( ๋ช…์„ธ๋งŒ ๋ณต์‚ฌ๋จ )
insert into ๋ฐฑ์—…ํ…Œ์ด๋ธ”๋ช… select * from ๋ฐฑ์—…ํ• ํ…Œ์ด๋ธ”๋ช…;  // ( ๊ฐ’๊นŒ์ง€ ๋ณต์‚ฌ )

6. ์ปฌ๋Ÿผ ๋ช…๋ น์–ด

// ํ˜•์‹
ALTER TABLE <table-name> [ADD | MODIFY | CHANGE | DROP] COLUMN column๋ช…

// ์‚ฌ์šฉ
ALTER TABLE testTable ADD COLUMN age VARCHAR(10);              // ์ถ”๊ฐ€
ALTER TABLE testTable MODIFY COLUMN age INTEGER;               // ์ˆ˜์ •
ALTER TABLE testTable CHANGE COLUMN age birth VARCHAR(40);     // ์ด๋ฆ„๊ณผ ์ปฌ๋Ÿผ๋‚ด์šฉ ์ˆ˜์ •
ALTER TABLE testTable DROP COLUMN birth;                       // ์ปฌ๋Ÿผ์‚ญ์ œ

๋‹จ ์ปฌ๋Ÿผ์„ ์ˆ˜์ •ํ•  ๋•Œ๋Š” ์—ฐ๊ด€๋˜์–ด์žˆ๋Š”์ง€, ์ž๋ฃŒํ˜•์ด ๋งž๋Š”์ง€, ๊ธฐ๋ณธ๊ฐ’์œ ๋ฌด์— ๋”ฐ๋ผ์„œ ํ™•์‹คํ•˜๊ฒŒ ์ˆ˜์ •ํ•ด์ค˜์•ผํ•จ

6.1 ์ปฌ๋Ÿผ ์ถ”๊ฐ€

// ํ˜•์‹
ALTER TABLE <table-name> ADD <column-name> [<data-type> <options>];

// ์‚ฌ์šฉ
ALTER TABLE testTable ADD COLUMN age VARCHAR(10) NOT NULL;

6.2 ์ปฌ๋Ÿผ ์‚ญ์ œ

// ํ˜•์‹
ALTER TABLE <table-name> DROP COLUMN <column-name>

// ์‚ฌ์šฉ
ALTER TABLE testTable DROP COLUMN birth;

6.3 ์ปฌ๋Ÿผ ํƒ€์ž… ๋ณ€๊ฒฝ

// ํ˜•์‹
ALTER TABLE <table-name> MODIFY <column-name> <type>;

// ์‚ฌ์šฉ
ALTER TABLE testTable MODIFY name VARCHAR(100);	// ๊ธฐ์กด ๋””ํดํŠธ๊ฐ’์€ ์‚ฌ๋ผ์ง

6.4 ์ปฌ๋Ÿผ ๋””ํดํŠธ๊ฐ’ ๋ณ€๊ฒฝ

// ํ˜•์‹
ALTER TABLE <table-name> ALTER COLUMN <column-name> set default <default-value>;

// ์‚ฌ์šฉ
ALTER TABLE testTable ALTER COLUMN name set default '๋ฌด๋ช…';

6.5 ์ปฌ๋Ÿผ ์œ„์น˜ ๋ณ€๊ฒฝ

// ํ˜•์‹
ALTER TABLE <table-name> MODIFY <column-name> <type> AFTER <front-column-name>;

// ์‚ฌ์šฉ
ALTER TABLE testTable MODIFY id integer AFTER name;

๊ธฐ์กด ํ…Œ์ด๋ธ” ์ˆœ์„œ๊ฐ€ id, name์ด๋ผ๋ฉด ์ดํ›„์— name, id์ˆœ์œผ๋กœ ๋ฐ”๋€œ

6.6 ์ปฌ๋Ÿผ ์ด๋ฆ„ ๋ณ€๊ฒฝ

// ํ˜•์‹
ALTER TABLE <table-name> CHANGE <๊ธฐ์กด-column-name> <๋ฐ”๊ฟ€-column-name> <type>;

// ์‚ฌ์šฉ
ALTER TABLE testTable CHANGE name fullname varchar(100);

6.7 ์ปฌ๋Ÿผ ์„ธํŒ…๊ฐ’ ๋ณ€๊ฒฝ

ALTER TABLE <table-name> [ENGINE | AUTO_INCREMENT | COMMENT | RENAME]
//                        ๊ธฐ๋ณธ์—”์ง„,     ์‹œ์ž‘์ˆซ์ž,       ์ฃผ์„,    ์ด๋ฆ„

7. ๋ฐ์ดํ„ฐ

7.1 ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€

// ํ˜•์‹
INSERT INTO <table-name>(<column-list>) VALUES(<data-list>);

// ์‚ฌ์šฉ
INSERT INTO testTable(_id, name) VALUES(1, 'john');

7.2 ๋ฐ์ดํ„ฐ ์ˆ˜์ •

// ํ˜•์‹
UPDATE <table-name> SET <change-data> WHERE <condition>;

// ์‚ฌ์šฉ
UPDATE testTable SET name = 'blue' WHERE _id = 1;

testTable์—์„œ _id๊ฐ€ 1์ธ ์ปฌ๋Ÿผ์˜ name์„ blue๋กœ ๋ณ€๊ฒฝํ•จ

7.3 ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰

# ํ˜•์‹
SELECT * FROM <table-name>;

# ์‚ฌ์šฉ
SELECT * FROM testTable;

7.4 ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ์ง€์šฐ๊ธฐ

# ๋ฐ์ดํ„ฐ๋งŒ ๋ชจ๋‘ or ์กฐ๊ฑด์œผ๋กœ ์ง€์›€
DELETE FROM TABLE <table-name> [WHERE <condition>]

# ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ด๋ธ”์˜ ์„ธํŒ…๊ฐ’์„ ๋ชจ๋‘ ์ดˆ๊ธฐํ™” ( auto_increment )
TRUNCATE TABLE <table-name>;

7.5 ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ ( update + select )

# update์™€ select๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉ
# ๋ฐ”๋กœ ์˜ˆ์‹œ User.name, Student.name์ด ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •
UPDATE User SET name = (SELECT name FROM Student WHERE id = 1) WHERE id = 1;

# 1๋ฒˆ Student์˜ name์„ 1๋ฒˆ User์˜ name์— ๋„ฃ์Œ

7.6 ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ ( insert + select )

# insert์™€ select๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉ
# ๋ฐ”๋กœ ์˜ˆ์‹œ User.name, Student.name์ด ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •
INSERT INTO User(name) SELECT name FROM WHERE id < 5;

# 1~5๋ฒˆ์˜ Student.name์„ User.name์— ๋„ฃ์Œ

8. ์กฐ๊ฑด

8.1 LIKE

# %๋Š” ์•ž๋’ค๋กœ ๋ช‡ ๊ธ€์ž๊ฐ€ ์™€๋„ ์ƒ๊ด€์—†์Œ
# _๋Š” ํ•œ ๊ธ€์ž๋งŒ ์ธ์ •ํ•จ
SELECT * FROM Student WHERE name [NOT] LIKE '%์ฃผ%';
SELECT * FROM Student WHERE name [NOT] LIKE '_์ฃผ_';

8.2 IN

# name์ด ์ฃผ๋ฏผ์„ฑ, ๋ฐฐ์ฃผ์„ฑ์ธ ์‚ฌ๋žŒ ์ฐพ๊ธฐ
SELECT * FROM Student WHERE name [NOT] IN ('์ฃผ๋ฏผ์„ฑ', '๋ฐฐ์ฃผ์„ฑ');

# (name = '์ฃผ๋ฏผ์„ฑ' or name = '๋ฐฐ์ฃผ์„ฑ' ๊ณผ ๊ฐ™์Œ)

8.3 BETWEEN

# id๊ฐ€ 10~20์ธ ์‚ฌ๋žŒ ์ฐพ๊ธฐ
SELECT * FROM Student WHRER id [NOT] BETWEEN 10 and 20;

# (id >= 10 and id <= 20 ๊ณผ ๊ฐ™์Œ)

8.4 DISTINCT

# ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค.
SELECT DISTINCT(addr) FROM Student;

# addr์ด ์ค‘๋ณต๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ „๋ถ€ ํ•˜๋‚˜๋กœ ํ‘œํ˜„ํ•จ

8.5 COUNT

# ์„œ์šธํŠน๋ณ„์‹œ์— ์‚ฌ๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋ช‡ ๋ช…์ธ์ง€ ๊ณ„์‚ฐ
SELECT COUNT(addr) FROM Student WHERE addr = '์„œ์šธ ํŠน๋ณ„์‹œ';

8.6 ORDER BY [RAND()]

# addr ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € ์ •๋ ฌํ•˜๊ณ  ์ดํ›„์— ๊ฐ™์€ addr์„ ๊ฐ€์ง„ ์‚ฌ๋žŒ์ด ์žˆ๋‹ค๋ฉด name์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
SELECT * FROM Student ORDER BY addr, name [DESC];

# ๊ธฐ์ค€ ์—†์ด ๋žœ๋ค์œผ๋กœ ์ •๋ ฌ
SELECT * FROM Student ORDER BY RAND();

8.7 LIMIT

# 0 ~ N๋ฒˆ์งธ ๊นŒ์ง€ ์ถœ๋ ฅ๋จ
SELECT * FROM Student LIMIT N;

# X๋ฒˆ์งธ๋ถ€ํ„ฐ Y๊ฐœ ์ถœ๋ ฅ๋จ
SELECT * FROM Student LIMIT X, Y;

8.8 GROUP BY

column์ด ๊ฐ™์€ ๊ฐ’๋ผ๋ฆฌ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

# addr์ด ๊ฐ™์€ ํ•™์ƒ๋ผ๋ฆฌ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋งŒ๋“ค๊ณ  count()ํ•จ์ˆ˜๋กœ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค.
SELECT addr AS '์ง€์—ญ', COUNT(addr) AS 'ํ•™์ƒ์ˆ˜' FROM Student GROUP BY addr;

# ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” addr์„ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์—ˆ์œผ๋ฏ€๋กœ addr์ž์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์€ ์ƒ๊ด€์—†์ง€๋งŒ,
# ๋‹ค๋ฅธ ์ปฌ๋Ÿผ์ธ id, name๊ฐ™์€ ๊ฒƒ์€ addr์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์—ฌ์žˆ์–ด์„œ ๊ฐ์ž๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.
# ์ฆ‰ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ๋ฌธ์€ ์—๋Ÿฌ๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค.
SELECT name FROM Student GROUP BY addr;

# ํ•˜์ง€๋งŒ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์€ ๊ฒƒ์— ํŠน์ˆ˜ํ•œ ์—ฐ์‚ฐ๋“ค์„ ํ•˜๋ฉด ์˜ค๋ฅ˜์—†์ด ์ถœ๋ ฅ์ด ๋œ๋‹ค.
SELECT AVG(id), COUNT(name) FROM Student GROUP BY addr;

# ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์ธ ๊ฒƒ๋“ค์˜ idํ‰๊ท ๊ณผ name์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ์‹์ด๋‹ค.

8.9 HAVING

GROUP BY์—์„œ ์กฐ๊ฑด์„ ๊ฑธ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

# addr์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ์นด์šดํŒ…ํ•œ ๊ฐ’์ด 25๊ฐœ ์ด์ƒ์ธ ๊ฒƒ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.
SELECT addr AS '์ง€์—ญ', COUNT(addr) AS cnt FROM Student GROUP BY addr HAVING cnt > 25 ORDER BY cnt DESC;

# cnt๋ฅผ ํ•œ๊ธ€๋กœ ์ž‘์„ฑํ•˜๋ฉด HAVING์ด๋‚˜ ORDER BY์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

8.10 case ~ when ~ then

if ~ else ๊ตฌ๋ฌธ๊ณผ ์œ ์‚ฌํ•จ

# addr์ด ๊ฒฝ๊ธฐ๋„๋ฉด 1, ๊ฒฝ์ƒ๋‚จ๋„๋ฉด 2, ๋‚˜๋จธ์ง€๋Š” 3
select distinct(addr),
	(case when addr like '๊ฒฝ๊ธฐ๋„%' then '1'
				when addr like '๊ฒฝ์ƒ๋‚จ๋„%' then '2'
                else '3' end)
	from Student;

# ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฐ’์„ ์กฐ๊ฑด ์—†์ด ๋ฐ”๋กœ ๋น„๊ตํ•  ๋•Œ๋Š” ์ฒซ ๋ฒˆ์งธ when๋ณด๋‹ค ์•ž์— ์‚ฌ์šฉํ•ด์„œ ๋’ค์— ๋ฐ˜๋ณต์ ์ธ ์‚ฌ์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
select distinct(addr),
	(case addr when '๊ฒฝ๊ธฐ๋„ ์šฉ์ธ์‹œ' then '1'
				when '๊ฒฝ์ƒ๋‚จ๋„ ์ง„์ฃผ์‹œ' then '2'
                else '3' end)
	from Student;

9. ์ถ”๊ฐ€ ์ •๋ณด

9.1 ๊ด€๊ณ„

9.1.1 ( 1 : 1 )

์˜ˆ๋ฅผ ๋“ค๋ฉด ๊ตญ๊ฐ€์™€ ๋Œ€ํ†ต๋ น์˜ ๊ด€๊ณ„๊ฐ€ 1:1 ๊ด€๊ณ„๋‹ค. ( ๋”ฑํžˆ ์˜ˆ์‹œ๊ฐ€ ๋– ์˜ค๋ฅด๋Š” ๊ฒŒ ์—†๋„ค์š”... )
๊ตญ๊ฐ€๋Š” ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๋Œ€ํ†ต๋ น์ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ๋Œ€ํ†ต๋ น๋„ ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๊ตญ๊ฐ€์— ์†ํ•ด์•ผ ํ•œ๋‹ค.

1:1 ๊ด€๊ณ„์—์„œ๋Š” ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ” ์ค‘์— ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์—๋งŒ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜๋Š” ํ‚ค๋ฅผ ๊ฐ€์ง€๋ฉด ๋œ๋‹ค.

9.1.2 ( 1 : N )

์˜ˆ๋ฅผ ๋“ค๋ฉด ์œ ์ €์™€ ๊ฒŒ์‹œ๊ธ€์˜ ๊ด€๊ณ„๊ฐ€ 1:N ๊ด€๊ณ„๋‹ค.
๊ณต๋™์ž‘์—…์ด ์—†๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์œ ์ €๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฒŒ์‹œ๊ธ€์€ ํ•˜๋‚˜์˜ ์œ ์ €์— ์ข…์†์ ์ด๋‹ค.

1:N ๊ด€๊ณ„์—์„œ๋Š” 1์—์„œ N์˜ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜๋Š” ํ‚ค๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.

9.1.3 ( N : M )

์˜ˆ๋ฅผ ๋“ค๋ฉด ์œ ์ €์™€ ๊ฒŒ์‹œ๊ธ€์˜ ์ข‹์•„์š” ๊ด€๊ณ„๊ฐ€ N:M ๊ด€๊ณ„๋‹ค.
์œ ์ €๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์— ์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๊ณ , ๊ฒŒ์‹œ๊ธ€๋„ ์—ฌ๋Ÿฌ ๋ช…์˜ ์œ ์ €์—๊ฒŒ ์ข‹์•„์š”๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

N:M ๊ด€๊ณ„์—์„œ๋Š” ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”์ด ํ•„์—ฐ์ ์œผ๋กœ ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค.
๋งŒ์•ฝ ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”์ด ์—†๋‹ค๋ฉด ๊ฐ ํ…Œ์ด๋ธ”์— ๊ฐ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜๋Š” ํ‚ค๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋Š”๋ฐ ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ฐธ์กฐ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ์—๋Š” ํ•œ ์ปฌ๋Ÿผ์— ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ๋„ฃ๊ฑฐ๋‚˜ ์ค‘๋ณต๋˜๋Š” ์ปฌ๋Ÿผ์„ ์ƒ์„ฑํ•ด์•ผ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
( ์œ ์ €๊ฐ€ ๋‘ ๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์— ์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅผ ๊ฒฝ์šฐ ์œ ์ €์˜ ๊ฒŒ์‹œ๊ธ€์•„์ด๋””์ปฌ๋Ÿผ์— ๋‘ ๊ฐœ์˜ ์ •๋ณด๋ฅผ ๋„ฃ๊ฑฐ๋‚˜, ๊ฐ™์€ ์œ ์ €๋ฅผ ๋‘๊ฐœ์˜ ์ปฌ๋Ÿผ์œผ๋กœ ๋‚˜๋ˆ ์„œ ๊ฐ๊ฐ ๊ฒŒ์‹œ๊ธ€ ์•„์ด๋””๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค. DB์˜ ๊ทœ์น™์„ ์–ด๊ธฐ๊ฑฐ๋‚˜, ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฐ˜๋ณต์ ์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ํ˜•ํƒœ๊ฐ€ ๋œ๋‹ค. )

์ด๋Ÿฐ ๋ถˆํ•„์š”ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  ํ•ด๋‹น ํ…Œ์ด๋ธ”์— ๊ฐ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜๋Š” ํ‚ค๋ฅผ ๋งตํ•‘ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

9.2 index

์ธ๋ฑ์Šค๊ฐ’์€ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค๋‘๋Š” ๊ฐ’์ด๋‹ค. ( ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์žˆ์œผ๋ฏ€๋กœ ๊ฒ€์ƒ‰์†๋„โ†‘ )

mysql ๊ธฐ์ค€์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€ ๋‹จ์œ„๋กœ ์ €์žฅํ•œ๋‹ค.
ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€์˜ ํฌ๊ธฐ๋Š” 16KB(16 * 1024BYTE)์ด๋‹ค.

ํŽ˜์ด์ง€์— ์ €์žฅ๋œ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌ, ํŠน์ • ๊ฐ’๋งŒ ๊ฒ€์ƒ‰ํ•˜๋ฏ€๋กœ ๋” ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ์— ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ SELECT๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ํ…Œ์ด๋ธ”์„ full scanํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ํšจ์œจ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๋‹ค๊ณ  ์ธ๋ฑ์Šค๋ฅผ ๋‚จ๋ฐœํ•˜๋ฉด ์ธ๋ฑ์Šค ํŽ˜์ด์ง€์—์„œ ๊ฒ€์ƒ‰ํ•  ์–‘์ด ๋Š˜์–ด๋‚˜๋ฏ€๋กœ ๋น„ํšจ์œจ์ ์ธ ์—ฐ์‚ฐ์ด ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ ๋‹นํ•˜๊ฒŒ ์กฐ์ ˆํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

  • primary key, foregin key, unique key๋Š” ์ž๋™์ ์œผ๋กœ ์ธ๋ฑ์Šค๋กœ ๋“ฑ๋ก๋œ๋‹ค.

9.2.1 index ๊ฒ€์ƒ‰

SHOW INDEX FROM <table-name>;

9.2.2 index ์ƒ์„ฑ

ALTER TABLE <table-name> ADD INDEX <index-name> (<column-name> <์ •๋ ฌ๋ฐฉ๋ฒ•(ASC, DESC)>);

9.2.3 index ์‚ญ์ œ

ALTER TABLE <table-name> DROP INDEX <index-name>;

9.2.4 ํ…Œ์ด๋ธ”์˜ ํ‚ค ๊ฒ€์ƒ‰

select * from information_schema.table_constraints where table_name = '<table-name>';

9.3 FOREIGN KEY

๋‹ค๋ฅธ ํ…Œ์ด๋ธ”๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

  • ์˜ˆ์‹œ
    ์˜ˆ์‹œ๋Š” author.id์™€ book.author_id์˜ ๊ด€๊ณ„๋ฅผ ์„ค์ •
    book.author_id๊ฐ€ author.id๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

9.3.1 ํ…Œ์ด๋ธ” ์ •์˜ ์‹œ ์„ ์–ธ

# ํ˜•์‹
[CONSTRAINT] [<index-name>] FOREIGN KEY (<์ฐธ์กฐํ• -column-name>) REFERENCES <์ฐธ์กฐ๋‹นํ• -table-name>(<์ฐธ์กฐ๋‹นํ• -column-name>) [ON (DELETE | UPDATE) (RESTRICT | CASCADE | SET NULL | NOACTION | SET DEFAULT)];

# ์‚ฌ์šฉ
# book์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ... book์ด author๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํ˜•์‹์ด๋ผ์„œ
# author๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ  ์ดํ›„์— book์„ ๋งŒ๋“ค ๋•Œ foreign key๋ฅผ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.
CONSTRAINT fk_author_book FOREIGN KEY (author_id) REFERENCES author(id) ON DELETE CASCADE;

9.3.2 ํ…Œ์ด๋ธ” ์ •์˜ ํ›„ ์„ ์–ธ

# ํ˜•์‹
ALTER TABLE <์ฐธ์กฐํ• -table-name> ADD CONSTRAINT [<index-name>] FOREIGN KEY(<์ฐธ์กฐํ• -column--name>) REFERENCES <์ฐธ์กฐ๋‹นํ• -table-name>(<์ฐธ์กฐ๋‹นํ• -column-name>) [ON (DELETE | UPDATE) (RESTRICT | CASCADE | SET NULL | NOACTION | SET DEFAULT)];

# ์‚ฌ์šฉ
ALTER TABLE book ADD CONSTRAINT pk_author_book FOREIGN KEY (author_id) REFERENCES author(id) ON DELETE CASCADE;

๊ธฐ๋ณธ์ ์œผ๋กœ ON DELETE๋ฅผ ์ž์ฃผ์‚ฌ์šฉํ•˜๋ฏ€๋กœ DELETE๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•จ
1. RESTRICT : ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ฐธ์กฐ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋ ๊ฒฝ์šฐ ์—๋Ÿฌํ‘œ์‹œ
2. CASCADE : ์ฐธ์กฐ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋ ๊ฒฝ์šฐ ๊ฐ™์ด ์‚ญ์ œ
4. SET NULL : ์ฐธ์กฐ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋ ๊ฒฝ์šฐ NULL๋กœ ๋Œ€์ฒด
5. NO ACTION : ์ฐธ์กฐ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋ ๊ฒฝ์šฐ ๊ทธ๋ƒฅ ๊ทธ๋Œ€๋กœ ๋†”๋‘ 
6. SET DEFAULT : ์ฐธ์กฐ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋ ๊ฒฝ์šฐ DEFAULT๋กœ ๋Œ€์ฒด

10. JOIN

์‹๋ณ„์ž๊ฐ’์„ ์ด์šฉํ•ด์„œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”๊ณผ ํ•ฉ์ณ์ง„ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด

  • ์˜ˆ์‹œ๋กœ ์‚ฌ์šฉํ•  ํ…Œ์ด๋ธ” ( book.authorId -> author.id์˜ foreign key )
bookํ…Œ์ด๋ธ”authorํ…Œ์ด๋ธ”
idnameauthorIdidnameage
1๋ฐ˜์ง€์˜ ์ œ์™•11J.R.R. ํ†จํ‚จ21
2ํ•ด๋ฆฌํฌํ„ฐ22JK ๋กค๋ง33
3ํ˜ธ๋น—13์•„๊ฐ€์‚ฌ ํฌ๋ฆฌ์Šคํ‹ฐ31
4๋„ˆ์˜ ์ด๋ฆ„์€.45ํžˆ๊ฐ€์‹œ๋…ธ ๊ฒŒ์ด๊ณ 55

10.1 INNER JOIN

๋‘˜ ๋‹ค ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ๊ฒฐ๊ด๋งŒ ๋ณด์•„์„œ ๋ณด์—ฌ์ฃผ๋Š” ๋ช…๋ น์–ด๋‹ค.

# ํ˜•ํƒœ
SELECT * FROM <table-name> JOIN <table-name> on <condition>;

# ์˜ˆ์‹œ
SELECT a.name as '์ €์ž๋ช…', b.name as '๋„์„œ๋ช…' FROM author AS a [INNER] JOIN book AS b on a.id = b.authorId;
  • ๊ฒฐ๊ณผ
์ €์ž๋ช…๋„์„œ๋ช…
J.R.R. ํ†จํ‚จ๋ฐ˜์ง€์˜ ์ œ์™•
J.R.R. ํ†จํ‚จํ˜ธ๋น—
JK ๋กค๋งํ•ด๋ฆฌํฌํ„ฐ

10.2 LFET [OUTER] JOIN

์ขŒ์ธก์— ์ ์€ ๊ฐ’ ์ฆ‰, ๋จผ์ € ์ ์€ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  ๊ฐ’์„ ํ•ฉ์ณ์„œ ๋ณด์—ฌ์ฃผ๊ณ  ์—†์œผ๋ฉด ๋งค์นญ๋˜๋Š” ์ƒ๋Œ€๊ฐ€ ์—†์œผ๋ฉด NULL๋กœ ์ฑ„์›Œ์„œ ๋ณด์—ฌ์ค€๋‹ค.

# ํ˜•ํƒœ
SELECT * FROM <table-name> LEFT [OUTER] JOIN <table-name> on <condition>;

# ์˜ˆ์‹œ
SELECT a.name as '์ €์ž๋ช…', b.name as '๋„์„œ๋ช…' FROM author AS a LEFT JOIN book AS b on a.id = b.authorId;
  • ๊ฒฐ๊ณผ
์ €์ž๋ช…๋„์„œ๋ช…
J.R.R. ํ†จํ‚จ๋ฐ˜์ง€์˜ ์ œ์™•
J.R.R. ํ†จํ‚จํ˜ธ๋น—
JK ๋กค๋งํ•ด๋ฆฌํฌํ„ฐ
์•„๊ฐ€์‚ฌ ํฌ๋ฆฌ์Šคํ‹ฐNULL
ํžˆ๊ฐ€์‹œ๋…ธ ๊ฒŒ์ด๊ณ NULL

10.3 RIGHT [OUTER] JOIN

LEFT JOIN์—์„œ ๊ธฐ์ค€์ ๋งŒ ๋ฐ”๋€ ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค.

# ํ˜•ํƒœ
SELECT * FROM <table-name> RIGHT [OUTER] JOIN <table-name> on <condition>;

# ์˜ˆ์‹œ
SELECT a.name as '์ €์ž๋ช…', b.name as '๋„์„œ๋ช…' FROM author AS a RIGHT JOIN book AS b on a.id = b.authorId;
  • ๊ฒฐ๊ณผ
์ €์ž๋ช…๋„์„œ๋ช…
J.R.R. ํ†จํ‚จ๋ฐ˜์ง€์˜ ์ œ์™•
J.R.R. ํ†จํ‚จํ˜ธ๋น—
JK ๋กค๋งํ•ด๋ฆฌํฌํ„ฐ
NULL๋„ˆ์˜ ์ด๋ฆ„์€.

11. method

sum, avg, mod, count, max, min, stdev, var_samp, cast, convert, now, str_to_date, concat, concat_ws, group_concat, if

12. ํŠธ๋žœ์ ์…˜

๋ฏผ๊ฐํ•˜๊ฑฐ๋‚˜ ์ค‘์š”ํ•œ ์ •๋ณด๋ฅผ ๋‹ค๋ฃฐ ๊ฒฝ์šฐ์— ์„ธ์ด๋ธŒํฌ์ธํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•

# auto commit ์ž„์‹œ์ ์œผ๋กœ ์ทจ์†Œ
start TRANSACTION;

# ๊ฐ์ข… ์ž‘์—… ( ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต ์ฒ˜๋ฆฌ )

# ์ •์ƒ์ ์œผ๋กœ ๋๋‚ฌ์„ ๊ฒฝ์šฐ ์ ์šฉ
COMMIT;

# ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์ตœ์‹  ์ปค๋ฐ‹์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ
ROLLBACK;

๋งˆ๋ฌด๋ฆฌ

์ถ”๊ฐ€๋กœ ๊ณต๋ถ€ํ•˜๋ฉด ๊ณ„์† ์ถ”๊ฐ€ํ•  ์˜ˆ์ •

์ฐธ๊ณ ํ•œ์‚ฌ์ดํŠธ

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