CREATE DATABASE dbname;
DROP DATABASE dbname;
SHOW DATABASES;
USE dbname;
CREATE DATABASE dbname DEFAULT CHARACTER SET utf8mb4;
SELECT host, user FROM USER;
CREATE USER 'theo'@'localhost' identified by '1234';
CREATE USER 'theo'@'%' identified by '1234';
SHOW GRANTS FOR 'theo'@'localhost';
GRANT ALL ON dbname.* to 'theo'@'localhost';
REVOKE ALL ON dbname.* from 'theo'@'localhost';
CREATE TABLE tbname
(
columnname datatype,
columnname datatype
PRIMARY KEY (columnname)
);
ALTER TABLE tbname
ADD PRIMARY KEY (columnname, columnname, ...)
SHOW TABLES;
DESC tbname;
ALTER TABLE tbname RENAME new_tbname;
ALTER TABLE tbname ADD COLUMN columnname datatype;
ALTER TABLE tbname MODIFY COLUMN columnname new_datatype;
ALTER TABLE tbname CHANGE COLUMN columnname new_columnname datatype;
ALTER TABLE tbname DROP COLUMN columnname;
DROP TABLE tbname;
INSERT INTO tbname (colname1, colname2, colname3, colname4)
VALUES (data1, data2, data3, data4);
INSERT INTO tbname
VALUES (data1, data2, data3, data4)
SELECT * FROM tbname;
SELECT colname1, colname2 FROM tbname;
SELECT * FROM tbname WHERE colname=condition;
UPDATE person SET age=23 WHERE name='이효리';
DELETE FROM person WHERE name='이상순';
SELECT AGE, NAME
FROM celeb
ORDER BY AGE DESC, NAME ASC;
SELECT NAME, AGE
FROM celeb
WHERE AGE > 29
ORDER BY AGE;
SELECT * FROM celeb
WHERE (NOT JOB_TITLE='가수' AND SEX='F') OR
(NOT AGE < 40 AND ID % 2 = 1);
SELECT * FROM CELEB WHERE SEX='F'
UNION -- 은 중복 제거 & UNION ALL은 중복 포함
SELECT * FROM CELEB WHERE JOB_TITLE='가수';
SELECT column1, column2, ...
FROM tableA
INNER JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
SELECT column1, column2, ...
FROM tableA
LEFT JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
MySQL에서는 FULL JOIN을 지원하지 않으므로 LEFT JOIN과 RIGHT JOIN의 UNION을 통해 실행할 수 있다.
SELECT column1, column2, ...
FROM tableA
LEFT JOIN tableB
ON tableA.column = tableB.column
WHERE condition
UNION
SELECT column1, column2, ...
FROM tableA
RIGHT JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
SELECT column1, column2, ...
FROM tableA, tableB
WHERE tableA.column = tableB.column
SELECT CONCAT('이름: ', name) FROM celeb;
SELECT name AS '이름', agency as '소속사' from celeb;
SELECT CONCAT(name, ': ', job_title) AS PROFILE FROM celeb;
SELECT s.season, s.episode, c.name, c.job_title
FROM celeb as c, snl_show as s
WHERE c.name = s.host;
SELECT CONCAT(s.season, '-', s.episode, '(',s.broadcast_date, ')') AS '방송정보',
CONCAT(c.name, '(', c.job_title, ')') AS '출연자정보'
FROM celeb AS c, snl_show AS s
WHERE c.name = s.host;
SELECT DISTINCT agency FROM celeb;
SELECT * FROM celeb ORDER BY age LIMIT 4;
SELECT case_number,
(SELECT avg(case_number)
FROM crime_status
WHERE crime_type LIKE '강도' AND status_type LIKE '검거') avg
FROM crime_status
WHERE police_station LIKE '은평' AND crime_type LIKE '강도' AND status_type LIKE '검거';
SELECT c.police_station, c.crime_type, c.case_number
FROM crime_status c,
(SELECT police_station, max(case_number) count
FROM crime_status
WHERE status_type LIKE '발생'
GROUP BY police_station) m
WHERE c.police_station = m.police_station
AND c.case_number = m.count;
SELECT name FROM celeb WHERE name = (SELECT host FROM snl_show WHERE id = 1);
SELECT host
FROM snl_show
WHERE host IN (SELECT name
FROM celeb
WHERE job_title LIKE '%영화배우%');
SELECT name
FROM police_station p
WHERE EXISTS (SELECT police_station
FROM crime_status c
WHERE p.name = c.reference and case_number > 2000);
SELECT name
FROM celeb
WHERE name = ANY (SELECT host
FROM snl_show);
SELECT name
FROM celeb
WHERE name = ALL (SELECT host
FROM snl_show
WHERE id = 1);
SELECT name, sex, agency
FROM celeb
WHERE (sex, agency) IN (SELECT sex, agency FROM celeb WHERE name = '강동원');