CREATETABLE sample62( noINTEGERNOTNULL, a VARCHAR(30), b DATE); )
결과
mysql>DESC sample62; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ |no|int|NO||NULL|| | a |varchar(30) | YES ||NULL|| | b |date| YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
데이터 행 삭제
TRUNCATE TABLE [테이블명]
ALTER TABLE로 테이블에 열 추가하기
명령어
ALTERTABLE sample62 ADD newcol INTEGER;
결과
DESC sample62; +--------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +--------+-------------+------+-----+---------+-------+ |no|int|NO||NULL|| | a |varchar(30) | YES ||NULL|| | b |date| YES ||NULL|| | newcol |int| YES ||NULL|| +--------+-------------+------+-----+---------+-------+ 4rowsinset (0.00 sec)
ALTER TABLE ADD로 테이블에 열을 추가할 수 있다.
ALTER TABLE로 열 속성 변경하기
명령어
ALTERTABLE sample62 MODIFY newcol VARCHAR(20);
결과
DESC sample62; +--------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +--------+-------------+------+-----+---------+-------+ |no|int|NO||NULL|| | a |varchar(30) | YES ||NULL|| | b |date| YES ||NULL|| | newcol |varchar(20) | YES ||NULL|| +--------+-------------+------+-----+---------+-------+ 4rowsinset (0.00 sec)
ALTER TABLE로 열 이름 변경하기
명령어
ALTERTABLE sample62 CHANGE newcol c VARCHAR(20);
결과
DESC sample62; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ |no|int|NO||NULL|| | a |varchar(30) | YES ||NULL|| | b |date| YES ||NULL|| | c |varchar(20) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 4rowsinset (0.00 sec)
ALTER TABLE로 열 삭제하기
명령어
ALTERTABLE smaple62 DROP c;
결과
DESC sample62; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ |no|int|NO||NULL|| | a |varchar(30) | YES ||NULL|| | b |date| YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
27장 제약
테이블 열에 제약 정의하기
명령어
CREATETABLE sample631( a INTEGERNOTNULL, b INTEGERNOTNULLUNIQUE, c VARCHAR(30) );
결과
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO||NULL|| | b |int|NO| PRI |NULL|| | c |varchar(30) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
DESC sample632; +--------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +--------+-------------+------+-----+---------+-------+ |no|int|NO| PRI |NULL|| | sub_no |int|NO| PRI |NULL|| | name |varchar(30) | YES ||NULL|| +--------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
열 제약 추가하기
기존 테이블
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO||NULL|| | b |int|NO| PRI |NULL|| | c |varchar(30) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
명령어
ALTERTABLE sample631 MODIFY c VARCHAR(30) NOTNULL;
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO||NULL|| | b |int|NO| PRI |NULL|| | c |varchar(30) |NO||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
테이블 제약 추가하기
기존 테이블
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO||NULL|| | b |int|NO| PRI |NULL|| | c |varchar(30) |NO||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO| PRI |NULL|| | b |int|NO| UNI |NULL|| | c |varchar(30) |NO||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
제약 삭제
열 제약 삭제하기
기존 테이블
+-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO| PRI |NULL|| | b |int|NO| UNI |NULL|| | c |varchar(30) |NO||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.01 sec)
c 열의 NOT NULL 제약 없애기
ALTERTABLE sample631 MODIFY c VARCHAR(30);
결과
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO| PRI |NULL|| | b |int|NO| UNI |NULL|| | c |varchar(30) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
테이블 제약 삭제하기
기존 테이블
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO| PRI |NULL|| | b |int|NO| UNI |NULL|| | c |varchar(30) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
DESC sample631; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | a |int|NO||NULL|| | b |int|NO| PRI |NULL|| | c |varchar(30) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.00 sec)
기본키
sample634 테이블 작성하기
CREATETABLE sample634( p INTEGERNOTNULL, a VARCHAR(30), CONSTRAINT pkey_sample634 PRIMARY KEY(p) );