본문 바로가기
MySQL/생활코딩

생활코딩 - MySQL - 8. 테이블의 생성

by SKim입니다 2020. 6. 8.

 

테이블 생성 방법 검색

 

검색어: create table in MySQL

 

https://dev.mysql.com/doc/refman/8.0/en/create-table.html

 

MySQL :: MySQL 8.0 Reference Manual :: 13.1.20 CREATE TABLE Statement

MySQL 8.0 Reference Manual  /  ...  /  SQL Statements  /  Data Definition Statements  /  CREATE TABLE Statement 13.1.20 CREATE TABLE Statement CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] [partition_opt

dev.mysql.com

여기는 너무 복잡하다.

 

검색어: create table in MySQL cheat sheet

 (컨닝 페이퍼)

 

https://www.sqltutorial.org/sql-cheat-sheet/

 

SQL Cheat Sheet Download PDF it in PDF or PNG Format

This 3-page SQL Cheat Sheet provides you with the most commonly used SQL statements. Download the SQL cheat sheet, print it out, and stick to your desk.

www.sqltutorial.org

 

 

mysql -uroot -p

 

USE op;

※ 세미콜론 없이 엔터를 치면 실행되지 않는다.

 → 이것을 이용해서 가독성을 좋게 할 수 있다.

 

▶ CREATE TABLE topic(

 

 

 

 

(1) 첫번째 컬럼(id) 만들기

※ 엑셀과 MySQL의 아 주 중요한 차이

 : MySQL에서는 컬럼의 데이터 타입을 강제할 수 있다.

 

검색어: mysql datatype number

 

https://www.techonthenet.com/mysql/datatypes.php

 

MySQL: Data Types

MySQL: Data Types The following is a list of datatypes available in MySQL, which includes string, numeric, date/time, and large object datatypes. String Datatypes The following are the String Datatypes in MySQL: Data Type Syntax Maximum Size Explanation CH

www.techonthenet.com

숫자 데이터타입

 

- INT(m): -2147483648 ~ 2147483647 의 정수

 

- BIGINT(m): -9223372036854775808 ~ 9223372036854775807 의 정수

 

 id INT(11) NOT NULL AUTO_INCREMENT,

 

- 괄호 안의 숫자는 나중에 검색할 때 얼마까지만 노출시킬 것인지를 정하는 것이다.

  보통 11을 많이 쓴다.

 

- NOT NULL: 값이 없는 것을 허용하지 않겠다.

 

- AUTO_INCREMENT: 자동으로 1씩 증가

 

 

 

 

 

 

(2) 두 번째 컬럼(title) 만들기

https://www.techonthenet.com/mysql/datatypes.php

 

MySQL: Data Types

MySQL: Data Types The following is a list of datatypes available in MySQL, which includes string, numeric, date/time, and large object datatypes. String Datatypes The following are the String Datatypes in MySQL: Data Type Syntax Maximum Size Explanation CH

www.techonthenet.com

String 데이터타입

 

- VARCHAR(size): Variable Character(문자). ~255 characters

 

- TEXT(size): ~65,535 characters

 

- LONGTEXT(size): ~4,294,967,295 characters

 

▶ title VARCHAR(100) NOT NULL,

 

 

 

 

(3) 세 번째 컬럼(description) 만들기

description TEXT NULL,

 

- NULL: 값이 없는 것을 허용하겠다.

 

 

 

 

(4) 네 번째 컬럼(created) 만들기

https://www.techonthenet.com/mysql/datatypes.php

 

MySQL: Data Types

MySQL: Data Types The following is a list of datatypes available in MySQL, which includes string, numeric, date/time, and large object datatypes. String Datatypes The following are the String Datatypes in MySQL: Data Type Syntax Maximum Size Explanation CH

www.techonthenet.com

날짜/시간 데이터타입

 

- DATETIME

 

▶ created DATETIME NOT NULL,

 

 

 

 

 

(5) 다섯 번째 컬럼(author) 만들기

▶ author VARCHAR(30) NULL,

 

 

 

 

 

(6) 여섯 번째 컬럼(profile) 만들기

▶ profile VARCHAR(100) NULL,

 

 

 

 

(7) PRIMARY KEY

▶PRIMARY KEY(id));

 

① 성능적인 측면

② 중복 방지

 

id 컬럼이 우리가 지금 생성하는 topic 테이블의 가장 중요한 메인 키야.

이 컬럼의 값은 각각의 값들이 고유해야 돼. 중복되면 안 돼.

 

 

 

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,

PRIMARY KEY(id));

 

 

 

 

 

 

※ 이런 에러가 나오는 경우: MySQL이 기본적으로 세팅해준 비밀번호를 쓰고 있는 경우.

error 1820은 에러메시지의 식별자이다.

이것으로 검색하면 된다.

 

 

댓글