[DataBase] SQL

2 minute read

[DataBase] SQL

SQL Data Definition


1. Data-definition language (DDL)

  • Defining relation schema
  • Deleting relation

  • Modifying relation schema

C++ vs. SQL


  • C++

    struct department {
          char dept_name [20];      
          char building[15];
          int    budget;
    };
    
  • SQL

     create table department(
         dept_name  char(20),
         building   char(20),     
         budget     int
    };
    

Basic Types in SQL(1)


  • char(n).
    • 사용자 지정 길이가 n인 고정 길이 문자열
  • varchar(n).
    • 사용자 지정 최대 길이가 n인 가변 길이 문자열
  • int.
    • 정수 (기계 종속적인 정수의 유한 서브 세트)
  • smallint.
    • 작은 정수(정수 도메인 유형의 기계 종속 서브 세트)
  • numeric(d, p).
    • 사용자 지정 정밀도 p 자리, d 자리의 고정 소수점 번호
      • 예) numeric (3, 1)
      • numeric(전체길이(소수점 이하 포함), 소수점 이하 길이)
  • real, double precision
    • 기계 종속 정밀도를 갖는 부동 소수점 및 배정 밀도 부동 소수점 숫자

Char vs. Varchar

  • char(10)

    char

  • varchar(10)

    varchar

Create Table Construct


  • SQL 관계는 create table 명령을 사용하여 정의됩니다.

    create table r (A1 D1, -> attributes

    ​ A2 D2,

    ​ . …………,

    ​ (integrity-constraint1), -> integrity constraints

    ​ … (무결성 제약 조건)

    ​ (integrity-constraintk));

    • r은 관계(relation)의 이름
    • 각 Ai 는 관계 r의 스키마에 있는 속성 이름
    • Di는 속성 Ai의 도메인에 있는 값의 데이터 유형
  • 예시)

    create table instructor (

    ID char(5),

    name varchar(20),

    dept_name varchar(20),

    salary numeric(8,2));

Insert Statement


  • insert into instructor values (‘10211’, ‘Smith’, ‘Biology’, 66000);
  • insert into instructor values (‘10212’, ‘Kim’, ‘Biology’, 76000);

Alter Table


  • alter table r add A D : 컬럼 추가
    • A는 관계 r에 추가할 속성의 이름
    • D는 A의 도메인
    • 관계의 모든 튜플에는 새 속성의 값으로 null이 할당됩니다.
    • 예) alter table instructor add monthly_salary numeric(8,2)
  • alter table r drop A : 컬럼 삭제
    • A는 관계 r의 속성의 이름
    • 많은 데이터베이스에서 지원하지않는 속성 삭제
    • 예) alter table instructor drop salary
  • alter table r modify A D DEFAULT [NOT NULL] : 컬럼 수정
    • A는 관계 r의 속성의 이름
    • D는 A의 도메인
    • DEFAULT : 디폴트 값
    • NOT NULL : NOT NULL 제약 조건
  • alter table r rename column A To N : 테이블 생성하면서 만들어졌던 컬럼명 변경
    • A는 변경해야할 컬럼명
    • N은 새로운 컬럼명
  • alter table r add constraint A I(컬럼명) : 생성 이후 제약조건 추가
    • A는 제약조건명
    • I는 제약 조건
  • alter table r drop constraint A : 테이블 생성 시 부여했던 제약조건 삭제
    • A는 제약 조건명

Drop Table


  • drop table r
    • r의 모든 튜플 및 스키마 삭제
  • delete from r
    • r의 모든 튜플을 삭제하고 관계 r을 유지합니다.

Integrity Constraints


create table instructor (
     ID        char(5),
     name      varchar(20) not null,
     dept_name varchar(20),
     salary    numeric(8,2),
     primary key (ID),
     foreign key (dept_name) references department(dept_name));
  • not null

    • 해당 속성에는 null 값이 허용 되지않습니다.
    • insert into instructor values (‘10212’, null, ’Biology’, 66000); -> ERROR
  • primary key (A1, …, An )

    • 속성에 대한 기본 키 선언은 null이 아니고 고유하지않도록 합니다

    • insert into instructor values (null, ‘Smith’, ’Biology’, 66000); -> ERROR

      insert into instructor values (‘10211’, ‘Kim’, ’Physics’, 91000); -> ERROR

    • create table instructor (
           ID        char(5) PRIMARY KEY,
           name      varchar(20) not null,
           dept_name varchar(20),
           salary    numeric(8,2),
           foreign key (dept_name) references department(dept_name));
      
  • foreign key (Am, …, An ) references r

    foreignkey

    • insert into instructor values (‘99999’, ’JW KIM’, ’Industrial Engineering’, 65000);

      -> ‘Industrial Engineering’이 department relation에 존재해야합니다.

      -> department relation에 없으면 Error


Leave a comment