$
, _
사용 가능CREATE TABLE
CREATE TABLE [IF NOT EXISTS] 테이블명
(
칼럼1 데이터타입,
칼럼2 데이터타입,
);
,
(콤마) 사용CREATE TABLE topic(
id INT(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
description TEXT NULL,
created DATETIME NOT NULL,
author VARCHAR(30) NULL,
profile VARCHAR(100) NULL,
level VARCHAR(100) DEFAULT '0',
PRIMARY KEY(id));
column_name DATA_TYPE(m)
NOT NULL : 공백을 허용하지 않게 설정
AUTO_INCREMENT : 데이터가 늘어날 때마다 값 증가
NULL : 값이 없는 것을 허용
DEFAULT : 기본 값 지정
PRIMARY KEY()
SHOW [EXTENDED][FULL] TABLES;
DESC topic;
INSERT INTO topic (title,description,created) VALUES('MySQL','MySQL is ...',NOW());
SELECT *
FROM 테이블
WHERE 조회 조건
ORDER BY 칼럼1 [ASC|DESC], 칼럼2 [ASC|DESC]
LIMIT n;
SELECT * FROM topic;
SELECT id,title FROM topic;
SELECT * FROM topic WHERE title='MySQL';
SELECT * FROM topic WHERE title='MySQL' ORDER BY id DESC;
SELECT * FROM topic WHERE title='MySQL' ORDER BY id DESC LIMIT 1;
-- 다른 데이터 베이스 조회
SELECT * FROM other.topic;
UPDATE topic SET description='ORACLE is NO.1', title='ORACLE NO.1' WHERE id=2;
WHERE
문 주의 ⚠️ - 없으면 모든 값 변경DELETE FROM topic WHERE id = 2;
WHERE
문 주의 ⚠️ - 없으면 모든 값 삭제DROP TABLE topic;
RENAME TABLE topic TO topic_backup;
SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.id;
SELECT topic.id,title,name FROM topic LEFT JOIN author ON topic.author_id = author.id;
SELECT topic.id AS topic_id FROM topic;