SQL 기초와 예시
SQL Fiddle | A tool for easy online testing and sharing of database problems and their solutions.
Query Panel Use this panel to try to solve the problem with other SQL statements (SELECTs, etc...). Results will be displayed below. Share your queries by copying and pasting the URL that is generated after each run.
sqlfiddle.com
웹사이트에서 sql 연습할 수 있음.
닭잡는데 소잡는 칼이 필요할까?
그냥 간단한 거 하고 싶은데 오라클이니 MySQL 틀어서 테이블 생성하고 지우기 귀찮.
그럴 때 이거 사용하면 좋다.
ddl은 commit이 자동으로 실행
dml은 commit을 해야 함.
truncate는 자동 commit이니 복구 불가.
truncate 와 delete의 차이점
truncate는 일부 삭제 불가
delete는 일부, 전체 삭제 가능
예시
설명이나 일반식을 보는 것보다 예시 문장 보는게 이해가 빨라서 적어 놓았음
create table pk_table (
key varchar2(50),
constraint pk_column primary key(key)
);
create table fk_table(
val varchar2(50),
key varchar2(50),
CONSTRAINT FK_COLUMN
FOREIGB KEY(key)
REFERENCES pk_table(key)
);
alter table emp rename to new_emp
alter table emp add(ename number(2) default 0) ;
altler table emp modify(ename varchar2(40) not null) ;
alter table emp drop column age ;
alter table emp rename column ename to new_ename ;
drop table emp ;
drop table emp cascade constraint ;
create view t_emp as select * from emp ;
select * from t_emp ;
drop view t_emp ;
insert into table(column1, column2) values(1,2) ;
insert into test1 select * from test2 ;
insert into fk_table values('test_val', 'pk_val');
update emp set ename ='ddd' where empno = 100 ;
update test_table set name='철수', age=29 where age=19;
delete from emp where empno = 100 ;
select * from emp where empno = 100 ;
select empno, ename from emp ;
select ename || '님' from emp;
select * from emp order by ename, sal desc ;
select /*+ index_desc(A) */ from emp A ;
select distinct deptno from emp order by deptno ;
Rownum
select * from test_table where rownum < 4;
select ROWNUM, val, date_val from( select val, date_val from test_table order by val desc);
Union
select * from test_table union select * from test_table2;
Inner join
select a.name, a.age from test_table a, test_table b where a.name= b.name;
select a.name, a.age from test_table a inner join test_table2 b on a.name = b.name
Left join
select a.name, a.age, b.name, b.val from test_table a, test_tabel2 b where a.name = b.name (+) ;
select a.name, a.age, b.name, b.val from test_table a left join test_table2 b on a.name = b.name ;
Right join
select a.name, a.age, b.name, b.val from test_table a, test_table2 b where a.name(+) = b.name ;
select a.name, a.age, b.name, b.val from test_table a right join test_table2 b on a.name = b.name ;
Full outer join
select a.name, a.age, b.name, b.val from test_table a full outer join test_table2 b on a.name = b.name ;