728x90
DB사전
※DB 사전을 사용하기 전 구조정보를 먼저 확인한 후 필요한 컬럼만 찾아 사용
dictionary
user_object
user_tables
user_tab_columns
user_object 데이터 사전 사용
DB에 존재하는 Object에 대한 정보를 가진 뷰
desc user_objects;
select object_name, object_type, created, status
from user_objects
order by object_type;
user_tables 데이터 사전
user(사용자) 소유의 테이블 정보를 조회
desc user_tables;
select table_name from user_tables;
user_tab_columns 데이터 사전
user 소유의 테이블 내 컬럼 정보
desc user_tab_columns;
select data_type, data_lenght, data_default, .. from user_tab_columns;
user_constraints 데이터 사전
테이블의 제약조건 정보
desc user_constraints;
select constraint_name, constraint_type, search_condition, r_constraint_name
from user_constraints;
user_cons_columns 데이터 사전
테이블의 제약조건 정보를 컬럼과 매칭한 정보
desc user_cons_columns;
select constraint_name, column_name
from user_cons_columns;
user_views 데이터 사전
사용자가 소유한 뷰 정보를 조회
desc user_views;
select view_name, text
from user_views;
user_sequences 데이터 사전
사용자가 소유한 시퀀스 정보 조회
desc user_sequences;
select sequence_name, min_vaule, max_value, increment_by, last_number
from user_sequences;
user_indexes 데이터 사전
사용자가 설정한 시퀀스 정보 조회
desc user_indexes;
select index_name, table_name, uniqueness
from user_indexes
where lower(table_name) = 'employees';
index 생성 방법
자동 생성 : PK, UK 제약조건이 선언된 컬럼
수동 생성 : create index 구문으로 생성
※제약조건은 객체가 아님 제약조건명과 객체명과 겹치는 것은 가능
user_synonyms 데이터 사전
사용자가 설정한 시노님 정보 조회
desc user_synonyms;
select table_name, synonym_name from user_synonyms;
조건부 표현식(if-then-else)
case식
-- 문법
case expr when comparison_expr1 then return_expr1
[when comparison_expr2 then return_expr2...
else else_expr]
end
select last_name, job_id, salary,
case job_id when 'IT_PROG' then 1.10*salary
when 'ST_CLERK' then 1.15*salary
when 'SA_REP' then 1.20*salary
else salary end "revised_salary"
from employees;
case 표현식
※case표현식은 case보다 더 다양한 비교 연산자를 사용할 수 있어 더 광범위하다는 차이점이 있음
-- 문법
case when 조건문1 then 표현식1
when 조건문2 then 표현식2...
else 기본 표현식
end
select last_name, salary,
case when salary<5000 then 'Low'
when salary<10000 then 'Medium'
when salary<20000 then 'Good'
end qualified_salary
from employees;
-- case식을 case 표현식으로 표현하기
select last_name, job_id salary,
case when job_id='IT_PROG' then salary*1.1
when job_id='ST_CLERK' then salary*1.15
when job_id='SA_REP' then salary*1.2
else salary end revised_salary
from employees;
문제
select column_name, constraint_name, constraint_type, search_condition, r_constraint_name
from user_cons_columns natual join user_constraints
where lower(table_name) = lower('&table_name');
728x90
'KDT > DB' 카테고리의 다른 글
240327 DB - 데이터 제어어(DCL) (0) | 2024.03.27 |
---|---|
240321 DB - 조건부 표현식 2 (0) | 2024.03.21 |
240313 DB - 데이터정의어(DDL) - Index, Synonym (0) | 2024.03.13 |
240306 DB - 데이터정의어(DDL) - View, Sequence 1 (0) | 2024.03.06 |
240304 DB - SQL활용 데이터정의어(DDL) - table3 (0) | 2024.03.04 |