웹모아
웹모아[가입] 로그인 / 회원가입   검색     


  
mySQL기본
 

실습-테이블구조변경
2006-02-28 16:54:19 댓글:(0)   조회:47952
 

1.실습-칼럼추가와 삭제
  1).칼럼추가
mysql> alter table orders2 add column many int not null;
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from orders2;
+---------+------------+--------+---------------------+------+
I OrderID I CustomerID I Amount I oDate               I many I
+---------+------------+--------+---------------------+------+
I       1 I          2 I  12500 I 2005-09-01 12:00:00 I    0 I
I       2 I          3 I  35000 I 2005-09-01 13:00:00 I    0 I
I       3 I          5 I  23000 I 2005-09-03 17:00:00 I    0 I
I       4 I          6 I  42500 I 2005-09-06 19:00:00 I    0 I
I       5 I          2 I  33000 I 2005-09-13 13:00:00 I    0 I
+---------+------------+--------+---------------------+------+
5 rows in set (0.00 sec)

2). 칼럼 삭제
mysql> alter table orders2 drop column many;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I     I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3). 열들의 앞부분에 새컬럼 추가
mysql> alter table orders2 add column many int not null first;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I many       I int(11)          I      I     I 0       I       I
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I     I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

4). 특정 칼럼뒤에 새 칼럼 삽입
mysql> alter table orders2 add column many int not null after Amount;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I     I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I many       I int(11)          I      I     I 0       I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)


=======================================================================================================

1. 칼럼 구조변경
mysql> alter table orders2 modify column many smallint;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I     I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I many       I smallint(6)      I YES  I     I NULL    I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

2). 칼럼의 이름과 자료형을 변경함.
mysql> alter table orders2 change  column many mmany char(10);
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;


+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I     I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I mmany      I char(10)         I YES  I     I NULL    I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

3). 기본키 추가
mysql> alter table orders2 add primary key(OrderID, CustomerID);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I PRI I 0       I       I
I CustomerID I int(10) unsigned I      I PRI I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I mmany      I char(10)         I YES  I     I NULL    I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

4) 기본키 삭제
mysql> alter table orders2 drop primary key;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I     I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I mmany      I char(10)         I YES  I     I NULL    I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

5). 인덱스 만들기
mysql> alter table orders2 add index order_idx (CustomerID, OrderID);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I MUL I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I mmany      I char(10)         I YES  I     I NULL    I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

인덱스 확인
mysql> show index from orders2;
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
I Table   I Non_unique I Key_name  I Seq_in_index I Column_name I Collation I Cardinality I Sub_part I Packed I Null I Index_type I Comment I
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
I orders2 I          1 I order_idx I            1 I CustomerID  I A         I        NULL I     NULL I NULL   I      I BTREE      I         I
I orders2 I          1 I order_idx I            2 I OrderID     I A         I        NULL I     NULL I NULL   I      I BTREE      I         I
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.00 sec)

6). 인덱스 삭제
mysql> alter table orders2 drop index order_idx;                     
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc orders2;
+------------+------------------+------+-----+---------+-------+
I Field      I Type             I Null I Key I Default I Extra I
+------------+------------------+------+-----+---------+-------+
I OrderID    I int(10) unsigned I      I     I 0       I       I
I CustomerID I int(10) unsigned I      I     I 0       I       I
I Amount     I int(11)          I      I     I 0       I       I
I mmany      I char(10)         I YES  I     I NULL    I       I
I oDate      I datetime         I YES  I     I NULL    I       I
+------------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

7.테이블 이름 바꾸기
mysql> alter table orders2 RENAME AS orderTest; 
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0
. 


[이전글]  실습 -인덱스
[다음글]  실습-수정과 삭제


 
댓글쓰기는 회원(로그인 하신분) 이상 가능합니다.


 댓글 (0)

  
mySQL기본
페이지: 1 / 6   

 php에서 mysql 사용시 기본 흐름
열람:25071   2017.03.16


 MySQL 기본 사용법 및 예제
열람:147047   2017.03.16


 ] INSERT SELECT를 이용한 데이터 복사
열람:81   2015.12.13


 MYSQL DB와 연결 - mysqli_connect
열람:4509   2014.08.13


 mysql 시작과 중지
열람:2428   2010.05.03


 information_schema 를 이용한 각 DB별 용량 체크
열람:2249   2010.04.15


 MySQL 유용한 명령어 모음
열람:3062   2010.04.15


   [RE] MySQL 유용한 명령어 모음
열람:3298   2010.04.15


 SQL에서 한번에 많은 자료를 삽입하는 명령어
열람:3178   2010.04.03


 Import a CSV file to MySQL
열람:4146   2008.10.28



[1]  [2]  [3]  [4]  [5]  

복수단어 검색은 공백(space)로 구분해 주세요.




 
최근 글 [손님]
 
인기 글 [손님]