SQL command line

1. DB๊ด€๋ฆฌ์ž ์—ฐ๊ฒฐ, ์‚ฌ์šฉ์ž ์ƒ์„ฑ, ๊ถŒํ•œ ์„ค์ •

1) ๊ด€๋ฆฌ์ž ๋กœ๊ทธ์ธ

conn system/1234

 

2) ์ผ๋ฐ˜์‚ฌ์šฉ์ž ์ƒ์„ฑ

create user scott identified by tiger
default tablespace users
temporary tablespace temp;

 

3) ๊ถŒํ•œ ๋ถ€์—ฌ

grant connect, resource, plustrace, alter session to scott;

 

4) ์ผ๋ฐ˜์‚ฌ์šฉ์ž ๋กœ๊ทธ์ธ

conn scott/tiger;

 

5) ์ƒ์„ฑ ์‚ฌ์šฉ์ž Oracle๊ณผ ๊ณ„์ • ์—ฐ๋™

 

 

2. Table ์ƒ์„ฑ

create table test_table(
id varchar(50) primary key,
pass varchar(30) not null,
name varchar(25) not null,
age number(2)
);

โ€‹

โ€‹3. ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€, ์กฐํšŒ

insert into test_table values('hong', '1234', 'ํ™๊ธธ๋™', 35);
insert into test_table values('kim', '5678', '๊น€๊ธธ๋™', 45);
select*from test_tanle;

โ€‹โ€‹

DB์— ๋ฐ˜์˜

commit;

โ€‹

์ข…๋ฃŒํ•˜๊ธฐ

quit;

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹spldeveloper

-- ํ•œ ์ค„ ์ฃผ์„(์ฃผ์„ : ์‹ค์ œ ์‹คํ–‰X)๋ฌธ

/* ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„๋ฌธ

'' ๋ฌธ์ž์—ด

= ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž (์ง€์ •ํ•˜๊ธฐ)

; ์™„๋ฃŒ

โ€‹

DB > ํ…Œ์ด๋ธ” > ํ–‰(๋กœ์šฐ, ๋ ˆ์ฝ”๋“œ, ํˆฌํ”Œ) ์—ด (์ปฌ๋Ÿผ, ๋ณ€์ˆ˜, ํ•„๋“œ, ๋„๋ฉ”์ธ, ์†์„ฑ)

์ œ์•ฝ์กฐ๊ฑด : ํ•„์ˆ˜๋กœ ๋“ค์–ด๊ฐ€์•ผ ํ•จ

โ€‹

varchar ๊ฐ€๋ณ€๊ธธ์ด(var) ๋ฌธ์ž(char) ์ž๋ฃŒํ˜•

primary key ์œ ์ผ์„ฑ์ด ์žˆ๋Š” ๊ธฐ๋ณธ ํ‚ค

โ€‹

 

โ€‹

โ€‹

SQL (Structured Query Language) : ๊ตฌ์กฐํ™”๋œ ์งˆ์˜ ์–ธ์–ด (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ „์šฉ ์งˆ๋ฌธ ์–ธ์–ด)

- DDL, DML, DCL

1. DDL : ๋ฐ์ดํ„ฐ ์ •์˜์–ด -> DBA, USER (CREATE-Table ์ƒ์„ฑ, ALTER-๊ตฌ์กฐ ๋ณ€๊ฒฝ, DROP-์‚ญ์ œ)

2. DML : ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด -> USER (SELECT-๊ฒ€์ƒ‰, INSERT-์‚ฝ์ž…, DELETE-์ˆ˜์ •, UPDATE-ํŠน์ • ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ)

3. DCL : ๋ฐ์ดํ„ฐ ์ œ์–ด์–ด -> DBA (GRANT-๊ถŒํ•œ ๋ถ€์—ฌ, REVOKE-๊ถŒํ•œ ํ•ด์ง€, COMMIT-DB์— ๋ฐ˜์˜, ROLLBACK)

โ€‹

โ€‹

1. DDL : ๋ฐ์ดํ„ฐ ์ •์˜์–ด

1) Table ์ƒ์„ฑ

ํ˜•์‹) create table ํ…Œ์ด๋ธ”๋ช…(์นผ๋Ÿผ๋ช… ๋ฐ์ดํ„ฐํ˜• [์ œ์•ฝ์กฐ๊ฑด], ์นผ๋Ÿผ๋ช… ๋ฐ์ดํ„ฐํ˜•...);

create table test2(
id varchar2(20) primary key,
passwd varchar2(50) not null,
name varchar2(25) not null
);

โ€‹

 

2) Table ๊ตฌ์กฐ ๋ณ€๊ฒฝ

(1) ํ…Œ์ด๋ธ” ์ด๋ฆ„ ๋ณ€๊ฒฝ

ํ˜•์‹) alter table ๊ตฌํ…Œ์ด๋ธ”๋ช… rename to ์ƒˆ ํ…Œ์ด๋ธ”๋ช…;

alter table test2 rename to member;
select*from tab;

โ€‹โ€‹

(2) ํ…Œ์ด๋ธ” ์นผ๋Ÿผ ์ถ”๊ฐ€

ํ˜•์‹) alter table ํ…Œ์ด๋ธ”๋ช… add (์นผ๋Ÿผ๋ช… ์ž๋ฃŒํ˜•(n));

alter table member add (reg_date date);
select*from member;

โ€‹โ€‹

(3) ํ…Œ์ด๋ธ” ์นผ๋Ÿผ ์ˆ˜์ • : ์ด๋ฆ„๋ณ€๊ฒฝ, type, ์ œ์•ฝ์กฐ๊ฑด ์ˆ˜์ •

ํ˜•์‹) alter table ํ…Œ์ด๋ธ”๋ช… modify (์นผ๋Ÿผ๋ช… ์ž๋ฃŒํ˜•(n) ์ œ์•ฝ์กฐ๊ฑด);

alter table member modify(passwd varchar2(25));
desc member;

โ€‹

(4) ํ…Œ์ด๋ธ” ์นผ๋Ÿผ ์‚ญ์ œ

ํ˜•์‹) alter table ํ…Œ์ด๋ธ”๋ช… drop column ์นผ๋Ÿผ๋ช…;

alter table member drop column passwd;
select * from member;

โ€‹

 

3) Table ์ œ๊ฑฐ

ํ˜•์‹) drop table ํ…Œ์ด๋ธ”๋ช… purge;

purge ์†์„ฑ : ์ž„์‹œํŒŒ์ผ ์ œ๊ฑฐ

drop table member purge; -- ํ…Œ์ด๋ธ”+์ž„์‹œํŒŒ์ผ ์ œ๊ฑฐ
drop table member; -- ํ…Œ์ด๋ธ” ์ œ๊ฑฐ
purge recyclebin ; ์ž„์‹œํŒŒ์ผ ์ œ๊ฑฐ

โ€‹

โ€‹

โ€‹

 

 

2. DML : ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด

create table depart(
dno number(4),
dname varchar(50),
daddress varchar(100)
);

โ€‹โ€‹

1) insert : ๋ ˆ์ฝ”๋“œ ์‚ฝ์ž…

ํ˜•์‹) insert into ํ…Œ์ด๋ธ”๋ช…(์นผ๋Ÿผ๋ช…1, .. ์นผ๋Ÿผ๋ช…n) values(๊ฐ’1, ... ๊ฐ’n);

insert into depart(dno, dname, daddress) values(1001, '์˜์—…๋ถ€', '์‹ฑ๊ฐ€ํด');
insert into depart values(1000, '๊ธฐํš์‹ค', '์„œ์šธ'); -- ์นผ๋Ÿผ๋ช… ์ƒ๋žต ๊ฐ€๋Šฅ
insert into depart(dno, daddress) values(1002, '๋‰ด์š•'); -- ๋ถ€๋ถ„ ์นผ๋Ÿผ๋ช…

โ€‹

2) select : ๋ ˆ์ฝ”๋“œ ๊ฒ€์ƒ‰โ€‹ (ํ™œ์šฉ๋„ ๊ฐ€์žฅ ๋†’์Œ)

ํ˜•์‹) select ์นผ๋Ÿผ๋ช… from ํ…Œ์ด๋ธ”๋ช… [where ์กฐ๊ฑด์‹];

select*from depart; -- ์ „์ฒด ์นผ๋Ÿผ๋ช… : *
select dno, dname from depart; -- ๋ถ€๋ถ„ ์นผ๋Ÿผ๋ช…
select *from depart where dname is not null; -- ๋ถ€์„œ๋ช…์ด ์žˆ๋Š” ๊ฒฝ์šฐ ํ•œ์ •ํ•ด์„œ ์กฐํšŒ
select *from depart where dname is null; -- ๋ถ€์„œ๋ช…์ด ์—†๋Š” ๊ฒฝ์šฐ๋งŒ

โ€‹โ€‹

3) update : ๋ ˆ์ฝ”๋“œ ์ˆ˜์ • (where์ ˆ ์—†์œผ๋ฉด ํ…Œ์ด๋ธ” ์ „์ฒด ์ˆ˜์ •๋จ)

ํ˜•์‹) update ํ…Œ์ด๋ธ”๋ช… set ์นผ๋Ÿผ๋ช… = ๊ฐ’ where ์กฐ๊ฑด์‹;

update depart set dname = '๊ฐœ๋ฐœ๋ถ€', daddress = '๋Œ€์ „์‹œ' where dno =1002;
select*from depart; -- ์ „์ฒด ์นผ๋Ÿผ๋ช… : *

 

4) delete : ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ

ํ˜•์‹) delete from ํ…Œ์ด๋ธ”๋ช… where ์กฐ๊ฑด์‹;

delete from depart where dno = 1002;
select*from depart; -- ์ „์ฒด ์นผ๋Ÿผ๋ช… : *

โ€‹

 

 

โ€‹

 

3. DCL : ๋ฐ์ดํ„ฐ ์ œ์–ด์–ด (๊ด€๋ฆฌ์ž ํŠนํ™”)

1) ๊ถŒํ•œ ์„ค์ • : grant ๊ถŒํ•œ, ์„ธ๋ถ€๊ถŒํ•œ to user; ์œ ์ €๋ช…

2) ๊ถŒํ•œ ํ•ด์ œ : revoke ๊ถŒํ•œ, ์„ธ๋ถ€๊ถŒํ•œ to user; ์œ ์ €๋ช…โ€‹

โ€‹

db ๋ฐ˜์˜ : ์ž‘์—…๋‚ด์šฉ

commit;

โ€‹

+ Recent posts