시작 1• Mailing List 관리를 위한 테이블로
create table mailing_list
( email varchar(100) not null primary key,
name varchar(100) );
• not null 이란 ? Primary key 이란 ?
• Insert 명령문
insert into mailing_list (name, email)
values ('Philip Greenspun','[email protected]');
• (name, email) 을 지정해 주는 것이 테이블 구조의 변화에도 영향을 안 받음 !
• 테이블 구조 변경
alter table mailing_list add (phone_number varchar(20));
시작 2• 입력 문자중에 ‘ 가 있는 경우
insert into mailing_list (name, email)
values ('Michael O''Grady','[email protected]');
• select 명령문의 예제
SQL> column email format a25
SQL> column name format a25
SQL> column phone_number format a12
SQL> set feedback on
SQL> select * from mailing_list;
시작 3• 이메일과 전화 번호를 같이 저장할려고 하면 ~~ 그리고 핸폰 / 직장 / 집 / 가게 등등의 한 사람이 여러개의 전화를 소유하고 있는 경우는 ?
drop table mailing_list;
create table mailing_list
( email varchar(100) not null primary key,
name varchar(100) );
create table phone_numbers
( email varchar(100) not null references mailing_list(email), number_type varchar(15) check (number_type in ('work','home','cell','beeper')),
phone_number varchar(20) not null );
• integrity error : foreign key 값이 존재하지 않는 경우 ~~SQL> insert into phone_numbers values ('[email protected]','work','(800) 555-1212');
ORA-02291: integrity constraint (SCOTT.SYS_C001080) violated - parent key not found
시작 4• referential integrity constraint ("references mailing_list")
insert into mailing_list (name, email)
values ('Philip Greenspun','[email protected]');
insert into mailing_list (name, email)
values ('Michael O''Grady','[email protected]');
insert into phone_numbers
values ('[email protected]','work','(800) 555-1212');
insert into phone_numbers
values ('[email protected]','home','(617) 495-6000');
insert into phone_numbers
values ('[email protected]','work','(617) 253-8574');
insert into phone_numbers
values ('[email protected]','beper','(617) 222-3456');
시작 5• check error : check (number_type in ('work','home','cell','beeper'))
ORA-02290: check constraint (SCOTT.SYS_C001079) violated
• join 예제 1 ( Cartesian Product : 테이블 서로 짝 짓기 !) N*M 개의 row
select * from mailing_list, phone_numbers;
• join 예제 2 ( 공통되는 key 값을 갖는 것들 만 !)
select * from mailing_list, phone_numbers where mailing_list.email = phone_numbers.email;
• delete 예제
delete from phone_numbers where email = '[email protected]';
• update 예제
update mailing_list set name = 'Phil-baby Greenspun' where email = '[email protected]';