본문 바로가기
CLASS/MYSQL

#3-1 / unique, alter문법

by eungSe__ 2024. 6. 4.

👀 product list

create table product(
pindex int(4) not null auto_increment,
pno char(4) not null, //상품 고유번호
pnm varchar(200) not null,
pmoney int(8) not null,
primary key(pindex,pno),
unique key(pno)
);

unique key 표시 안됨

 

[ unique key ]

=> primary key에서 절대 중복되지 말아 하는 데이터 colomn을 선정(중복시 error 발생)

     단, primary key에 없는 colomn 을 사용하더라도 문제가 되지 않음

     ("key" 는 그룹명일뿐 다른거 써도댐 - 통상적으로 key를 사용할 뿐 aaa 이런거 써도 댐) 

    https://dev-eunse.tistory.com/132 참고

 

* show indexes in 테이블명; => 

 

 Seq_in_index  : key number 구조 상세하게 

 

 

*  show table status from 데이터베이스명;  => 데이터베이스 관련 각 테이블별 상세정보를 출력

 

🔽 데이터 추가

insert into product values ('0','1234','냉장고','150000');
insert into product values ('0','1234','토스트기','800000'); 

 

시 error = > uniquekey

 

 

[ alter 문법 ]

컬럼을 수정,추가,삭제 등 할수 있는 명령어

 

* 추가 방법 

- alter table table명 add 추가할column명 자료형 null(or not null); 

=> table 기준 가장 아래에 컬럼이 추가됨

 

- alter table table명 add 추가할column명 자료형 null(or not null) after 기존column명

=> table 기준 자신이 원하는 위치에 column 추가

 

ex)

alter table product add ptext varchar(100) null after pnm;

alter table product add puse char(1) null default 'Y';

 

* colomn 속성 변경

자료형 수정, 자료형 범위를 조정할 때 사용

 

- alert table table명 modify column명 자료형 null (or not null) ;

 

* colomn 삭제

colomn삭제시 해당 데이터도 모두 삭제됨 - 주의

 

-alert table table명 drop colomn명;

 

'CLASS > MYSQL' 카테고리의 다른 글

#3-3 / excel 미러링, data backup 및 복구  (0) 2024.06.04
#3-2 / 외부 사용자 추가 방법  (0) 2024.06.04
#2-3 / database table 설계  (0) 2024.06.03
#2-2 / 사용자에게 권한주기  (0) 2024.06.03
#2-1 / MYSQL 기초문법  (0) 2024.06.03