1. 사용할 데이터 베이스 선정

USE dbname;

 

2. table 생성

CREATE TABLE table (field1 type1, field2 type2);

 

3. 행, 열 데이터 타입 설정

참고

https://www.w3schools.com/mysql/mysql_datatypes.asp

 

MySQL Data Types

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

 

ex)

2-1  (열:  id, title, description, created, author, profile)

id 값이 정수타입으로 필수NOT NULL이고, 열이 증가하면 id값이 자동으로 하나씩 증가하는AUTO_INCREMENT 열 생성

CREATE TABLE topic(
	id INT(11) NOT NULL AUTO_INCREMENT

2-2 

title 값이 String이고 100자까지만 저장 가능한 열 생성

CREATE TABLE topic(
	id INT(11) NOT NULL AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL

 

2-3 

description 값이 String이고 본문내용의 유무와 길이가 자유인 열 생성

CREATE TABLE topic(
	id INT(11) NOT NULL AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL,
    description TEXT NULL //빈 값 허용

 

2-4

created 값이 날짜와 시간이 함께 저장되는 열 생성

CREATE TABLE topic(
	id INT(11) NOT NULL AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL,
    description TEXT NULL,
    created DATETIME NOT NULL

 

2-5

author, profile 값이 String이고 NULL이 허용되는 열 생성

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,

 

3. 주요 키 값 id 로 설정

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에서 지원하는 데이터 형(MySQL DataTypes)

형태데이터형범위크기

숫자형 TINYINT -128 ~ 128 , 0 ~ 255 1 Byte
SMALLINT -32768 ~ 32767 , 0 ~ 65535 2 Byte
MEDIUMINT -8388608 ~ 8388607, 0 ~ 16777215 3 Byte
INT, INTEGER -2147483648 ~ 2147483647, 0 ~ 4294967295 4 Byte
BIGINT -9223372036854775808 ~ 9223372036854775807
0 ~ 18446744073709551615
8 Byte
FLOAT -3.402823466E+38 ~ -1.175494351E-38
1.175494351E-38 ~ 3.402823466E+38
4 Byte
DOUBLE [PRECISION], REAL 1.7976931348623157E+308 ~ -2.2250738585072014E-308
0 ~ 2.2250738585072014E-308
8 Byte
DECIMAL(M,D), NUMERIC(M,D) 데이터 베이스 설정 및 시스템에 따라 다름 가변적 크기
날자형 DATE '1000-01-01' ~ '9999-12-31' 3 Byte
TIME '-838:59:59' ~ '838:59:59' 3 Byte
DATETIME '1000-01-01 00:00:00' ~ '9999-12-31 23:59:59' 8 Byte
TIMESTAMP '1970-01-01 00:00:01' ~ '2038-01-19 03:14:07' 4 Byte
YEAR 1901 ~ 2155 1 Byte
문자(열)형 CHAR(M) 1~ 255 개의 문자 M <= 255 Byte
BINARY(M) 1 ~ 255 개의 문자 M Byte
VARCHAR(M), VARBINARY(M) 1 ~ 255 개의 문자 M Byte
TINYBLOB, TINYTEXT 최대 2^8 입력된 길이 만큼
BLOB, TEXT 최대 2^16 입력된 길이 만큼
MEDIUMBLOB, MEDIUMTEXT 최대 2^24 입력된 길이 만큼
LONGBLOB, LONGTEXT 최대 2^32 입력된 길이 만큼
ENUM 최대 65525 개 1 ~ 2 Byte
SET 최대 64 개의 셋 1 ~ 8 Byte

출처 : https://gyrfalcon.tistory.com/entry/mySQL-Data-Type

 

 

 

 

 

 

 

참고문헌

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 cheat sheet

https://www.mysqltutorial.org/mysql-cheat-sheet.aspx

 

MySQL Cheat Sheet

MySQL cheat sheet provides you with the on-page that contains the most commonly used statements that help you practice with MySQL more effectively.

www.mysqltutorial.org

강의

https://youtu.be/d3ye07XRexs?si=qkZ4HGB-zgQhxvza 

 

'Database > mysql' 카테고리의 다른 글

mySQL 서버 접속과 데이터베이스 생성  (0) 2023.10.02
docker-compose.yml file 생성  (0) 2023.08.25

+ Recent posts