반응형

ALTER TABLE
- 테이블내 컬럼을 추가, 삭제, 변경 합니다.
- 기본적으로 뒷 컬럼에 추가됩니다.
▶ 순서 지정
- 제일 뒤에 'FIRST' 또는 'ALTER 열 이름' 으로 지정합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER TABLE usertbl | |
ADD homepage VARCHAR(30) -- 열추가 | |
DEFAULT 'hhtp://www.hanbit.co.kr' -- 디폴트 값 | |
NULL; -- null 허용 |
▶ 명령어
명령어 | 설명 |
ADD | 컬럼 추가 |
MODIFY | 컬럼 속성 변경 |
CHANGE | 컬럼 이름 변경 |
DROP | 컬럼 삭제 |
▷ 컬럼 추가
ALTER TABLE 테이블명 ADD 열이름 자료형;
ALTER TABLE sample62 ADD newcol INT;
▷ 컬럼 속성 변경
ALTER TABLE 테이블명 MODIFY 열이름 자료형;
ALTER TABLE sample62 MODIFY newcol VARCHAR(20);
▷ 컬럼 이름 변경
ALTER TABLE 테이블명 CHANGE 기존 열이름 새 열이름 자료형;
ALTER TABLE sample62 CHANGE newcol c VARCHAR(20);
▷ 컬럼 삭제
ALTER TABLE 테이블명 DROP 열이름;
ALTER TABLE sample62 DROP c;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 회원 이름(name)의 컬럼명을 uName으로 변경하고 데이터 형식을 VARCHAR(20)으로 변경, NULL허용 | |
ALTER TABLE usertbl | |
CHANGE COLUMN name uNAME VARCHAR(20) NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 컬럼 삭제 | |
ALTER TABLE usertbl | |
DROP COLUMN mobile1; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 기본키를 삭제하는 경우 | |
ALTER TABLE usertbl | |
DROP PRIMARY KEY; |
외래키 추가
기존에 이미 테이블이 생성된 상태에서 컬럼에 외래키 속성을 추가할 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER TABLE 기준 테이블 ADD FOREIGN KEY (컬럼) REFERENCES 상대 테이블.(컬럼); | |
ALTER TABLE atbl ADD FOREIGN KEY (userID) REFERENCES btbl.userID; | |
-- 상대 테이블의 컬럼은 PK여야 합니다. |
반응형
'SQL' 카테고리의 다른 글
[MySQL] SubQuery (0) | 2022.09.17 |
---|---|
[MySQL] 테이블 추가, 삭제 (0) | 2022.09.17 |
[MySQL] EXISTS (0) | 2022.09.17 |
[MySQL] CASE (0) | 2022.09.16 |
[MySQL] 집계함수 (1) | 2022.09.16 |