본문 바로가기

국비지원_핀테크

17일차_ [DB] Oracle DB 설치, sqldeveloper 설치 및 DB 생성, 기본 문법

 

 

 

 

Oracle DB 설치, sqldeveloper 설치

기존 PC 에 오라클 버전이 설치되어 있다면 버전을 확인한 뒤 구선생님께 "오라클 19c 완전 삭제" 와 같은 방식으로 검색하여 프로그램 삭제 뿐만이 아니라 완벽하게 설치나 설정을 날려준 뒤 시작한다.

 

 

 

 

 

DB, sqldeveloper 설치

 

15일차_ [DB] DB 다운로드

오라클 DB 다운로드 Oracle Database 19c Download for Microsoft Windows x64 (64-bit) Oracle Database 19c Grid Infrastructure (19.3) for Microsoft Windows x64 (64-bit) Contains the Grid Infrastructure Software including Oracle Clusterware, Automated St

youngho3358.tistory.com

 

 

 

 

 

Oracle DB 설치

 

1. C드라이브에 app 폴더 생성

app 폴더

 

 

2. 다운로드 받은 오라클 DB 를 app 폴더로 이동

폴더 이동

 

 

3. 압출 풀기

압축 풀기

 

 

4. 폴더 안의 setup.exe 클릭하여 설치

setup.exe 실행

 

 

5. 다음

 

 

6. 다음

 

 

7. 다음

 

 

8. 비밀번호 설정

오라클 설치 위치를 C:\app\oracle 로 지정한 뒤 비밀번호 설정

 

 

9. 예

 

 

10. 설치

 

 

11. 닫기

 

 

 

 

 

 

 

 

 

 

sqldeveloper 설치 및 DB 생성

 

1. sqldeveloper 압축 해제

 

 

2. sqldeveloper.exe 실행

 

 

3. 아니오 클릭

 

 

4. + 버튼 클릭

 

 

5. DB 의 이름, 사용자 이름(ID), 비밀번호 입력, ip정보, 포트 번호, DB 버전이 19 이상이라면 SID 값을 orcl 로 변경 후 저장 > 접속 클릭

최초 생성 system 은 모든 권한을 가진 사용자 (관리자) 로 생성됨 > 즉, 일반 계정을 생성해야 함

 

 

6. 방금 입력한 아이디 / 비밀번호 입력

 

 

7. 명령어 입력창 출력, 생성된 핀테크 DB 는 default 생성 DB 이다.

 

 

