oracle-table과column

ezzange·2022년 9월 16일
0

oracle

목록 보기
1/5
post-thumbnail

행 읽기

1.select 문의 기본 형식

테이블 생성

-->create table 테이블이름 ( 컬럼이름 컬럼데이터타입 null허용여부 );

drop table topic;
create table topic (
  id NUMBER not null,
  title VARCHAR2(40) not null,--가변형 데이터에는 VARCHAR2
  description VARCHAR2(4000), --VARCHAR2최대 4000byte
  created date not null
);

행 추가

--> insert into 테이블이름 (칼럼1, 칼럼2, ...) VALUES (칼럼순서에 맞는 데이터 값);

insert into topic
    (id,title,description,created)
    VALUES
    (1,'oracle','oracle is ...',sysdate);
insert into topic
    (id,title,description,created)
    VALUES
    (2,'mysql','mysql is ...',sysdate);
insert into topic
    (id,title,description,created)
    VALUES
    (3,'sql server','sql server is ...',sysdate);
insert into topic
    (id,title,description,created)
    VALUES
    (4,'mongoDB','mongoDB is ...',sysdate);

2.행과 컬럼 제한하기

테이블 출력

-->select *(모든 컬럼) from 테이블 이름

	SELECT * from topic;
	-->select 컬럼이름 from 테이블 이름
	select id from topic;
	-->id가 1인 행만 출력 
	select * from topic where id =1;
	-->id가 1보다 큰 행만 출력 
	select * from topic where id >1;
	-->id가 1인 행에서 id, title, created만 출력
	select id, title, created from where id=1;

3.정렬과 페이징

order by를 통해 정렬

-->select * from 테이블이름 order by 칼럼 정렬순;

	-->id를 기준으로 큰 수 먼저 정렬
		select * from topic order by id desc;
		-->id를 기준으로 작은 수 먼저 정렬
		select * from topic order by id asc;

페이징 : 원하는 행만 가져오기

-->offset n rows : n+1번째 행부터 출력 또는 n번째 인덱스 번호로 생각 (어디서부터 가져올것인가.)

	select * from topic 
    offset 1 rows;
	select * from topic offset 2 rows; 
	select * from topic 
   	offset 0 rows --> 모든행을 다 가져오겠다.
  	fetch next 1 rows only;--> fetch next 1 rows only : n개만 가져오겠다.
	select * from topic offset 1 rows fetch next 1 rows only; 
	COMMIT;    

4. 수정 및 삭제

행 수정

--> update 테이블이름 set 수정칼럼 = 수정할칼럼내용 where 수정할행의칼럼;

	update topic 
   	 set title = 'mssql', description = 'mssql is ...'
   	 where id=3;
	COMMIT; 

행 삭제

--> delete from 테이블이름 where 삭제를 원하는 행의 위치

	delete from topic where id=3;
	COMMIT; 

0개의 댓글