본문 바로가기

청년취업아카데미/DayLog

[ Database ] Day 06. Deadlock and Log

...더보기

TIP

Deadlock은 프로그램 동작시 가끔 생길 수 있습니다. 이번 포스팅에서는 데드락이 생기는 이유와 데드락의 필요조건에 대하여 알아봅니다.

# DCL

Commit: Memory(Buffer)에 있는 내용을 물리적 공간(Table Space)에 저장,
commit을 안했을 경우, 다른 세션에서 접근하면 결과가 다르게 나올 수 있다.
Roll Back: Memory에 있는 내용을 지움.

 

-INITCAP(name) : 첫 글자만 대문자 처리

NVL(column, null default 값) : column 안의 값이 null 일 경우 default 초기화 시킴

select ename, hiredate
from emp e1
where deptno = (select deptno from emp e2 where INITCAP(ename)='Blake');

-- 한꺼번에 데이터 삽입
insert all
when height > 170 then
     into height_info values(studno, name, height)
when weight > 70 then
     into weight_info values(studno, name, weight)
select studno, name, height, weight
from student
where grade >= 2;

 

 

# 데드락( Dead Lock )

교착 상태는 한 시스템 내에서 다음의 네 가지 조건이 동시에 성립할 때 발생한다.

  1. 상호배제(Mutual exclusion)
  2. 점유 대기(Hold and wait)
  3. 비선점(No preemption)
  4. 순환 대기(Circular wait) 

update emp
set sal = sal*1.1
where empno = 7369;

update emp
set sal = sal*1.1
where empno = 7839;

update emp
set sal = comm=500
where empno = 7839;

update emp
set sal = comm=300
where empno = 736;

----- dead lock이 걸리는 이유 : 트랜잭션은 전부 실행되는 것이 아닌 부분적으로 실행되는데, 이때 트랜잭션이 끝나기 전에 자원을 잠금(lock)하기 때문에 교착상태가 발생한다.

 

 

 

# SQL Log

(1) 실습 기본 파일

Create Table - EMP DEPT 실습용 자료 생성.sql
0.01MB

(2) EMP 실습 파일

EmpEx.sql
0.00MB

 

 

 

# 동의어(synonym)

데이터베이스 객체의 소유권은 해당 객체를 생성한 사용자가 가지므로 사용자가 소유한 객체에 접근하기 위해서는 소유자로부터 접근 권한을 부여받아야한다.

 

 

# 권한 주기

 

<유저 생성 및 권한 주기>

create user scott identified by tiger;
grant query rewrite to scott;
grant create session, resource to scott;

select * from user_sys_privs;

create user scott2 identified by tiger2
default tablespace users
temporary tablespace temp;
grant connect, resource to scott2;

 

<Table 권한 주기>

[System 계정]
create table project(
project_id    number(5) constraint pro_id_pk primary key,
project_name  varchar2(100),
studno        number(5),
profno        number(5)
)
insert into project values (12345, 'profolio', 10101, 9901);
select * from project;
grant select on project to Scott;

[Scott 계정]
select * from project; --> Error
ORA-00942: table or view does not exist
00942. 00000 -  "table or view does not exist"

select * from system.project; --> 결과 나옴.