데이터베이스 객체란? 테이블이나 뷰, 인덱스 등 데이터베이스 내에 정의하는 모든 것을 일컫는다.
ex
데이터베이스 테이블!but
열이나 별명은 객체가 아니다.# CREATE TABLE 테이블명(열 정의1, 열 정의2 ...)
CREATE TABLE 테이블명(열명 자료형 [DEFAULT 값] [NULL|NOT NULL])
mysql> CREATE TABLE sample62(
-> no INTEGER NOT NULL,
-> a VARCHAR(30),
-> b DATE);
Query OK, 0 rows affected (0.01 sec)
mysql> DESC sample62;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| no | int | NO | | NULL | |
| a | varchar(30) | YES | | NULL | |
| b | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
DROP TABLE 테이블명
TRUNCATE TABLE 테이블명
ALTER TABLE 테이블명 하부명령
ALTER TABLE 테이블명 ADD 열 정의
mysql> ALTER TABLE sample62 ADD newcol INTEGER;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> 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 | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
ALTER TABLE 테이블명 MODIFY 열 정의
mysql> ALTER TABLE sample62 MODIFY newcol VARCHAR(20);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> 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 | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
ALTER TABLE CHANGE [기존 열 이름] [신규 열 정의]
mysql> ALTER TABLE sample62 CHANGE newcol c VARCHAR(20);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> 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 | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
ALTER TABLE 테이블명 DROP 열명
mysql> ALTER TABLE sample62 DROP c;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC sample62;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| no | int | NO | | NULL | |
| a | varchar(30) | YES | | NULL | |
| b | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)