CREATE TABLE USER_PRIMARYKEY(
USER_NO NUMBER PRIMARY KEY,
USER_ID VARCHAR2(20) UNIQUE,
USER_PWD VARCHAR2(30) NOT NULL,
USER_NAME VARCHAR2(30),
GENDER VARCHAR2(10),
PHONE VARCHAR2(30),
EMAIL VARCHAR2(50)
);
insert into user_primarykey
values(1,'user01','pass01','일용자','남','010111111','user01@naver.com');
--실행가능
insert into user_primarykey
values(1,'user02','pass02','이용자','여','010000100','user02@naver.com');
--프라이머리키의 중복으로 오류
오류 보고 -
ORA-00001: unique constraint (KH.SYS_C007067) violated
insert into user_primarykey
values(2,'user02','pass02','이용자','여','0100000000','user02@naver.com');
--null을 프라이머리키에 넣어서 오류
오류 보고 -
ORA-01400: cannot insert NULL into ("KH"."USER_PRIMARYKEY"."USER_NO")
create table pirmarykey2(
user_no number,
user_id varchar2(20),
primary key(user_no, user_id)
);
유니크 즉 고유값이다
유일한 값만 들어가도록 하고 싶을 때 사용합니다. 즉 중복을 허용하지 않는것이죠
그리고 당연히NOT NULL과 함께 사용할 수 있습니다.
create table shop_member(
user_no number unique,
user_id varchar(20) primary key,
user_pwd varchar(30) not null,
user_name varchar2(30),
gender char(1),
phone varchar2(20),
email varchar2(30)
);
create table shop_buy(
buy_no number primary key,
user_id varchar2(20) references shop_member(user_id),
proucdt_name varchar2(20),
reg_date date default sysdate);
insert into shop_member
values(1,'user01','pass01','일용자','M','0100000000','user01@iei.or.kr');
insert into shop_member
values(2,'user02','pass02','이용자','F','0110000000','user02@iei.or.kr');
insert into shop_member
values(3,'user03','pass03','삼용자','M','0120000000','user01@iei.or.kr');
--유저 3명추가
insert into shop_buy
values(1,'user01','축구화',default);
insert into shop_buy
values(2,'user02','농구화',default);
insert into shop_buy
values(3,'user03','족구화',default);
--정상 추가 가능
insert into shop_buy
values(4,'user04','피구화',default);
--shop_member에 없는 user이기 때문에 등록오류
오류 보고 -
ORA-02291: integrity constraint (KH.SYS_C007074) violated - parent key not found
delete from shop_member
where user_id = 'user01';
-- 레퍼런스 하고 있는 user01일 삭제하는 명령어
-- 오류로 삭제 불가
--오류 보고 -
ORA-02292: integrity constraint (KH.SYS_C007074) violated - child record found
DELETE FROM SHOP_BuY
WHERE USER_ID = 'user01';
--1
delete from shop_member
where user_id = 'user01';
--2
create table shop_buy(
buy_no number primary key,
user_id varchar2(20) references shop_memeber(user_id) **on delete set null**,
proucdt_name varchar2(20),
reg_date date default sysdate
);
on delete set null
을 함께 선언해준다delete from shop_member
where user_id = 'user01';
select * from shop_buy;
--shop_buy 출력
---------출력결과
1 null 축구화 22/07/19
2 user02 농구화 22/07/19
3 user03 족구화 22/07/19
create table shop_buy(
buy_no number primary key,
user_id varchar2(20) references shop_memeber(user_id) **on delete cascade**,
proucdt_name varchar2(20),
reg_date date default sysdate
);
create table shop_buy(
buy_no number **CONSTRAINT** buy_no_pk primary key ,
user_id varchar2(20) references shop_member(user_id) on delete cascade,
proucdt_name varchar2(20),
reg_date date default sysdate
);
create table shop_member(
user_no number, --unique
user_id varchar(30), --primary key
user_pwd varchar(40), --not null
phone varchar2(20),
email varchar2(50)
);
alter table shop_member
add constraint pk_user_id primary key(user_id);
alter table shop_member
add constraint uno_user_no unique(user_no);
alter table shop_member
modify user_pwd not null;
alter table employee_copy
drop constraint SYS_C007049;
--제약조건 이름을 입력하면 제약조건이 전부 삭제된다.
create table shop_buy(
buy_no number, --p
user_id varchar2(20), --r
product_name varchar2(50),
reg_date date); --default sysdate
alter table shop_buy
add constraint pk_buy_no primary key(buy_no);
alter table shop_buy
add constraint r_user_id_shop_member
foreign key (user_id) references shop_member(user_id);
alter table shop_buy
modify reg_date default sysdate;
--제약조건 활상화/비활성화
alter table shop_buy enable constraint r_user_id_shop_member;
--활성화
--alter table 테이블이름 enable constraint 컨스랜트이름;
alter table shop_buy disable constraint r_user_id_shop_member;
--비활성화
--alter table 테이블이름 disable constraint 컨스랜트이름;
alter table employee_copy add emp_name1 varchar2(40);
--컬럼추가
alter table employee_copy modify emp_name1 number;
--컬럼 수정
alter table employee_copy drop column emp_name1;
--컬럼 삭제
alter table shop_buy
rename column user_id to new_user_id;
--컬럼 이름 변경