BACK END/DataBase

2일차(06.13)테이블 - 컬럼과 레코드(연산자, 정렬, 수정, 삭제)

라미보 2022. 6. 14. 02:22

 

 

oracle에서도 조건문을 사용 합니다.

 

 

 

[select 형식]
select 칼럼1,칼럼2,... from 테이블명;

 

select 칼럼1,칼럼2,... from 테이블명
where 칼럼=값;
(조건문일때 where를 사용한다.)

 

 


 

[연산자]

 

> (크다)    < (작다)

>= (크거나 작다)    <= (작거나 같다)

= (같다)     <> (같지 않다.)

 

and 구문 : between A and B - A보다 크고(이상) B보다 작은(이상) , A >= and <= B   : A와 B의 사이 / (자바에서 &&)

in구문 : or 연산자의 대체 구문이다

|| → 문자열을 연결하고 싶을 때 사용하는 연산자 (자바에서는 or의 뜻을 가짐)

 


 

 

 

 

 select * from members;

  ID NAME                     SALARY BIRTH    EMAIL
---- -------------------- ---------- -------- --------------
   1 제시카                      100 90/12/25 aaa@naver.com
   2 티파니                      700 85/12/03 bbb@naver.com
   3 수영                        300 78/08/21 aaa@daum.net
   4 효연                        800 97/05/09 ccc@daum.net
   5 제시카                      500 91/12/02 kkk@naver.com
   6 제시카                      500 91/12/02 kkk@naver.com
   7 유리                        400 89/04/12
   8 윤아                        300 72/09/05
   9 써니                        600 87/03/04 abc@naver.com
  10 수영                    1000000 22/06/13

 

 

 

 

 

Q. id가 3인 레코드의 모든 칼럼 조회

select * from members

where id=3;

 

Q. salary가 500이하인 레코드의  id, name, salary 칼럼만 조회

select id,name, salary from members

where salary<=500;

 (<= 이하, >= 이상)

 

Q. salary가 500이하 이면서 id가 5이상인 레코드의 id, name, salary 칼럼만 조회

select id,name, salary from members
where salary<=500 and id>=5;

 java에서 &&  , db에서는 and 

 

Q. id가 3~7 사이인 레코드 조회

select * from members
where id >=3 and id<=7;   ( A >= and <= B : A와 B의 사이)

 

또는 아래와 같이 표시할 수 있다.

select * from members
where id between 3 and 7;  (between A and B : A보다 크고(이상) B보다 작은(이상))

 

 

Q. 급여(salary)가 300~500인 레코드 조회

select * from members
where salary >=300 and salary <=500;

 

select * from members
where salary between 300 and 500;

 

 

Q. 생일이 90-1-1 이후인 레코드만 조회

select * from members
where birth>'90-1-1';

 

Q. 생일이 90년대인 레코드 조회

select * from members
where birth>='90-1-1' and birth <='99-12-31';

 

select * from members
where birth between '90-1-1'and '99-12-31';

 

 

Q. id 1, 4, 7 레코드 조회

select * from members
where id =4 or id=1 or id=7;

(java에서는 ||  = db에서는 or )

 

select * from members
where id in(1,4,7);

 

 

Q. id 수영, 효연 조회

select * from members
where name in('수영','효연');   

 

select * from members
where name ='수영' or name='효연';

 

 

Q. 번호와 이름만 출력

select id as 번호, name as 이름  from members;

 

select id as 번호, name as "이름", email as "이메일 주소"  from members;
별칭에 공백이 있을 때에는 꼭 큰따옴표로 둘러싸여한다.

 

 

Q. 이메일 값이 null인 레코드 조회

select * from members
where email is null;

 

Q. 이메일이 null값이 아닌 것을 조회

select * from members
where email is not null;

 

Q. 급여가 300인 레코드 조회

select * from members
where salary =300;

 

Q. 급여가 300이 아닌 레코드 조회

elect * from members
where salary <> 300;

 

Q. 급여가 300이면서 이메일이 없는 레코드 조회

select * from members
where salary=300 and email is null;

 

Q. 90년대생 중에 급여가 500이상인 레코드 조회

select * from members
where birth >= '90-1-1' and birth <= '99-12-31' and salary >=500;

 

select * from members
where birth between  '90-1-1' and  '99-12-31' and salary >=500;

 

 

Q. NAME||'의급여는'||SALARY||'원입니다.'

 

select name || '의 급여는' || salary || '원 입니다.'
from members;

(|| → 문자열을 연결하고 싶을 때 사용하는 연산자)

 

 

Q. 1번의 생일은 ~입니다

     10번의 생일은 ~입니다.

 

select id || '번의 생일은' || birth || '입니다.'
from members;

 

 


 

<연산자 like구문> : 패턴 일치 방식으로 조회, 패턴 문자 사용

와일드카드 문자

1) _(밑줄) : 1글자

2) % : 0~무한대 

 

예시)
김_:  김 다음에 꼭 다른 글자가 나와야 합니다. (김정, 김밥, 김호)
김__: 김 다음에 글자 2개가 들어와야 합니다. (김강회, 김혜인,  김밥(x) ) 
_김_: 김 앞뒤로 글자가 들어와야 합니다. (이김밥, 윤김정,   김호(x), 정김(x) )
_니 : 써니, 티파니(x)
__니:티파니

 