8. 계정 생성 ( c##youngho3358 / 1234 )

유저 생성 완료

코드 실행 : Ctrl + Enter

 

 

9. 방금 생성한 계정으로 DB 생성 시도 ( 테스트 버튼 클릭 )

계정을 생성되었지만 DB 접근 권한이 없기 때문에 오류 발생

 

 

10. dba(database admin) 권한 추가

권한 부여

 

 

11. 다시 DB 생성 시도 ( 테스트 버튼 클릭 ) 후 접속 클릭하여 DB 생성

 

 

12. DB 생성 완료

 

 

13. 테이블 생성 ( 테이블 명 : person )

create table person(
    num number, -- 숫자 저장 자료형
    name VARCHAR2(10), -- 문자 저장 자료형(byte)
    birth VARCHAR2(10),
    tel VARCHAR2(20),
    PRIMARY key( num ) 
    -- num 이라는 값을 기본 키로 설정
    -- num 값의 중복을 허용하지 않음을 의미
    -- null 값을 등록할 수 없고 값이 무조건 채워져 있어야 한다
);

create table <테이블명> ( 테이블 값 );

 

 

14. 생성된 테이블 확인

 

 

15. 테이블 확인 ( desc )

desc person;

person 테이블 확인

테이블 정보 출력

 

 

16. 테이블 하나 더 생성해보기

create table person111(
    num number, -- 숫자 저장 자료형
    name VARCHAR2(10), -- 문자 저장 자료형(byte)
    birth VARCHAR2(10) not null, -- null 값을 허용하지 않음
    tel VARCHAR2(20),
    PRIMARY key( name ) 
    -- num 이라는 값을 기본 키로 설정
    -- num 값의 중복을 허용하지 않음을 의미
    -- null 값을 등록할 수 없고 값이 무조건 채워져 있어야 한다
);

desc person111;

데이터 출력

 

 

17. person 테이블에 데이터 추가

insert into person values(1000,'홍길동','20230227',010-1111-2222);

데이터 추가된 것 확인

 

 

18. 테이블에 중복된 num 값으로 데이터 추가 시도

아까 테이블 생성할 때 num 값을 primary 키 값으로 중복을 허용하지 않았으므로 오류 발생

 

 

19. num 값을 변경한 뒤 정상적으로 데이터 추가 완료

 

 

20. 데이터를 추가하는 다른 방법

insert into person(num, name, birth, tel) values(1002,'성춘향','20191122',010-2222-3333);

각각의 추가될 컬럼 값을 순서대로 지정해 준 뒤 값을 적어서 데이터를 추가할 수도 있음

추가 성공

 

 

21. 데이터를 가지고 와서 출력

select * from person; -- person 테이블에 존재하는 모든 데이터를 출력
select num, name from person; -- person 테이블에 존재하는 num 과 name 의 데이터를 출력
select * from person where num = 1000; -- person 테이블로 부터 num 이 1000 과 같은 데이터의 모든 값을 출력
select * from person where name = '김기동'; -- person 테이블로 부터 name 이 김기동 과 같은 데이터의 모든 값을 출력

 

 

22. 현재 존재하는 테이블을 출력

select * from tab;

 

 

23. 테이블 삭제

delete from person; -- person 테이블 삭제
select * from person; -- 테이블이 삭제되어 출력되는 내용이 없음

 

 

24. 테이블의 데이터 삭제 및 변경

insert into person values(1000,'홍길동','20230227',01011112222);
insert into person values(1001,'김기동','20230222',01011113333);
insert into person(num, name, birth, tel) values(1002,'성춘향','20191122',01022223333);
-- 다시 삭제한 데이터 추가

delete from person where tel='01011113333'; -- person 테이블에서 전화번호가 01011113333 인 행 삭제
update person set name='김길이'; -- person 테이블의 모든 name 을 김길이 로 변경
update person set name='홍길동', birth='111' where num=1002; -- person 테이블의 num 이 1002 인 행의 이름을 홍길동, 생일을 111 로 변경

코드 실행 결과

 

 

25. developer 에서 작업할때는 모든 내용은 commit 이전에는 실제 DB 에 적용되지 않는다.

( 임시 저장소에서만 적용된다, 테이블을 생성하는 경우는 바로 적용된다 )

★ java 에서 작업할때는 자동으로 commit 이 진행된다 ★

commit; -- 커밋

delete from person; -- person 테이블의 모든 내용 삭제
select * from person;

rollback;
select * from person; -- 이전 커밋된 시점으로 되돌린다 ( person 테이블의 데이터가 commit 시점으로 복구됨 )

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

실습 예제

(pk) 는 primary key 를 의미

 

실습 풀이

create table STUDENT(
    id VARCHAR2(10),
    name varchar2(10),
    kor VARCHAR2(5),
    eng VARCHAR2(5),
    math VARCHAR2(5),
    primary key( id )
);

insert into student values ( '15012248', '개똥이', '80', '95', '98' );
insert into student values ( '18262266', '춘향이', '98', '96', '90' );
insert into student(id, name, kor, eng, math) values ( '22068272', '멍청이', '25', '46', '60' );

update student set kor='70', eng='80', math='92' where id='22068272';

commit;

delete from student;

rollback;

select * from student;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

26. 테이블 정보 변경 ( 컬럼 추가, 컬럼 자료형 변경, 컬럼 삭제, 컬럼 이름 변경, 테이블 삭제 )

create table test_table(num number); -- num 이라는 컬럼을 가진 테이블 생성
desc test_table; -- 테이블 정보 출력

alter table test_table add ( name varchar2(20) ); -- name 이라는 컬럼을 추가
desc test_table; -- 테이블 정보 출력

alter table test_table modify ( name number ); -- name 컬럼의 자료형을 number 로 변경
desc test_table; -- 테이블 정보 출력

alter table test_table drop column name; -- name 컬럼을 삭제
desc test_table; -- 테이블 정보 출력

alter table test_table rename column num to num_b; -- num 컬럼의 이름을 num_b 로 변경
desc test_table; -- 테이블 정보 출력

drop table test_table; -- test_table 이라는 table 삭제
desc test_table; -- 테이블 정보 출력

 

 

27. 연산

create table STUDENT(
    id VARCHAR2(10),
    name varchar2(10),
    kor VARCHAR2(5),
    eng VARCHAR2(5),
    math VARCHAR2(5),
    primary key( id )
);

insert into student values ( '15012248', '개똥이', '80', '95', '98' );
insert into student values ( '18262266', '춘향이', '98', '96', '90' );
insert into student(id, name, kor, eng, math) values ( '22068272', '멍청이', '25', '46', '60' );

select * from student where eng >= 90; -- 영어가 90 보다 크거나 같은 사용자를 출력
select * from student where kor != 100; -- 숫자가 문자 형태로 저장되었지만 숫자로 연산할 수 있다
select math from student where name='춘향이'; -- 90 출력
select math/2 from student where name='춘향이'; -- 45 출력
select math + 100 from student where name='춘향이'; -- 190 출력
select math * 100 from student where name='춘향이'; -- 9000 출력
select mod(math,3) from student where name='춘향이'; -- 3으로 나눈 나머지인 0 출력
select * from student where kor>=90 and math=90; -- and 연산도 사용가능, 춘향이 행 출력
select * from student where not kor>=90; -- 국어 점수가 90 미만인 사용자 출력

select * from student where kor>=90 and kor<=100; -- 국어가 90 이상 100 이하인 사용자 출력
select * from student where kor between 90 and 100; -- 국어가 90 이상 100 이하인 사용자 출력

and, or, not 

 

 

28. number 자료형 자릿 수 설정

create table test_number(
    num_ps number(5, 2), -- 총 5자리 중 소수점 2자리 ( 즉, 정수는 3자리 )
    num_p number(3), -- 정수 3자리
    num number
);
insert into test_number values (1.123456, 1.123456, 1.123456); -- 저장 가능한 범위까지 저장된다.
insert into test_number values (1234.123456, 1.123456, 1.123456); -- num_ps 에 저장 가능한 정수 범위를 넘어서 오류 발생
insert into test_number values (1.123456, 1234.123456, 1.123456); -- num_p 에 저장 가능한 정수 범위를 넘어서 오류 발생
select * from test_number; -- 1.12, 1, 1.123456 출력

 

 

 

29. 날짜 자료형 

create table test_date(my_date date); -- date 자료형 컬럼인 my_date 를 가진 test_date 테이블 생성
insert into test_date values(sysdate); -- sysdate : 현재 날짜를 얻어오는 것 ( 시, 분, 초 도 저장됨 )
insert into test_date values('2000/12/24'); -- 날짜 추가 형식 ( 년, 월, 일 만 저장됨 )

alter session set nls_date_format = 'YYYY/MM/DD HH:MI:SS'; -- 기존 형식은 년/월/일 로 출력되는데 시/분/초 까지 출력되게 저장
select * from test_date; -- 년/월/일 시/분/초 까지 출력됨

insert into test_date values ('2000/12/24 12:12:12'); -- 년, 월, 일, 시, 분, 초 까지 저장됨
select * from test_date where my_date < '2024/02/27'; -- 날짜로도 연산이 가능, 2024년 02월 27일 이전 데이터만 출력됨

 

 

30. char 자료형과 varchar 자료형의 차이

create table test_char(
    ch char(5),
    var varchar2(20)
); -- char 자료형과 varchar 자료형이 따로 존재한다

-- char 자료형은 고정 자료형
-- varchar 자료형은 가변 자료형

insert into test_char values('1', '1');
select lengthb(ch), lengthb(var) from test_char; -- char 자료형은 5바이트 크기 출력, varchar 자료형은 1바이트 크기 출력
-- 고정 자료형은 바이트의 크기가 고정
-- 가변 자료형은 최대 바이트 크기만 지정해놓으면 해당 크기 내에서 저장되는 자료형의 크기로 변환됨

 

 

31. 와일드 카드, 검색 필터 ( order by, desc, asc )

create table STUDENT(
    id VARCHAR2(10),
    name varchar2(10),
    kor VARCHAR2(5),
    eng VARCHAR2(5),
    math VARCHAR2(5),
    primary key( id )
);

insert into student values ( '15012248', '개똥이', '80', '95', '98' );
insert into student values ( '18262266', '춘향이', '98', '96', '90' );
insert into student(id, name, kor, eng, math) values ( '22068272', '멍청이', '25', '46', '60' );

select * from student;
insert into student values('4', '길고인', '10', '10', '10');
select * from student where name like '김%'; -- name 이 김으로 시작하는 모든 자료를 다 가져옴
select * from student where name like '%인'; -- name 이 인으로 끝나는 모든 자료를 다 가져옴
select * from student where name like '%이%'; -- name 에 이 라는 문자가 들어간 모든 자료를 다 가져옴

select * from student;
select * from student order by name desc; -- 이름을 기준으로 내림차순으로 데이터를 출력
select * from student order by name asc; -- 이름을 기준으로 오름차순으로 데이터를 출력

select * from student order by eng desc; -- 영어를 기준으로 내림차순으로 데이터를 출력

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

실습 예제

 

insert into employee values('설까치',1800000,'삼성','2017/10/24');
insert into employee values('로버트',1850000,'애플','2019/01/04');
insert into employee values('고도리',2200000,'엘지','2017/11/06');
insert into employee values('김개똥',2500000,'SK','2017/04/14');
insert into employee values('리우뚱',2410000,'샤오미','2018/01/09');
insert into employee values('강민',1900000,'삼성','2019/10/24');
insert into employee values('할리',1800000,'애플','2019/12/04');
insert into employee values('심심해',4630000,'엘지','2015/04/02');
insert into employee values('놀아줘',2770000,'SK','2017/01/24');
insert into employee values('왕만두',3650000,'샤오미','2016/08/04');
insert into employee values('머리빨',4210000,'삼성','2015/03/18');
insert into employee values('마리오',2720000,'애플','2017/01/04');
insert into employee values('최치우',4320000,'엘지','2015/06/07');
insert into employee values('안깔쌈',3490000,'SK','2015/09/07');
insert into employee values('끝짱',2200000,'샤오미','2017/05/04');
insert into employee values('막장',1920000,'삼성','2018/11/24');
insert into employee values('드라마',3420000,'애플','2016/07/29');
insert into employee values('개똥이',1800000,'엘지','2018/12/24');
insert into employee values('마포구',2230000,'SK','2018/01/05');
insert into employee values('소고기',1800000,'샤오미','2019/01/09');
insert into employee values('짜장면',2100000,'삼성','2017/10/24');
insert into employee values('소곱창',2200000,'애플','2017/11/04');
insert into employee values('참이슬',1950000,'엘지','2017/10/24');
insert into employee values('뤼우뚱',1800000,'SK','2018/10/24');
insert into employee values('위메프',1800000,'샤오미','2019/12/07');
insert into employee values('북경시',1880000,'삼성','2018/11/14');
insert into employee values('스미스',1970000,'애플','2019/06/04');
insert into employee values('핸드폰',7200000,'엘지','2010/01/27');
insert into employee values('빅스비',3570000,'SK','2015/02/17');
insert into employee values('노라줘',3200000,'샤오미','2015/11/24');
insert into employee values('사이다',2400000,'삼성','2017/09/26');
insert into employee values('김말이',7000000,'애플','2010/01/21');
insert into employee values('오도독',6230000,'엘지','2011/08/19');

insert into employee values('쌈지돈',3710000,'SK','2015/08/19');
insert into employee values('화장지',1770000,'샤오미','2019/04/28');
insert into employee values('소화기',1920000,'삼성','2019/10/07');
insert into employee values('박효신',2670000,'애플','2017/11/24');
insert into employee values('판빙빙',3120000,'엘지','2016/05/19');
insert into employee values('김재욱',4190000,'SK','2015/01/02');
insert into employee values('송혜교',4280000,'샤오미','2015/01/02');
insert into employee values('송중기',3700000,'삼성','2016/02/17');
insert into employee values('손홍민',2220000,'애플','2018/02/04');
insert into employee values('백종원',2760000,'엘지','2017/10/14');
insert into employee values('오창석',2620000,'SK','2017/09/04');
insert into employee values('스텔라',2500000,'샤오미','2017/11/20');
insert into employee values('멕스웰',1970000,'삼성','2017/10/30');
insert into employee values('조현',  2720000,'애플','2018/11/11');
insert into employee values('류현진',2600000,'엘지','2015/11/19');
insert into employee values('은지원',5670000,'SK','2017/10/16');
insert into employee values('전효성',3750000,'샤오미','2015/09/15');
insert into employee values('이채은',3400000,'삼성','2016/02/09');
insert into employee values('최고봉',8900000,'애플','2010/01/04');
insert into employee values('광화문',1860000,'엘지','2017/10/24');
insert into employee values('동대문',1790000,'SK','2017/10/24');
insert into employee values('서대문',2880000,'샤오미','2016/02/27');
insert into employee values('대통령',2320000,'삼성','2016/05/24');
insert into employee values('예지원',1780000,'애플','2019/01/09');
insert into employee values('정소민',2900000,'엘지','2016/10/22');
insert into employee values('워너원',3000000,'SK','2015/10/10');
insert into employee values('북한군',3200000,'샤오미','2015/11/11');
insert into employee values('남한군',2500000,'삼성','2016/12/19');
insert into employee values('짜투리',1850000,'애플','2018/04/03');
insert into employee values('이승기',1900000,'엘지','2018/01/01');
insert into employee values('기차길',1790000,'SK','2018/05/02');
insert into employee values('길거리',2700000,'샤오미','2016/07/20');

데이터 미리 등록하고 시작

 

 

예제 풀이1

create table employee(
    name  VARCHAR2(15),
    salary  number,
    job  varchar2(20),
    join_company  date
);

insert into employee values('홍길동', 2000000, '컴퓨터', '2222/12/24');
alter session set nls_date_format = 'YYYY/MM/DD';
select * from employee;

테이블 생성한 뒤 미리 집어 넣을 모든 데이터를 저장

 

 

예제 풀이 2

delete from employee; -- 홍길동 이름을 가진 데이터 삭제
select * from employee;

-- 이후 데이터 모두 복사 붙여넣기해서 저장 --

select * from employee where salary*12 > 100000000; -- 연봉이 1억이 넘는 사람 출력
select name, join_company from employee where join_company < '2014/12/31'; -- 2015년도 이전에 입사한 사람의 이름과 입사년도 출력
select * from employee where salary between 2800000 and 3000000; -- 월급이 280만원 ~ 300만원인 사람 출력

 

 

예제 풀이 3

select * from employee where join_company>= '2015/01/01' and salary*12 > 60000000; -- 입사년도가 2015년 이상이며 연봉이 6000만원 이상인 사람들을 출력
select * from employee where (job = '엘지' or job =  '삼성') and salary*12 > 50000000; -- 회사가 삼성이거나 엘지이며 연봉이 5000만원 이상인 사람들을 출력

 

 

예제 풀이 4

select * from employee where name like '%김%' and salary*12 >= 30000000 order by job asc, salary desc;
-- 회사는 오름 차순으로 정렬하고 연봉은 내림 차순으로 정렬하고 이름에 '김'이 들어가며 연봉은 3000만원 이상인 모든 내용을 출력하시오

 

 

 

 

 

 

 

 

 

 

실습 예제

 

insert into test_func values(3,'77');
insert into test_func values(4,'67.123');
insert into test_func values(5,'123.123');
insert into test_func values(6,'99');
insert into test_func values(7,'99.456');
insert into test_func values(8,'128');
insert into test_func values(9,'123.777');
insert into test_func values(10,'101.44');

select * from test_func where mod(id,2)=1;
select id, round(num/2,2) from test_func where mod(id,2)=0;
728x90