Oracle day 05

유요한·2024년 7월 1일
0

DataBase(MySQL & Oracle)

목록 보기
13/17

컬럼추가

alter table 테이블 이름 add 컬럼이름 자료형;


데이터 정의어(DDL)

  • 생성 : create
  • 수정 : alter
  • 삭제 : drop
💡 DDL과 DCL은 자동 커밋이 되기 때문에 롤백을 할 수 없다.

제약종류

  • null : null을 허용할 수 있다. (기본값)
  • not null : null을 허용 안함
    create table newBook(
        bookid number not null,
        bookname varchar2(20) not null,
        publisher varchar2(20),
        price number
    );
    // ORA-01400 오류 발생 
    // 원인 not null안에 null을 넣으려고 해서 발생하는 오류
    insert into newBook values(1, null, '코스타미디어', 30000);
    insert into newBook values(null, '신나는 자바', null, null);
  • unqiue : 유일해야 한다.값의 중복을 허용하지 않는다. 즉, 유일값이다. 하지만 null은 허용한다. 단지 중복만 확인하는 것이다.
       // unique 제약
       drop table newBook;
       create table newBook(
           bookid number unique,
           bookname varchar2(20) not null,
           publisher varchar2(20),
           price number
       );
       insert into newBook values(1, '재밌는 자바', '코스타미디어', 30000);
       // ORA-00001: 무결성 제약 조건(C##MADANG.SYS_C008366)에 위배됩니다
       // bookid는 유일값인데 중복된 값을 넣어서 에러 발생
       insert into newBook values(1, '신나는 자바', null, null);
  • defalut : 생략하면 설정된 기본값을 사용 기본값을 줘서 데이터가 안들어올 때 그 값을 설정해준다.
    create table newBook(
        bookid number primary key,
        bookname varchar2(20) not null,
        publisher varchar2(20) default '코스타미디어',
        price number
    );
    insert into newBook values(1, '재미있는 자바', '한빛미디어', 5000);
    insert into newBook values(2, '신나는 자바', '코스타미디어', 5000);
    insert into newBook (bookid, bookname) values(3, '재미있는 자바');
  • check : 들어갈 수 있는 값의 조건을 설정
    컬럼의 값에 대하여 조건식을 설정하고자 할 때 사용
    create table newBook(
        bookid number primary key,
        bookname varchar2(20) not null,
        publisher varchar2(20) default '코스타미디어',
        price number check (price >= 1000)
    );
    insert into newBook values(1, '재미있는 자바', '한빛미디어', 5000);
    // ORA-02290: 체크 제약조건(C##MADANG.SYS_C008384)이 위배되었습니다
    insert into newBook values(2, '재미있는 자바', '한빛미디어', 900);
  • primary key : 주식별자 설정
    not null + unique
    다른 레코드와 구별하기 위하여 식별자를 설정하기 위하여 사용
    create table newBook(
        bookid number primary key,
        bookname varchar2(20) not null,
        publisher varchar2(20),
        price number
    );
    insert into newBook values(1, '재밌는 자바', '코스타미디어', 30000);
    // ORA-00001: 무결성 제약 조건(C##MADANG.SYS_C008366)에 위배됩니다
    insert into newBook values(1, '재밌는 공부', '코스타미디어', 30000);
    // ORA-01400 오류 발생 
    insert into newBook values(null, '재밌는 자바', '코스타미디어', 30000);
  • foregin key : 참조키를 설정 💡 부모와 자식의 관계를 설정

데이터 추가

  • insert into 테이블 values(값1, 값2, …);

    값의 수와 순서는 테이블의 구조와 동일해야 한다.

  • insert into 테이블 (컬럼1, 컬럼2, …) values(값1, 값2, …);

    나열한 컬럼대로 맞춰야 한다.

테이블 삭제

drop table 테이블이름;

profile
발전하기 위한 공부

0개의 댓글