[DB] ORACLE - 6์ผ
in DB
ORACLE
์ค๋ผํด 6์ผ
[ํ ์ด๋ธ์์ฑ]
create table ํ
์ด๋ธ๋ช
(์ปฌ๋ผ๋ช
1 ์ปฌ๋ผํ์
[์ ์ฝ์กฐ๊ฑด],
์ปฌ๋ผ๋ช
2 ์ปฌ๋ผํ์
[์ ์ฝ์กฐ๊ฑด],.....);
- ๋ฌธ์๋ก ์์(30์์ด๋ด) : ์๋ฌธ ๋์๋ฌธ์,์ซ์,ํน์๋ฌธ์( _ , $ , # ),ํ๊ธ
- ์ค๋ณต๋๋ ์ด๋ฆ์ ์ฌ์ฉ ์๋จ
- ์์ฝ์ด(create, table, column๋ฑ)์ ์ฌ์ฉํ ์ ์๋ค
- ์๋ฃํ
- number : number(์ ์ฒด์๋ฆฌ,์์์ดํ), number โ ์ซ์ํ(๊ฐ๋ณํ)
- int : ์ ์ํ ์ซ์(๊ณ ์ ํ)
- varchar/varchar2 : ๋ฌธ์,๋ฌธ์์ด(๊ฐ๋ณํ) โ ์ต๋ 4000byte
- char : ๋ฌธ์,๋ฌธ์์ด(๊ณ ์ ํ) ==> 2000byte
- date : ๋ ์งํ
- clob : ๋ฌธ์์ด => ์ต๋ 4GB
- blob : ๋ฐ์ด๋๋ฆฌํ(๊ทธ๋ฆผ,์์ ,๋์์..) ===> ์ต๋4GB
-์ ์ฝ์กฐ๊ฑด-
not null : ํด๋น์ปฌ๋ผ์ NULL์ ํฌํจ๋์ง ์๋๋ก ํจ (์ปฌ๋ผ)
unique : ํด๋น์ปฌ๋ผ ๋๋ ์ปฌ๋ผ ์กฐํฉ๊ฐ์ด ์ ์ผํ๋๋ก ํจ (์ปฌ๋ผ,ํ ์ด๋ธ)
primary key : ๊ฐ ํ์ ์ ์ผํ๊ฒ ์๋ณํ ์ ์๋๋กํจ (์ปฌ๋ผ,ํ ์ด๋ธ)
references table(column) :
ํด๋น ์ปฌ๋ผ์ด ์ฐธ์กฐํ๊ณ ์๋ (๋ถ๋ชจ)ํ ์ด๋ธ์ ํน์ (์ปฌ๋ผ,ํ ์ด๋ธ) ์ปฌ๋ผ๊ฐ๋ค๊ณผ ์ผ์นํ๊ฑฐ๋ ๋๋ NULL์ด ๋๋๋ก ๋ณด์ฅํจ
check : ํด๋น์ปฌ๋ผ์ ํน์ ์กฐ๊ฑด์ ํญ์ ๋ง์กฑ์ํค๋๋กํจ (์ปฌ๋ผ, ํ ์ด๋ธ)
[์ฐธ๊ณ ] primary key = unique + not null
ex)
idx ์ผ๋ จ๋ฒํธ primary key
id ์์ด๋ unique
name ์ด๋ฆ not null
phone ์ ํ๋ฒํธ
address ์ฃผ์
score ์ ์ check
subject_code ๊ณผ๋ชฉ์ฝ๋
hire_date ์
ํ์ผ ๊ธฐ๋ณธ๊ฐ(์ค๋๋ ์ง)
marriage ๊ฒฐํผ check
-์ ์ฝ์กฐ๊ฑดํ์ธ-
constraint_name:์ด๋ฆ
constraint_type:์ ํ
โ p:primary key
โ u:unique
โ r:reference
โ c:check, not null
search_condition : check์กฐ๊ฑด ๋ด์ฉ
r_constraint_name : ์ฐธ์กฐํ ์ด๋ธ์ primary key ์ด๋ฆ
delete_rule : ์ฐธ์กฐํ ์ด๋ธ์ primary key ์ปฌ๋ผ์ด ์ญ์ ๋ ๋ ์ ์ฉ๋๋ ๊ท์น
โ (no action, set null, cascade๋ฑ)
-์ญ์ RULE-
on delete cascade : ๋์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ณ ,ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๋ ๋ฐ์ดํฐ๋ ์ญ์
on delete set null : ๋์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ณ ,ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๋ ๋ฐ์ดํฐ๋ NULL๋ก ๋ฐ๊ฟ
on delete restricted : ์ญ์ ๋์ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๋ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ฉด ์ญ์ ํ ์ ์์(๊ธฐ๋ณธ๊ฐ)
-์์ RULE-
on update cascade : ๋์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ฉด,ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๋ ๋ฐ์ดํฐ๋ ์์
[ํ ์ด๋ธ์์ ]
-๊ตฌ๋ฌธ-
alter table ํ
์ด๋ธ๋ช
add ์ปฌ๋ผ๋ช
๋ฐ์ดํฐํ์
[์ ์ฝ์กฐ๊ฑด]
add constraint ์ ์ฝ์กฐ๊ฑด๋ช
์ ์ฝ์กฐ๊ฑดํ์
(์ปฌ๋ผ๋ช
)
modify ์ปฌ๋ผ๋ช
๋ฐ์ดํฐํ์
drop column ์ปฌ๋ผ๋ช
[cascade constraints]
drop primary key [cascade] | union (์ปฌ๋ผ๋ช
,.....) [cascade] .... | constraint
์ ์ฝ์กฐ๊ฑด๋ช
[cascade]
-์ด๋ฆ๋ณ๊ฒฝ-
alter table ๊ธฐ์กดํ
์ด๋ธ๋ช
rename to ์ํ
์ด๋ธ๋ช
rename ๊ธฐ์กดํ
์ด๋ธ๋ช
to ์ํ
์ด๋ธ๋ช
alter table ํ
์ด๋ธ๋ช
rename column ๊ธฐ์กด์ปฌ๋ผ๋ช
to ์์ปฌ๋ผ๋ช
alter table ํ
์ด๋ธ๋ช
rename constraint ๊ธฐ์กด์ ์ฝ์กฐ๊ฑด๋ช
to ์์ ์ฝ์กฐ๊ฑด๋ช
[ํ ์ด๋ธ๋ณต์ฌ]
- ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ด์ฉํ ํ ์ด๋ธ์์ฑ๋ฐ ํ(๋ ์ฝ๋)๋ณต์ฌ
- ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ด์ฉํด์ ๋ณต์ฌํ๊ฒฝ์ฐ not null์ ์ ์ธํ ์ ์ฝ์กฐ๊ฑด์ ๋ณต์ฌ์๋จ
-๊ตฌ๋ฌธ -
create table ํ
์ด๋ธ๋ช
([์ปฌ๋ผ๋ช
1,์ปฌ๋ผ๋ช
2.....]) as ์๋ธ์ฟผ๋ฆฌ
-๊ตฌ์กฐ๋ง ๋ณต์ฌ -
create table ํ
์ด๋ธ๋ช
1
as select * from ํ
์ด๋ธ๋ช
2 where 1=0
[์ํ์ค]
- ์์ฐจ์ ์ผ๋ก ์ ์๊ฐ์ ์๋์ผ๋ก ์์ฑํ๋ ๊ฐ์ฒด
create sequence ์ํ์ค๋ช
[increment by ์ฆ๊ฐ๊ฐ] [start with ์์๊ฐ]
[maxvalue ์ต๋๊ฐ | minvalue ์ต์๊ฐ]
[cycle | nocycle]
[cache | nocache]
increment by ์ฆ๊ฐ๊ฐ : ์ฆ๊ฐ/๊ฐ์ ๊ฐ๊ฒฉ(๊ธฐ๋ณธ๊ฐ : 1)
start with : ์์๋ฒํธ(๊ธฐ๋ณธ๊ฐ : 1)
maxvalue / minvalue : ์ํ์ค์ ์ต๋/์ต์๊ฐ์ง์
cycle/nocycle : ์ต๋/์ต์๊ฐ์ ๋๋ฌ ์ ๋ฐ๋ณต์ฌ๋ถ๊ฒฐ์
cache / nocache : ์ง์ ํ ์๋๋งํผ ๋ฉ๋ชจ๋ฆฌ ์์ฑ์ฌ๋ถ๊ฒฐ์ (์ต์๊ฐ : 2, ๊ธฐ๋ณธ๊ฐ : 20)
[ insert ]
ํ ์ด๋ธ์ ๋ฐ์ดํฐ(์๋ก์ดํ)์ถ๊ฐ
insert into ํ ์ด๋ธ๋ช [ (column1, column2, .....)] values (value1,value2,.....)- column๊ณผ values์ ์์์ผ์น
- column๊ณผ values์ ๊ฐ์ ์ผ์น
[ update ]
ํ ์ด๋ธ์ ํฌํจ๋ ๊ธฐ์กด ๋ฐ์ดํฐ์์
์ ์ฒด ๋ฐ์ดํฐ ๊ฑด์(ํ์)๋ ๋ฌ๋ผ์ง์ง ์์
์กฐ๊ฑด์ ๋ง๋ ํ(๋๋ ์ด)์ ์ปฌ๋ผ๊ฐ์ ๊ฐฑ์ ํ ์ ์๋ค
update ํ ์ด๋ธ๋ช set ์ปฌ๋ผ๋ช 1=value1, ์ปฌ๋ผ๋ช 2=value2 ..... [where ์กฐ๊ฑด์ ]where ์ด ์๋ต์ด ๋๋ฉด ์ ์ฒดํ์ด ๊ฐฑ์
set์ ์ ์๋ธ์ฟผ๋ฆฌ์ฌ์ฉ๊ฐ๋ฅ, default์ต์ ์ฌ์ฉ๊ฐ๋ฅ
[ delete ]
ํ ์ด๋ธ์ ํฌํจ๋ ๊ธฐ์กด๋ฐ์ดํฐ๋ฅผ ์ญ์
ํ ๋จ์๋ก ์ญ์ ๋๋ฏ๋ก ์ ์ฒดํ์๊ฐ ๋ฌ๋ผ์ง
delete [from] ํ ์ด๋ธ๋ช [where ์กฐ๊ฑด์ ];- where์ ์๋ตํ๋ฉด ์ ์ฒดํ์ด ์ญ์ ๋จ
- ๋ฐ์ดํฐ๋ ์ญ์ ๋๊ณ ํ ์ด๋ธ ๊ตฌ์กฐ๋ ์ ์ง๋จ
[ transaction์ฒ๋ฆฌ ]
์ผ์ ์์๊ณผ ๋์ด ์๋ฒฝํ๊ฒ ๋ง๋ฌด๋ฆฌ(commit)
์ฒ๋ฆฌ๋์ค ์ธํฐ๋ฝํธ(interrupt:์ฅ์ )๊ฐ ๋ฐ์ํ๋ฉด ๋๋์์ด(rollback)
ex1 ) ํ ์ด๋ธ : test
create table test(
id number(5),
name char(10),
address varchar2(50));
ex2 ) ํ ์ด๋ธ๋ช : user1
create table user1(
idx number primary key,
id varchar2(10) unique,
name varchar2(10) not null,
phone varchar2(15),
address varchar2(50),
score number(6,2) check(score >=0 and score <= 100),
subject_code number(5),
hire_date date default sysdate,
marriage char(1) default 'N' check(marriage in('Y','N')));
ex3 ) ์ ์ฝ์กฐ๊ฑดํ์ธ
select constraint_name, constraint_type
from user_constraints
where table_name='USER1';
ex4 ) ํ ์ด๋ธ๋ช : user2
create table user2(
idx number constraint PKIDX primary key,
id varchar2(10) constraint UNID unique,
name varchar2(10) constraint NOTNAME not null,
phone varchar2(15),
address varchar2(50),
score number(6,2) constraint CKSCORE check(score >=0 and score <= 100),
subject_code number(5),
hire_date date default sysdate,
marriage char(1) default 'N' constraint CKMARR check(marriage in('Y','N')));
ex5 ) ์ ์ฝ์กฐ๊ฑดํ์ธ
select constraint_name, constraint_type
from user_constraints
where table_name='USER2';
ex6 ) ์ถ๊ฐ
insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(1,'aaa','kim','010-000-0000','์์ธ',75,100,'2010-08-01','Y');
insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(1,'aaa','kim','010-000-0000','์์ธ',75,100,'2010-08-01','Y');
โ ๋ฌด๊ฒฐ์ฑ์ ์ฝ์กฐ๊ฑด์ ์๋ฐฐ(์ด์ : idx 1 ์ค๋ณต)
insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'aaa','kim','010-000-0000','์์ธ',75,100,'2010-08-01','Y');
โ ๋ฌด๊ฒฐ์ฑ์ ์ฝ์กฐ๊ฑด์ ์๋ฐฐ(์ด์ : id aaa ์ค๋ณต)
insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','','010-000-0000','์์ธ',75,100,'2010-08-01','Y');
โ NULL์ ("HR"."USER1"."NAME") ์์ ์ฝ์
ํ ์ ์์ต๋๋ค
insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','lee','010-000-0000','์์ธ',120,100,'2010-08-01','Y');
โ ์ฒดํฌ์ ์ฝ์กฐ๊ฑด์ ์๋ฐฐ๋์์ต๋๋ค(์ด์ : score๊ฐ 0~100์ฌ์ด์ ์ ์ด์ด์ผํจ)
insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','lee','010-000-0000','์์ธ',75,100,'2010-08-01','K');
โ ์ฒดํฌ์ ์ฝ์กฐ๊ฑด์ ์๋ฐฐ๋์์ต๋๋ค(์ด์ :marriage๊ฐ Y ๋๋ N์ด์ด์ผํจ)
insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','lee','010-000-0000','์์ธ',75,100,'2010-08-01','N');
ex7 ) ํ ์ด๋ธ ๋ชฉ๋ก ํ์ธ
select * from tab;
ex8 ) ํ ์ด๋ธ์ ๋ด์ฉํ์ธ
select * from user1;
select * from user2;
ex9 ) ํ ์ด๋ธ์ ๊ตฌ์กฐํ์ธ
desc user1; (= describe user1)
ex10 ) ์ ์ฝ์กฐ๊ฑด์ ์ด๋ฆ์ ์์ฑํ์ง ์์๊ฒฝ์ฐ
select constraint_name, constraint_type
from user_constraints
where table_name='USER1';
ex11 ) ์ ์ฝ์กฐ๊ฑด์ ์ด๋ฆ์ ์์ฑํ ๊ฒฝ์ฐ
select constraint_name, constraint_type
from user_constraints
where table_name='USER2';
ex12 ) ํ ์ด๋ธ๋ช ๋ณ๊ฒฝ : test โ user3
alter table test rename to user3;
ex13 ) ์ปฌ๋ผ์ถ๊ฐ : user3 โ phone varchar2(15)
alter table user3 add phone varchar2(15);
desc user3;
ex14 ) ์ ์ฝ์กฐ๊ฑด์ถ๊ฐ : user3 โ id์ unique ,์ ์ฝ์กฐ๊ฑด๋ช UID2
alter table user3 add constraint UID2 unique(id);
select constraint_name, constraint_type
from user_constraints
where table_name='USER3';
ex15 ) ์ปฌ๋ผ์ถ๊ฐ : user3 โ no number (PK์ค์ )
alter table user3 add no number primary key;
desc user3
ex16 ) ๊ตฌ์กฐ๋ณ๊ฒฝ : user3 โ name char(10) ๋ฅผ varchar2(10)๋ก ๋ฐ๊ฟ
alter table user3 modify name varchar2(10);
desc user3
ex17 ) ์ปฌ๋ผ์ญ์ : user3 โ address
alter table user3 drop column address;
desc user3
ex18 ) ํ ์ด๋ธ์ญ์ / ํด์งํต๋น์ฐ๊ธฐ: user3
drop table user3;
purge recyclebin; โ ํด์งํต ๋น์ฐ๊ธฐ
drop table user3 purge; โ ํด์งํต์ ๋ฃ์ง ์๊ณ ๋ฐ๋ก ์ญ์
flashback table user3 to before drop; โ ํด์งํต์์ ๋์ด๋ฆฌ๊ธฐ
ex19 ) ์ํ์ค์์ฑ / ์ญ์
create sequence idx_sql increment by 2 start with 1 maxvalue 9 cycle nocache;
select idx_sql.nextval from dual; โ ๋ค์ ์ํ์ค๊ฐํ์(nextval)
select idx_sql.currval from dual; โ ํ์ฌ ์ํ์ค๊ฐํ์(currtval)
drop sequence idx_sql;
ex20 ) ํ ์ด๋ธ์์ฑ๊ณผ ์ํ์ค์ ์ฉ
create table book(
no number primary key,
subject varchar2(50),
price number,
year date);
create sequence no_seq increment by 1 start with 1 nocycle nocache;
insert into book(no,subject,price,year)values(no_seq.nextval,'์ค๋ผํด ๋ฌด์์ ๋ฐ๋ผํ๊ธฐ',10000,sysdate);
insert into book(no,subject,price,year)values(no_seq.nextval, '์๋ฐ 3์ผ ์์ฑ',15000,'2007-03-01');
insert into book values(no_seq.nextval, 'JSP ๋ฌ์ธ ๋๊ธฐ',18000,'2010-01-01');
select * from book;
ex21 ) ํ ์ด๋ธ ๊ตฌ์กฐ๋ง ๋ณต์ฌ
create table user3 as select * from user2 where 1=0;
desc user3;
select constraint_name, constraint_type, search_condition
from user_constraints
where table_name='USER3'; โ not null์ ์ ์ธํ๊ณ ๋ ์ ์ฝ์กฐ๊ฑด์ด ๋ณต์ฌ ์๋จ
โ not null์ ์ฝ์กฐ๊ฑด๋ sys_*****๋ก ๋ณต์ฌ๋จ
ex22 ) ํ ์ด๋ธ(idx โ bunho, name โ irum, address โ juso) ์ ๋ณต์ฌํ๊ณ id๊ฐ bbb์ธ ๋ ์ฝ๋๋ฅผ ๋ณต์ฌํ์์ค
์๋ณธํ ์ด๋ธ : user1 / ์ฌ๋ณธํ ์ด๋ธ : user4
create table user4(bunho, irum, juso)
as select idx, name, address from user1 where id='bbb';
select * from user4;
ex23 ) ํ ์ด๋ธ ์์ฑํ ํ์ถ๊ฐ
ํ ์ด๋ธ๋ช : dept
deptno number โ ๊ธฐ๋ณธํค, ์ ์ฝ์กฐ๊ฑด๋ช (DNO)
dname varcahr2(30) โ ๋ ํ์ฉ์๋จ, ์ ์ฝ์กฐ๊ฑด๋ช (DNAME)
ํ ์ด๋ธ๋ช : emp
empno number โ ๊ธฐ๋ณธํค,์ ์ฝ์กฐ๊ฑด๋ช (ENO)
ename varchar2(30) โ ๋ํ์ฉ์๋จ, ์ ์ฝ์กฐ๊ฑด๋ช (ENAME)
deptno number โ ์ธ๋ํค, ์ ์ฝ์กฐ๊ฑด๋ช (FKNO),
๋์๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ณ ์ฐธ์กฐํ๋๋ฐ์ดํฐ๋ NULL๋ก ๋ฐ๊ฟ
create table dept(
deptno number constraint DNO primary key,
dname varchar2(30) constraint DNAME not null);
create table emp(
empno number constraint ENO primary key,
ename varchar2(30) constraint ENAME not null,
deptno number, constraint FKNO foreign key(deptno) references dept on delete set null);
insert into dept(deptno,dname) values(10,'๊ฐ๋ฐ๋ถ');
insert into dept(deptno,dname) values(20,'์์
๋ถ');
insert into dept(deptno,dname) values(30,'๊ด๋ฆฌ๋ถ');
insert into dept(dname) values(40,'๊ฒฝ๋ฆฌ๋ถ'); โ ORA-00913: ๊ฐ์ ์๊ฐ ๋๋ฌด ๋ง์ต๋๋ค
insert into dept(deptno,dname) values(40,'๊ฒฝ๋ฆฌ๋ถ');
insert into emp(empno, ename, deptno) values(100,'๊ฐํธ๋',10);
insert into emp(empno, ename, deptno) values(101,'์์ด์ ',20);
insert into emp(empno, ename, deptno) values(102,'์ ์ฌ์',50);
โ 50๋ฒ๋ถ์ ์์(๋ฌด๊ฒฐ์ฑ์ ์ฝ์กฐ๊ฑด์๋ฐฐ)-๋ถ๋ชจํค๊ฐ ์์ต๋๋ค
insert into emp(empno, ename, deptno) values(103,'์ดํจ๋ฆฌ',40);
insert into emp(empno, ename) values(105,'์ฅ๋๊ฑด');
insert into emp(ename, deptno) values('๊ณ ์์',10);
โ primary key๋ NULLํ์ฉ ์ํจ
commit;
ex24 ) ์ญ์
delete from dept;
select * from dept;
rollback;
select * from dept;
delete from dept where deptno=40;
select * from dept;
select * from emp;
ex25 ) ์์ (update)
emp ํ ์ด๋ธ ์ฅ๋๊ฑด ์ฌ์์ ๋ถ์๋ฒํธ๋ฅผ 30์ผ๋ก ์์ ํ์์ค
update emp set deptno=30 where ename='์ฅ๋๊ฑด';
select * from emp;
commit;
