MYSQL ๋ช…๋ น์–ด

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค CRUD

  1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
    mysql > CREATE DATABASE dbname;

  2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชฉ๋ก ๋ณด๊ธฐ
    mysql > SHOW DATABASES;

  3. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉํ•˜๊ธฐ
    mysql > USE dbname;

  4. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ญ์ œํ•˜๊ธฐ
    mysql > DROP DATABASE [IF EXISTS] dbname;

ํ…Œ์ด๋ธ” CRUD

  1. ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    mysql > CREATE TABLE tablename();
    ex) CREATE TABLE mytable(
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR,
    PRIMARY KEY(id)
    ); // ์†Œ๋ฌธ์ž๋Š” ์ž„์˜์ง€์ •(column), ๋Œ€๋ฌธ์ž๋Š” ๋ช…๋ น์–ด

  2. ํ…Œ์ด๋ธ” ๋ณด๊ธฐ
    mysql > SHOW TABLES;

  3. ํ…Œ์ด๋ธ” ๋ชฉ๋ก ๋ณด๊ธฐ
    mysql > DESC tablename;

  4. ํ…Œ์ด๋ธ” ์‚ฌ์šฉํ•˜๊ธฐ
    mysql > Use tablename;

  5. ํ…Œ์ด๋ธ” ์‚ญ์ œํ•˜๊ธฐ
    mysql > DROP TABLE [IF EXISTS] tablename;

  6. ํ…Œ์ด๋ธ” ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ ์ถ”๊ฐ€
    mysql > ALTER TABLE tablename ADD COLUMN [์ถ”๊ฐ€ํ•  ์ปฌ๋Ÿผ] [์ถ”๊ฐ€ํ•  ์ปฌ๋Ÿผ ๋ฐ์ดํ„ฐํƒ€์ž…];
    ex) ALTER TABLE mytable ADD COLUMN model_type varchar(10) NOT NULL;

  7. ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€๊ฒฝ
    mysql > ALTER TABLE tablename MODIFY COLUMN [๋ณ€๊ฒฝํ•  ์ปฌ๋Ÿผ๋ช…] [๋ณ€๊ฒฝํ•  ์ปฌ๋Ÿผ ๋ฐ์ดํ„ฐํƒ€์ž…];

  8. ํ…Œ์ด๋ธ” ์ด๋ฆ„ / ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€๊ฒฝ
    mysql > ALTER TABLE tablename CHANGE COLUMN [๊ธฐ์กด์˜ ์ปฌ๋Ÿผ๋ช…] [๋ณ€๊ฒฝํ•  ์ปฌ๋Ÿผ๋ช…] [๋ณ€๊ฒฝํ•  ์ปฌ๋Ÿผ ๋ฐ์ดํ„ฐํƒ€์ž…];

  9. ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ ์‚ญ์ œ
    mysql > ALTER COLUMN tablename DROP COLUMN [๊ธฐ์กด์˜ ์ปฌ๋Ÿผ๋ช…] [์‚ญ์ œํ•  ์ปฌ๋Ÿผ๋ช…];

