MYSQL ๋ช ๋ น์ด
MYSQL ๋ช ๋ น์ด ์ ๋ฆฌ
MYSQL ๋ช ๋ น์ด
๋ฐ์ดํฐ๋ฒ ์ด์ค CRUD
-
๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ
mysql >CREATE DATABASE dbname;
-
๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ชฉ๋ก ๋ณด๊ธฐ
mysql >SHOW DATABASES;
-
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉํ๊ธฐ
mysql >USE dbname;
-
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ญ์ ํ๊ธฐ
mysql >DROP DATABASE [IF EXISTS] dbname;
ํ ์ด๋ธ CRUD
-
ํ ์ด๋ธ ์์ฑ
mysql >CREATE TABLE tablename();
ex) CREATE TABLE mytable(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR,
PRIMARY KEY(id)
); // ์๋ฌธ์๋ ์์์ง์ (column), ๋๋ฌธ์๋ ๋ช ๋ น์ด -
ํ ์ด๋ธ ๋ณด๊ธฐ
mysql >SHOW TABLES;
-
ํ ์ด๋ธ ๋ชฉ๋ก ๋ณด๊ธฐ
mysql >DESC tablename;
-
ํ ์ด๋ธ ์ฌ์ฉํ๊ธฐ
mysql >Use tablename;
-
ํ ์ด๋ธ ์ญ์ ํ๊ธฐ
mysql >DROP TABLE [IF EXISTS] tablename;
-
ํ ์ด๋ธ ์๋ก์ด ์ปฌ๋ผ ์ถ๊ฐ
mysql >ALTER TABLE tablename ADD COLUMN [์ถ๊ฐํ ์ปฌ๋ผ] [์ถ๊ฐํ ์ปฌ๋ผ ๋ฐ์ดํฐํ์ ];
ex) ALTER TABLE mytable ADD COLUMN model_type varchar(10) NOT NULL; -
ํ ์ด๋ธ ๋ฐ์ดํฐ ํ์ ๋ณ๊ฒฝ
mysql >ALTER TABLE tablename MODIFY COLUMN [๋ณ๊ฒฝํ ์ปฌ๋ผ๋ช ] [๋ณ๊ฒฝํ ์ปฌ๋ผ ๋ฐ์ดํฐํ์ ];
-
ํ ์ด๋ธ ์ด๋ฆ / ๋ฐ์ดํฐ ํ์ ๋ณ๊ฒฝ
mysql >ALTER TABLE tablename CHANGE COLUMN [๊ธฐ์กด์ ์ปฌ๋ผ๋ช ] [๋ณ๊ฒฝํ ์ปฌ๋ผ๋ช ] [๋ณ๊ฒฝํ ์ปฌ๋ผ ๋ฐ์ดํฐํ์ ];
-
ํ ์ด๋ธ ์ปฌ๋ผ ์ญ์
mysql >ALTER COLUMN tablename DROP COLUMN [๊ธฐ์กด์ ์ปฌ๋ผ๋ช ] [์ญ์ ํ ์ปฌ๋ผ๋ช ];
๋ฐ์ดํฐ CRUD
- ๋ฐ์ดํฐ ์์ฑ
- mysql >
INSERT INTO tablename VALES(123, '๊ฐ1' ...);
// ์ ์ฒด ์ปฌ๋ผ - mysql >
INSERT INTO tablename (column1, column2 ...) VALUES(123, '๊ฐ1' ...);
// ๋์ ์ปฌ๋ผ
- mysql >
- ๋ฐ์ดํฐ ์กฐํ
- mysql >
SELECT * FROM tablename;
// ์ ์ฒด ๋ฐ์ดํฐ - mysql >
SELECT column1, column2 ... FROM tablename;
// ๋์ ์ปฌ๋ผ
- mysql >
-
๋ฐ์ดํฐ ์กฐํ ์ ํ์๋๋ ์ปฌ๋ผ๋ช ๋ณ๊ฒฝ mysql >
SELECT beforename1 AS aftername1, beforename2 AS aftername2 From tablename;
- ๋ฐ์ดํฐ ์ ๋ ฌํด์ ์กฐํ
DESC๋ ๋ด๋ฆผ์ฐจ์, ASC๋ ์ค๋ฆ์ฐจ์- mysql >
SELECT * FROM tablename ORDER BY column DESC
// column์ ์ ๋ ฌ๊ธฐ์ค - mysql >
SELECT column1, column2 ... FROM tablename ORDER BY column ASC
// column์ ์ ๋ ฌ๊ธฐ์ค
- mysql >
-
OR๊ณผ AND ์กฐ๊ฑด ์ฐ์ฐ์๋ก ๋ฐ์ดํฐ ์กฐํ
mysql >SELECT * FROM tablename WHERE column1 < '๊ฐ' OR column2 > 2 OR colum3 = 3;
- LIKE๋ก ๋ถ๋ถ์ผ์น(%, _ ์ฐ์ฐ์) ๋ฐ์ดํฐ ์กฐํ
- mysql >
SELECT * FROM tablename WHERE column LIKE '%๊ฐ%';
- mysql >
SELECT * FROM tablename WHERE column LIKE '๊ฐ__'
// ๋ค์ ๋๊ธ์ ๋ถ์ ๊ฒฝ์ฐ
- mysql >
- LIMIT๋ก ๋ฐ์ดํฐ ์ ์ ํํด์ ๋ฐ์ดํฐ ์กฐํ
- mysql >
SELECT * FROM tablename LIMIT 10;
// ์ฒ์๋ถํฐ 10๊ฐ ๋ฐ์ดํฐ ์กฐํ - mysql >
SELECT * FROM tablename 10 LIMIT 20;
// 10๋ถํฐ 20๊ฐ ๋ฐ์ดํฐ ์กฐํ
- mysql >
-
์กฐ๊ฑด ์กฐํฉํด์ ๋ฐ์ดํฐ ์กฐํ
์กฐํฉ์์ SELECT FROM WHERE ORDER BY LIMIT mysql >SELECT column1, column2 FROM tablename WEHERE column1 < 2 AND column2 LIKE %๊ฐ% ORDER BY column1 DESC LIMIT 10;
-
๋ฐ์ดํฐ ์์
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 โ-+โโ+โโโโ+โโโโ+
- ๋ฐ์ดํฐ ์ญ์
- mysql >
DELETE FROM tablename WHERE ํน์ column = '๊ฐ';
- mysql >
DELETE FROM tablename;
- mysql >