%은% : 은을 포함한 나머지 글자 ( 은, 최은, 은희, 곽은정, 김세은 )
김_%: 김 다음으로 한 글자가 오고, 그 뒤로는 몇 글자가 와도 된다. (김석, 김선호, 김아무개 )
%니 : '니'로 끝나는 사람을 다 찾습니다.  (니, 써니, 티파니 , 니가(x))


 

Q. 이름이 '니'로 끝나는 레코드 조회

select * from members
where name like '%니';

 

Q. 이메일이 daum인 사람 레코드 조회

select * from members
where email like '%daum%';

 

Q. 생일이 90년대생인 레코드 조회

select * from members
where birth like '9%';

 

이렇게도 조회할 수 있습니다.

select * from members
where birth  between '90-1-1'and '99-12-31';

 

 

 

 

 


talble을 조회하였을 때, 아이디 순서대로 나오는 것이 기본은 아닙니다.

순서가 뒤죽박죽 섞여서 나올 수도 있습니다. 정렬을 배워보겠습니다

 

 

[정렬 방법]

select 칼럼명~

from 테이블명

[where 조건]

order by 정렬 방식;

 

[정렬 방식]

order by 칼럼명1, 칼럼명2,...방식(오름차순 or 내림차순)

order by 칼럼번호1, 칼럼번호2,....

asc = 오름차순(order by 칼럼명 desc;)    

desc= 내림차순                                     →테이블명에서는 desc: 구조를 뜻함

 

 


 

 

Q. 오름차순 가나다... 순서대로 나온다.

select * from members
order by name asc;

 

Q. 이름 내림차순

select * from members
order by name desc;

 

Q. 생일 내림차순

select * from members
order by birth desc;

 

Q.생일 오름차순

select * from members
order by birth asc;

 

Q. 이메일 오름차순

select * from members
order by email asc;

 

Q. 오름차순 'asc 생략이 가능하다' → desc 내림차순은 생략 불가

select * fom members

order by name;

 

Q. 이름 기준, 오름차순 정렬, 같은 이름이 있으면 salary 높은 레코드가 먼저 나오도록 조회

select * from members

order by name asc, salary desc;  (내림차순으로 급여 낮은 사람은 뒤에 나오게 한다.)

 

 

Q. Salary 낮은 레코드부터 먼저 나오도록 조회하고,
같은 salary가 있으면 생일이 빠른 레코드가 먼저 나오도록 조회

 

select * from members

order by salary asc, birth asc;

 

asc(오름차순)은 생략이 가능하므로 아래와 같이 써도 된다.

select * from members

order by salary,birth

 

Q. email naver인 레코드를 급여 높은 것부터 나오도록 조회

select * from members

where email like '%naver%'

order by salary desc;

 

 

Q. 80년 대생 중에 급여가 500 이하인 레코드를 이름 오름차순으로 조회

select * from members

where birth like '8%' and salary <=500

order by name asc;

 

아래와 같이 하여도 같은 값이 나옵니다.

select * from members

where birth between '80-1-1' and '89-12-31' and salary <=500

order by name asc;

 

 

 

 


update 구문  -행 수정하기

 

[형식]

update 테이블이름

set 컬럼1=값1, 컬럼2=값2,...

where 조건식;

 

-update 하기 전에 이전 작업 commit먼저 해야 한다. 안 하면 rollback 안될 수 있다.

-형식보다는 문장을 외워두는 것이 좋겠습니다.

 


 

Q. 윤아로 이름을 수정하고 싶을 때

update members

set name ='윤아';

 

▪ rollback  : 방금 했던 작업 취소, 마지막으로 했던 commit으로 돌아간다.

 

Q. 이름 윤아로 수정, salary 50으로 수정

update members

set name= '윤아', salary =50;

 

Q. id 3, 5인 것만 바꾸고 싶다

update members

set name='윤아', salary=50

where id = 3 or id =5;

 

Q. id가 7인 레코드의 salary를 200, email ''abc@daum.net'으로 수정

update members

set salary=200,email='abc@daum.net'

where id=7;

 

Q. 모든 레코드의 salary를 50원 인상으로 수정

update members

set salary = salary +50;

 

Q.email이 없는 곳에 xyz@google.com 넣기

update members

set email = 'xyz@google.com'

where email is null;

 

 

 

 


 

[delet 형식]

 

delete from 테이블이름

[where 조건식]


 

Q.id가 5 이하인 레코드 삭제

delete from members

where id <= 5;

 

delete from members : 행을 삭제합니다

여기서 from을 생략할 수 있습니다(=delete members)

 

 

Q.salary가 500~800인 레코드 삭제 

delete members

where salary between 500 and 800;

 

 

 

(※ 레코드 삭제는 delete, 테이블 삭제는 drop table, 계정 삭제는 drop user Id cascade;)

레코드삭제 : delete 테이블명 where 조건;
테이블삭제 : drop table 테이블명;
계정삭제 : drop user id cascade;
alter table 테이블명 drop column 삭제될컬럼 ; -삭제하고 rollback을 해도 삭제된 내용이 돌아오지 않는다.