SQL/Oracle

[Oracle] Oracle student 테이블 생성

에띠 2022. 6. 12. 16:03
728x90

테이블 생성

  create table 테이블명 (

    컬럼명 데이터타입 제약조건,

    ...

);

create table tb_student(
    s_idx number(5) not null, -- 인덱스
    s_name varchar2(20) not null, -- 이름
    s_age number(3) not null, -- 나이
    s_gender char(1) not null, -- 성별
    s_hp varchar(20), -- 연락처
    constraint s_gender check(s_gender in ('m', 'w')) -- check 제약조건 'm', 'w'만 삽입가능
);

 

데이터 조회

select * from 테이블명 // *; : 전체 데이터 조회

select 컬럼명1, 컬럼명2, ... from 테이블명;

select * from tb_student;

 

테이블 삭제

drop table 테이블명;

drop table tb_student;

 

컬럼 추가

alter table 테이블명 add(

    컬럼명 데이터타입 [제약조건]

);

alter table tb_student add(
    s_email varchar2(100)
);

 

제약조건 삭제

alter table 테이블명 drop constraint 컬럼명;

alter table tb_student drop constraint s_gender;

 

컬럼 수정

alter table 테이블명 modify(

    컬럼명 데이터타입 제약조건

);

alter table tb_student modify(
    s_gender varchar2(10) check(s_gender in ('male', 'female'))
);

 

테이블 이름 변경

alter table 원래 테이블명 rename to 변경할 테이블명;

alter table tb_student rename to tb_customer;
alter table tb_customer rename to tb_student;

 

컬럼 이름 변경
alter table 테이블명 rename column 원래 컬럼명 to 변경할 컬럼명;

alter table tb_student rename column s_hp to s_tel;

 

데이터 삽입
insert into 테이블명(컬럼명1, 컬럼명2, 컬럼명3, 컬럼명4, 컬럼명5) values (값1, 값2, 값3, 값4, 값5);

insert into tb_student(s_idx, s_name, s_age, s_gender, s_tel) values (1, '김사과', 20, 'female', '010-1111-1111');

 

데이터 수정
update 테이블명 set 수정할 데이터의 컬럼명 = 수정할 데이터 where 수정할 데이터의 컬럼명  = 수정할 데이터의 컬럼의 값;

-- s_idx의 값이 1인 사람의 s_tel 변경

update tb_student set s_tel = '010-2222-2222' where s_idx = 1;



데이터 삭제
delete from 테이블명 where 수정할 데이터의 컬럼명  = 수정할 데이터의 컬럼의 값;

-- s_idx의 값이 1인 사람의 데이터 삭제

delete from tb_student where s_idx = 1;
728x90