** 자기 공부용 및 암기용으로 포스팅하는 글 입니다.
** 정확한 정보는 보장 할 수 없으니, 참고용으로만 봐주시면 감사하겠습니다.
** 제 개인적인 생각 및 자기 암시목적으로 작성되기에 이점 양해 부탁드립니다.
** MySQL 기준으로 작성되었음을 알립니다.
★ 알아볼 내용
1. Databese 데이터 타입
2. 제약 조건
3. ON DELECT
1. Databese 데이터 타입
주로 사용하는 데이터 타입은 아래와 같다.
-. CHAR(n)
고정 길이 문자 ( n : 1 ~ 255 byte )
-. VARCHAR(n)
가변 길이 문자 ( n : 1 ~ 65535 byte )
-. TINYTEXT
문자열 데이터 ( n : 최대 255 byte )
-. TEXT
문자열 데이터 ( n : 최대 65535 byte )
-. DECIMAL(p,s)
정밀한 고정 소숫점 숫자 ( p : 정밀도, s : 소숫점 자릿수)
-. BIT
0과 1, 또는 NULL 값을 가질 수 있는 정수 데이터 형식
-. ENUM('val1','val2'...)
정해진 몇가지의 값들 중 하나만 저장.
-. DATE
날짜(연,월,일) 형식
-. TIME
시간(시,분,초) 형식
-. DATETIME
날짜(연,월,일) + 시간(시,분,초) 형식. 사용자가 입력한 날짜 및 시간을 정확히 저장
-. TIMESTAMP
날짜(연,월,일) + 시간(시,분,초) 형식. 기본적으로 UTC기준이지만, 시간대 변환을
고려 하여 세션 시간대의 날짜 및 시간을 정확히 저장.
DEFAULT CURRENT_TIMESTAMP 또는 ON UPDATE CURRENT_TIMESTAMP 옵션을 통해 행이나
열이 변화될 때 자동으로 해당 시간을 입력할 수 있음.
2. 제약조건
제약조건을 통해 DB에 저장되는 값들이 특정 규칙에 부합하도록 강제 할 수 있다. 종류에는 4가지가 있다.
-. PRIMARY KEY
기본키 설정.
ex) PRIMARY KEY id 또는 id INT PRIMARY KEY
-. UNIQUE
지정된 속성들의 값들이 서로 다르게 유지되어야 함.
ex) UNIQUE id 또는 id INT UNIQUE
-. FOREIGN KEY ~ REFERENCES
하나의 테이블에 있는 속성이 다른 테이블의 기본키를 참조함으로 써 외래 키 관계 설정
ex) FOREIGN KEY (department_id) REFERENCES departments(department_id)
-. NOT NULL
열의 값이 절대 NULL이 아니여야함.
ex) id INT NOT NULL
3. ON DELECT
외래키에서의 제약조건을 설정 할 수 있다.
-. CASCADE
부모 테이블에서 행이 삭제되면, 이 행을 참조하는 모든 자식 테이블의 관련 행도 자동으로 삭제됨.
즉, 부모 행이 삭제되면 자식 행도 함께 삭제됨.
-. SET NULL
부모 테이블에서 행이 삭제되면, 자식 테이블에서 해당 외래 키를 NULL로 설정
이 옵션을 사용하려면 외래 키 열이 NULL을 허용하는 것이어야 함
-. SET DEFAULT
부모 테이블에서 행이 삭제되면, 자식 테이블에서 해당 외래 키를 기본값으로 설정
이 옵션을 사용하려면 외래 키 열에 기본값이 설정되어 있어야 함
-. RESTRICT
부모 테이블에서 행이 삭제되려고 하면, 해당 행이 자식 테이블에 참조되어 있으면 삭제를 방지
즉, 부모 행이 자식 행에 의해 참조되면 부모 행을 삭제할 수 없음
-. NO ACTION
RESTRICT와 유사하게 작동
부모 테이블에서 행이 삭제되려고 하면 해당 행이 자식 테이블에 참조되어 있으면 삭제를 방지
'DB[SQL] 지식 및 공부' 카테고리의 다른 글
[DB_SQL] 문제 풀이 #1 (0) | 2024.04.16 |
---|---|
[DB_SQL] 공부 및 지식 #1 DDL-개요 (0) | 2024.04.16 |
[DB_SQL] 공부 및 지식 #1 DDL (0) | 2024.03.31 |