Part 3. The Relational Data Model and SQL

Book_Chapter 6 Basic SQL

  1. SQL Data Definition and Data Types
  2. Specifying Constraints in SQL
  3. Basic Retrieval Queries in SQL
  4. INSERT, DELETE, and UPDATE Statements in SQL
  5. Additional Features of SQL
  6. Summary

1. SQL Data Definition and Data Types

  • SQL(Structured Query Language) : Data Definition in SQL
    relation->table / tuple-> row / attribute->column
  • Schema and Catalog concepts
    - SQL Schema: 스키마 이름으로 식별되고, 스키마 authorization identifier와 각 요소에 대한 descriptor를 갖는다.
    - Create Schema <SCHEMA name> Authorization <USER NAME>
  • Catalog : named collection of schema
    - 카탈로그는 카탈로그에 있는 모든 스키마에 대한 정보와, 스키마의 descripter를 제공하는 information_schema를 포함한다.
    - 카탈로그들이 생성되면 자료사전에 저장되기 때문에 자료 사전이라고 부르기도 함.
    - 카탈로그에 저장된 정보를 meta data라고 한다.
  •  Create table
    - create table <table이름>.<schema이름> ( <column name>, <data type>,<constraints> )
    - Data type
    (1) numeric: integer, real, formatted numberedecimal(소수점 전 digit 수, 소수점 이후 digit 수) 
    (2) character(String data type): char(n) - 고정길이, varchar(n) - 가변길이
    (3) bit-string data type: bit(n)-고정, bit varying(n)- 가정
    (4) date data type: YYYY-MM-DD(유효값만 인정)
    (5) time data type: HH:MM:SS
    (6) timestamp data type: date + time+ time qualifier
    (7) interval data type: time interval
  • DROP
    - DROP SCHEMA <schema name> CASCADE: 스키마와 관련된 모든 테이블, 도메인, 요소들을 삭제
    - DROP SCHEMA <schema name> RESTRICT: 스키마의 element가 없는 경우 스키마를 삭제
    - DROP TABLE <relatioion name> CASCADE: 릴레이션과 해당 릴레이션을 참조하는 릴레이션을 삭제
    - DROP TABLE <relatioion name> RESTRICT: 릴레이션이 다른 constraints(foreign key/ view)에 참조되지 않을 경우 삭제
  • ALTER
    - add an attribute: ALTER TABLE <table>.<schema> ADD <attribute>;
    - drop an attribute(cascade, restricted) : ALTER TABLE <table>.<schema> DROP <attribute> CASCADE;
    - drop and add default value: ALTER TABLE <table>.<schema> ALTER <attribute> DROP DEFAULT;
                                           ALTER TABLE <table>.<schema> ALTER <attribute> SET DEFAULT "value";
    - drop named constraint: ALTER TABLE <table>.<schema> DROP CONSTRAINTS <attribute> CASCADE;

2. Specifying Constraints in SQL

  • SQL(Structured Query Language) : Data Definition in SQL
    - Referential Integrity in SQL
    (1) No Action: update나 delete를 거절
    (2) Casacade: 참조하는 모든 튜플을 삭제
    (3) Set Null/Set Default: 참조 튜플의 값을 null이나 default로 설정
    - Primary Key/Foreign Key + References <Table>
  • Constraints
    (1) 속성 데이터형 정의: create domain ssn_Type as char(9);
    (2) not null constraint: key constraint or null is not allowed.
    (3) 기본 값 정의: default < value>
    (4) base table &  virtual table
    - base table은 create table로 선언된 relation을 의미. virtual table은 create view로 선언된 relation

3. Queries SQL

  • Basics : SELECT<attribute list> FROM <table list> WHERE <condition>;
    <attribute list> : 질의 결과로 나타나는 속성 이름들
    <table list> : operand tables
    <condition> : search condition(Boolean search expressions)
  • Aliasing : 서로 다른 테이블의 속성 이름이 같은 경우
    (1) qualified name을 사용
    (2) join할 때, table name을 새롭게 설정
  • Table as Set
    - DISTINCT : relation=서로 다른 레코드의 집합 vs table=list(duplicated tuples)
    - Set operation(union compatible, no duplicate): UNION, INTERSECT, EXCEPT...
  • Like
    - % : 연속된 임의의 character. 부분 string match
    -  _ :  single character
  • Order by <attribute>desc/asc
  • Nested Queries: 쿼리 안에 쿼리가 존재하는 경우.
    - IN이나 =가 사용되었을 경우 nested Queries는 단일 select 문으로 표현될 수 있다.
    - name confliction on unqualified name: qualified name을 사용할 때, inner query에서 어떤 query의 속성을 사용하는지 확인이 용이함
  •  IN operator: compare a value v(튜플) with a set of value V(집합) ->boolean값을 반환
  • ALL(v >ALL V): tuple v가 집합 V의 모든 값보다 클 경우 true
  • EXIST: correlate된 nested query가 비어있으면 true, 아니면 false를 리턴
  • UNIQUE: 쿼리의 결과에 중복하는 튜플이 존재하지 않을때(DISTINCT)
  • CONTAINS(=divide-by): sql92의 표준에 포함이 되어 있지 않음. FOR ALL-> EXCEPTION, NOT EMPTY 확인
  • built-in function(count, sum, max, min, avg...): Select문이나 having절에서 사용 가능
  • Having clause & group by
    - group by: 그룹별로 집계하는 조건
    - having: group에 대한 선택 조건. group by가 항상 필요함.

4. INSERT, DELETE, and UPDATE Statements in SQL

  • INSERT INTO <table name> values (tuple value), (tuple value)....
  • DELETE FROM <table name> WHERE <condition>
    - where 조건이 없으면 table의 모든 tuple을 삭제한다.
  • UPDATE <table name> SET <attribute name> = <new value>,... WHERE <condition>

참고문헌 : Fundamentals of Database Systems 7th Edition

+ Recent posts