数据库 关系模型02 英文

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Students
sid 53666 53688 53650 name login Jones jones@cs Smith smith@eecs Smith smith@math age 18 18 19 gpa 3.4 3.2 3.8
6
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
2
பைடு நூலகம்
Primary Key Constraints (Cont.)
CREATE TABLE Students (sid CHAR(20), name CHAR(30), login CHAR(20), age INTEGER, gpa REAL, UNIQUE (login, age), CONSTRAINT StudentsKey PRIMARY KEY (sid))
5
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Foreign Keys in SQL

Only students listed in the Students relation should be allowed to enroll for courses.
Deletion does not cause a violation of domain, PRIMARY KEY, or UNIQUE constraints. However, an update can cause violations, similar to an insertion (for a single table without pointers on it). For foreign key violations:
Specifying Foreign Key Constraints in SQL
CREATE TABLE Enrolled (studid CHAR(20), cid CHAR(20), grade CHAR(10), PRIMARY KEY (studid, cid), FOREIGN KEY (studid) REFERENCES Students)

Avoids data entry errors, too!
1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Primary Key Constraints

A set of fields is a (candidate) key for a relation if :
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
10
Transactions and Constraints
CREATE TABLE Students (sid CHAR(20), name CHAR(20), CHAR(20) NOT NULL, honors PRIMARY KEY (sid), FOREIGN KEY (honors) REFERENCES Courses (cid)) CREATE TABLE Courses (cid CHAR(20), CHAR(20), cname grader CHAR(20) NOT NULL, PRIMARY KEY (cid), FOREIGN KEY (grader) REFERENCES Students (sid))

Similar if primary key of Students tuple is updated.
8
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Enforcing Referential Integrity (Cont.)

A legal instance of a relation is one that satisfies all specified ICs.

DBMS should not allow illegal instances.

If the DBMS checks ICs, stored data is more faithful to real-world meaning.


Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
4
Foreign Keys, Referential Integrity
Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) Like a `logical pointer’. E.g. sid is a foreign key referring to Students:
“For a given student and course, CREATE TABLE Enrolled (sid CHAR(20) there is a single grade.” vs. cid CHAR(20), “Students can take only one grade CHAR(2), course, and receive a single grade PRIMARY KEY (sid,cid) ) for that course; further, no two CREATE TABLE Enrolled students in a course receive the (sid CHAR(20) same grade.” cid CHAR(20), Used carelessly, an IC can prevent grade CHAR(2), the storage of database instances PRIMARY KEY (sid), that arise in practice! UNIQUE (cid, grade) )
The same attribute name is not required.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
7
Enforcing Referential Integrity
Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students. What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!) What should be done if a Students tuple is deleted?

Deletions of Enrolled tuples do not violate referential integrity, but insertions could. Insertions of Students tuples do not violate referential integrity, but deletions could.
CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students )
Enrolled
sid 53666 53666 53650 53666 cid grade Carnatic101 C Reggae203 B Topology112 A History105 B
Integrity Constraints (ICs)

IC: condition that must be true for any instance of the database; e.g., domain constraints.

ICs are specified when schema is defined. ICs are checked when relations are modified.


Enrolled (sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references. Can you name a data model w/o referential integrity? Links in HTML – 404 Error
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
3
Primary and Candidate Keys in SQL

Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
9
Referential Integrity in SQL

SQL supports all 4 options CREATE TABLE Enrolled on deletes and updates. (sid CHAR(20), cid CHAR(20), Default is NO ACTION grade CHAR(2), (delete/update is rejected) PRIMARY KEY (sid,cid), CASCADE (also delete FOREIGN KEY (sid) all tuples that refer to REFERENCES Students deleted tuple) ON DELETE CASCADE SET NULL / SET DEFAULT ON UPDATE CASCADE ) (sets foreign key value of referencing tuple)


Also delete all Enrolled tuples that refer to it. Disallow deletion of a Students tuple that is referred to. Set sid in Enrolled tuples that refer to it to a default sid. (In SQL, also: set an attribute to a special value null, denoting `unknown’ or `inapplicable’.)
1. No two distinct tuples can have same values in all key fields, and 2. This is not true for any subset of the key. Part 2 false? A superkey. If there’s >1 key for a relation, one of the keys is chosen (by DBA) to be the primary key.
相关文档
最新文档