๋ฐ์ดํ„ฐ CRUD

  1. ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
    1. mysql > INSERT INTO tablename VALES(123, '๊ฐ’1' ...); // ์ „์ฒด ์ปฌ๋Ÿผ
    2. mysql > INSERT INTO tablename (column1, column2 ...) VALUES(123, '๊ฐ’1' ...); // ๋Œ€์‘ ์ปฌ๋Ÿผ
  2. ๋ฐ์ดํ„ฐ ์กฐํšŒ
    1. mysql > SELECT * FROM tablename; // ์ „์ฒด ๋ฐ์ดํ„ฐ
    2. mysql > SELECT column1, column2 ... FROM tablename; // ๋Œ€์‘ ์ปฌ๋Ÿผ
  3. ๋ฐ์ดํ„ฐ ์กฐํšŒ ์‹œ ํ‘œ์‹œ๋˜๋Š” ์ปฌ๋Ÿผ๋ช… ๋ณ€๊ฒฝ mysql > SELECT beforename1 AS aftername1, beforename2 AS aftername2 From tablename;

  4. ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•ด์„œ ์กฐํšŒ
    DESC๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ, ASC๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ
    1. mysql > SELECT * FROM tablename ORDER BY column DESC// column์€ ์ •๋ ฌ๊ธฐ์ค€
    2. mysql > SELECT column1, column2 ... FROM tablename ORDER BY column ASC // column์€ ์ •๋ ฌ๊ธฐ์ค€
  5. OR๊ณผ AND ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋กœ ๋ฐ์ดํ„ฐ ์กฐํšŒ
    mysql > SELECT * FROM tablename WHERE column1 < '๊ฐ’' OR column2 > 2 OR colum3 = 3;

  6. LIKE๋กœ ๋ถ€๋ถ„์ผ์น˜(%, _ ์—ฐ์‚ฐ์ž) ๋ฐ์ดํ„ฐ ์กฐํšŒ
    1. mysql > SELECT * FROM tablename WHERE column LIKE '%๊ฐ’%';
    2. mysql > SELECT * FROM tablename WHERE column LIKE '๊ฐ’__'// ๋’ค์— ๋‘๊ธ€์ž ๋ถ™์„ ๊ฒฝ์šฐ
  7. LIMIT๋กœ ๋ฐ์ดํ„ฐ ์ˆ˜ ์ œํ•œํ•ด์„œ ๋ฐ์ดํ„ฐ ์กฐํšŒ
    1. mysql > SELECT * FROM tablename LIMIT 10; // ์ฒ˜์Œ๋ถ€ํ„ฐ 10๊ฐœ ๋ฐ์ดํ„ฐ ์กฐํšŒ
    2. mysql > SELECT * FROM tablename 10 LIMIT 20; // 10๋ถ€ํ„ฐ 20๊ฐœ ๋ฐ์ดํ„ฐ ์กฐํšŒ
  8. ์กฐ๊ฑด ์กฐํ•ฉํ•ด์„œ ๋ฐ์ดํ„ฐ ์กฐํšŒ
    ์กฐํ•ฉ์ˆœ์„œ SELECT FROM WHERE ORDER BY LIMIT mysql > SELECT column1, column2 FROM tablename WEHERE column1 < 2 AND column2 LIKE %๊ฐ’% ORDER BY column1 DESC LIMIT 10;

  9. ๋ฐ์ดํ„ฐ ์ˆ˜์ •
    mysql > UPDATE tablename SET ์ˆ˜์ •ํ•˜๊ณ ์‹ถ์€ columnname1 = '์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’1', ... WHERE column = '๊ฐ’';
    ex) โ€”-+โ€”โ€”+โ€”โ€”โ€”โ€“+โ€”โ€”โ€”โ€”+ | id | name | model_num | model_type |

    โ€”-+โ€”โ€”+โ€”โ€”โ€”โ€“+โ€”โ€”โ€”โ€”+ 3 i7 7700 Kaby Lake

    โ€”-+โ€”โ€”+โ€”โ€”โ€”โ€“+โ€”โ€”โ€”โ€”+ mysql> UPDATE mytable SET name = โ€˜i5โ€™, model_num = โ€˜5500โ€™ WHERE id = 3; mysql> SELECT * FROM mytable;

    โ€”-+โ€”โ€”+โ€”โ€”โ€”โ€“+โ€”โ€”โ€”โ€”+ id name model_num model_type
    โ€”-+โ€”โ€”+โ€”โ€”โ€”โ€“+โ€”โ€”โ€”โ€”+ 3 i5 5500 Kaby Lake

    โ€”-+โ€”โ€”+โ€”โ€”โ€”โ€“+โ€”โ€”โ€”โ€”+

  10. ๋ฐ์ดํ„ฐ ์‚ญ์ œ
    1. mysql > DELETE FROM tablename WHERE ํŠน์ • column = '๊ฐ’';
    2. mysql > DELETE FROM tablename;