오라클 테이블 명령어
명령어 설명 예시
desc 테이블명 테이블 구조 보기 desc testtable;
alter table 테이블명 add(칼럼명 칼럼형) 테이블 칼럼 추가 alter table testtable add(addcal number);
alter table 테이블명 drop(칼럼명) 테이블 칼럼 삭제 alter table testtable drop(addcal);
alter table 테이블명 rename column 원래 칼럼명 to 변경할 칼럼명 테이블 칼럼명 수정 alter table testtable originalcolumn to changecolumn
select * from tab 모든 테이블 보기 select * from tab;

[시퀀스 생성]

create sequence 시퀀스명

start with 시작번호

increment by 증가값

maxvalue 최대값

minvalue 최소값

cycle or nocycle -- cycle 설정시 최대값에 도달하면 최소값부터 다시 시작 nocycle 설정시 최대값 도달시 시퀀스 생성중지

cache or nocache -- cache 설정시 메모리에 시퀀스 값을 미리 할당하고 nocache 설정시 시퀀스 값을 메모리에 할당하지 않음

시퀀스 생성은 좌측과 같이 생성이 가능하다 아래 여러가지 옵션값은 생략이 가능하다.

create sequence testsequence

start with 1

increment by 1

maxvalue 1000

minvalue 1

no cycle

[시퀀스 검색]

select sequence_name,

min_value,

max_value,

increment_by,

cycle_flag

from user_sequences;

   

[시퀀스 적용]

시퀀스명.nextval

시퀀스 적용은 좌측과 같이 insert 때 시퀀스명.nextval로 적용할 수 있다. insert into member values(test_seq.nextval, 'name', '0100000000');

[pk 2개 선언]

constraint pk명 primary key(칼럼명1, 칼럼명2)

pk를 2개 선언하길 원할 경우 좌측과 같이 진행할 수 있다. create table money_tbl_02( 
custno number(6) not null, 
salenol number(8) not null, 
pcost number(8), 
amount number(4), 
price number(8), 
pcode varchar2(4), 
sdate date, 
constraint money_pk primary key (custno, salenol) 
);

[별칭 선언]

as 별칭명

별칭을 선언할 수 있다. select Max(custno) as max from member_tbl_02";"select Max(custno) as max from member_tbl_02;
(SELECT NVL(MAX(num),0)+1 FROM nuriboard) mariadb의 경우 ifnull을 사용하나 오라클에서는 NVL이 ifnull과 같다.  
sysdate 현재 날짜

select sysdate as "Current Date" from dual;

현재 날짜를 출력하고 열 이름을 Current Date로 함

next_day([DATE 입력], [요일]) 입력한 DATE 이후 날짜 중에서 찾고자 하는 요일의 첫 번째 일자를 반환 next_day(add_months(hire_date,6),'월요일') as "6개월 후 월요일"
add_months([DATE 입력]) 날짜에 월을 빼거나 더하는 함수 add_months(hire_date,6)
LPAD([값], [총 문자길이], ["채움문자"]) 왼쪽부터 총 길이만큼 지정된 문자열을 채운다. lpad(salary,15,'*')
to_char([날짜],['서식']) 날짜를 서식에 맞춰서 문자열로 반환

to_char(hire_date,'day') as "입사일"

위와 같이 하면 요일이 출력됨

length([문자열 or 변수]) 문자열 길이를 구하는 함수 length(first_name)
group by [변수] 데이터들을 원하는 그룹으로 나눌 수 있다. select
job_id as "직업",
max(salary) as "최대급여",
min(salary) as "최소급여",
sum(salary) as "급여의합"
from EMPLOYEES
group by job_id;
where [일치하길 원하는 칼럼명] in (조건1, 조건2, 조건3) 좌측 예시와 같이 in을 적용하면 or과 같이 하나씩 비교하여 조건이 맞을 경우 값을 반환한다. where department_id in (select department_id from EMPLOYEES where first_name like '%T%')
like

like 조건식은 문자열의 패턴을 검색할 때 사용하는 조건식이다.

좌측 예시와 같이 first_name like '%T%'를 진행할 경우 T가 포함된 이름을 조회한다.

select department_id from EMPLOYEES where first_name like '%T%'
'%A' A로 끝나는 문자열을 조회한다.  
'A%' A로 시작하는 문자열을 조회한다.  
'%A%' A를 포함하는 문자열을 조회한다.  

CASE

WHEN [칼럼명] 조건식 then 결과

ELSE 결과

END

조건식 if~else문과 같음

예제를 참고하여 사용하도록하자

select
first_name as "이름",
job_id as "업무",
salary as "급여",
CASE 
WHEN commission_pct is null then salary * 0
ELSE salary * commission_pct
END as "보너스",
CASE
WHEN commission_pct is null then (salary * 0)+salary
ELSE (salary * commission_pct)+salary
END as "급여+보너스"
from EMPLOYEES;
decode([컬럼명],[조건1],[결과],[조건2],[결과]) if, else와 비슷한 함수이다.  
select * from user_sequences; 현재 스키마의 시퀀스를 조회할 수 있다.  
synonym 객체의 실제 이름과 소유자 그리고 위치를 감춤으로써 데이터베이스 보안을 개선하는데 사용 한다 create SYNONYM EMPLOYEE
for EMPLOYEES;

[시퀀스]

select [시퀀스명].currval from [테이블명]

현재 시퀀스 값을 조회한다.

다만 생성 후 바로 조회시 조회가 안되므로 [시퀀스명].nextval로 시퀀스를 이동시킨 후 조회를 해야한다.

select test_seq_sequence.currval from dual;

[처음 생성시]

다음과 같이 먼저 nextval로 시퀀스 이동 후 조회하여야 조회가 가능함

selecselect test_seq_sequence.nextval from dual;

select test_seq_sequence.currval from dual;

set verify on/off sql 명령어나 pl/sql에서 &을 이용한 치환 변수 등을 사용할 때 치환되기 전 후의 자세한 값을 보일 건지의 여부를 결정한다. 기본값은 ON이다.  
     
     
     
MYSQL & 마리아DB 테이블 명령어
명령어 설명 예시
alter table [테이블명] add constraint [pk명] primary key([칼럼명]) 기본키 추가 ALTER TABLE SHOPUSER ADD CONSTRAINT shopuser_id PRIMARY KEY(id);
alter table [테이블명] drop primary key [pk명] 기본키 삭제 alter table SHOPUSER drop constraint shopuser_id
alter table [추가할 테이블명] add constraint [외래키명]  foreign key([칼럼명]) references [부모테이블명] (pk컬럼명) [ON DELETE CASECADE /  ON UPDATE CASECADE] 외래키 추가  

• ON DELTE CASECADE : 외래키에서 참조하는 키가 포함된 행을 삭제하려고 하면 해당 외래 키가 포함되어 있는 모든 행도 삭제

• ON UPDATE CASECADE : 외래키에서 참조하는 키 값이 포함된 행에서 키 값을 업데이트 하면 해당 외래 키를 구성하는 모든 값도 키에 지정된 새 값으로 업데이트되도록 지정


alter table [테이블명] drop foreign key [외래키명]
외래키 삭제  
[MariaDB] alter table [테이블명] change [원래칼럼명] [새칼럼명] [새칼럼타입] 마리아 디비에서 칼럼명 변경  
     
     

+ Recent posts