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

Part 2. Conceptual Data Modeling and Database Design

Book_Chapter 4 The Enhanced Entity-Relationship(EER) Model

  1. Subclasses, Superclasses, and Inheritance
  2. Specialization and Generalization
  3. Constraints and Characteristics of Specialization and Generalization Hierarchies
  4. Modeling of UNION Types Using Categories
  5. A Sample UNIVERSITY EER Schema, Design Choices, and Formal Definitions
  6. Example of Other Notation :Representing Specializaiton and Generalization in UML Class Diagrams
  7. Data Abstaraction, Knowledge Representation, and Ontology Concepts
  8. Summary

1. Subclasses, Superclasses, and Inheritance

2. Specialization and Generalization

  •  용어 정의 
    용어 정의
    Entity type(=class) 같은 속성을 가진 개체의 집합
    class 개체의 집합
    Subclass super class에 포함되는 entity의 부분 집합 super class와 subclass를
    IS-A relation ship으로 표현
    Superclass subclass의 본체
    Inheritance superclass의 속성을 subclass가 갖게 되는 것을 의미
    Specialization entity type(super class)의 subclass를 정의하는 프로세스.
    super class를 specialization하면 superclass의 속성을 subclass가 상속받는다.
    Generalization 여러 entity type에서 공통 기능을 식별하여 단일 superclass로 만드는 프로세스.
    subclass를 generalization하면 superclass가 된다.
  • IS-A Hierarchies를 사용하는 이유 
    - subclss에 특정한 descriptive 속성을 추가하기 위해서
    - relationship에 참여하는 entity를 식별하기 위해서
  • Subclassses in data modeling
    - model specific attributes : subclass는 superclass의 특성을 상속받고, 추가적인 속성을 갖는다. 
    - model specific relationship on subclass

3. Constraints and Characteristics of Specialization and Generalization Hierarchies

  • predicate-defined/attribute-defined
    - subclass S의 membership이 같은 조건으로 정의되는 속성을 supclass C의 추가 속성으로 만들었을 때, Subclass S의 defining predicate으로 표현한다. S=C[P]
    - 모든 specialize된 subclass가 superclass의 공통 속성으로 membership condition을 가질 때, specialization은 그 자체로 attributed-defined specialization으로 정의되고, 그 속성은 defining attribute라고 부른다.
  • user-defined
    -subclass에서 membership을 결정하는 조건이 없을 경우 subclass는 사용자가 subclass에 entity를 추가하는 작업을 수행하고, user-defined subclass가 된다. membership은 자동으로 결정되는 것이 아니라 entity에 따라 별개로 specify된다.
    - diagram에서는 circle이 없는 형태로 그려진다.
  • disjoint / overlapping / total / partial
    (1) disjoint constraints
     - 다른 subclass와 공통의 entity가 없는 경우 disjoint라고 한다.
     - attribute-defined이면서 단일 값 속성을 갖는 경우 disjoint subclass이다.
     - diagram에서는 원 안의 d로 표현한다.
    (2) overlap
     - 하나의 entity가 2개 이상의 subclass에 동시에 포함되어 있는 경우 overlap이라고 한다.
     - diagram에서는 원 안의 o로 표현한다.
    (3) total specialization
     - super class의 모든 entity가 특정 subclass의 member가 되는 경우를 의미한다.
     - diagram에서는 ==(double line)으로 표현한다.
    (4) partial specialization
     - 전체 참여의 반대. super class의 entity의 일부반 subclass의 member가 되는 경우
  • insertion & deletion rules for specialization / generalization
    - superclass의 entity를 삭제할 경우, subclass에서도 삭제된다.
    - superclass에 entity를 추가할 경우, predicate-defined subclass에도 추가된다.
    - total specialization의 superclass에 entity를 추가할 경우, 적어도 한개 이상의subclasss에 추가된다.
  • Specialization hierarchies & lattices
    - hierarchy : single inheritance(in java)
    - lattice : multiple inheritance(in cpp..)
  • Top-down || Bottom-up conceptual design
    - Top-down : successive specialization
    - Bottom-up : successive generalization
    - and combination of two 

4. Modeling of UNION Types Using Categories

  • Union type(=category)
    - 사용자가 생성한 가상의 class. 다른 entity 개체로부터 임의로 entity collection을 만들고 이를 union이라고 부른다.

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

+ Recent posts