Skip to main content

2 Data Models

Logical data independence: Maintain views when logical structure changes

Phisical data independence: Maintain logical structure when phisical structure changes

20210506164120

ER Model

Key constraints​

arrow: 可以divided(从尾到头at most one)

graph RL id1[Departments]-->id2{Manages}; subgraph 1 *did*---id1; dname---id1; budget---id1; end id2---id3[Employees]; subgraph 2 *ssn*---id3; name---id3; lot---id3; end
create table manages(
ssn char(11),
did integer,
primary key (did),
foreign key (ssn) references Employees,
foreign key (did) references Departments
)

Participation constraints​

粗线: Total participation(从尾到头at least one)

故以下从尾到头exactly one

graph RL id1[Departments]==>id2{Manages}; subgraph 1 *did*---id1; dname---id1; budget---id1; end id2---id3[Employees]; subgraph 2 *ssn*---id3; name---id3; lot---id3; end
create table manages(
did integer,
dname char(20),
budget real,
ssn char(11) not null,
primary key(did),
foreign key (ssn) references Employees on delete no action
)

Weak Entity Set​

An entity set that does not have a primary key

20210512170303

create table Dep_Policy(
pname char(20),
age integer,
cost real,
ssn char(11) not null,
primary key (pname,ssn),
foreign key (ssn) references employees on delete cascade
)

Functional Dependencies

Definition​

Let RR be a relation scheme

let X⊆RX \subseteq R and Y⊆RY \subseteq R.

We say that a relation instance r(R)r(R) satisfies a functional dependency X→YX \rightarrow Y if for every pair of tuples t1∈rt1 \in r and t2∈rt2 \in r, if t1[X]=t2[X]t1[X] = t2[X] then t1[Y]=t2[Y]t1[Y] = t2[Y].

ArmStrong's Axioms​

Reflexivity: If X⊇YX \supseteq Y, then X→YX \rightarrow Y(trivial FD)

Augmentation: If X→YX \rightarrow Y, then XZ→YZXZ \rightarrow YZ for any Z.

Transitivity: If X→YX \rightarrow Y and Y→ZY \rightarrow Z, then X→ZX \rightarrow Z

Union: If X→YX \rightarrow Y and X→ZX \rightarrow Z, then X→YZX \rightarrow YZ

Decomposition: If X→YZX \rightarrow YZ, then X→YX \rightarrow Y and X→ZX \rightarrow Z

Keys​

  • A superkey is a set of attributes A1,...,AnA_1, ..., A_n s.t.

    for any other attribute B, we have A1,...,An→BA_1, ..., A_n \rightarrow B

  • A candidate key (or sometimes just key) is a minimal superkey