付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、The Relational ModelClass 2Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)Why Study the Relational Model? Most widely used model in Commercial DBMSs: Vendors: IBM, Informix, Microsoft, Oracle, Sybase“Legacy systems” in old
2、er models E.G., IBMs IMSCompetitorsObject-oriented model e.g., O2, but also Oracle, DB2XML Modele.g. Natix, but also IBM, OracleKey-Value Stores/NonSQLe.g. BigTable, MongoDB, etc. Relational Database: DefinitionsRelational database: a set of relationsRelation: made up of 2 parts:Instance : a table,
3、with rows (tuples) and columns (attributes). #Rows = cardinality, #attributes = degree / arity.Schema : specifies name of relation, plus name and type of each column.E.G. Students(sid: string, name: string, login: string,age: integer, gpa: real).Think of a relation as a set of rows or tuplesi.e., al
4、l rows are distinct (not required by commercial database)Example Instance of Students Relation Cardinality = ?, degree = ? Cardinality = 3, degree = 5. All rows are distinct Do all columns in a relation instance have to be distinct?Attribute Types / DomainEach column of a relation has a nameSet of a
5、llowed values for each column is called domain of columnDomain specifies that values of the column must be drawn from the domain associated with the column domain constraintColumn values are (normally) required to be atomic, i.e., indivisibleThe special value null is a member of every domain (ignore
6、 for now)Relations are Unordered Order of tuples is irrelevant (tuples may be stored in an arbitrary order) E.g., Account relation with unordered tuplesDatabaseA database consists of multiple relationsInformation about an enterprise is broken up into parts, with each relation storing one part of the
7、 informationE.g.: account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customersStoring all information as a single relation such as bank(account-number, balance, customer-name, .)results in:repetition
8、of information (e.g., two customers own an account)need for null values (e.g., represent a customer without an account)Normalization theory (Chapter 7) deals with how to efficiently design relational schemas.Relational Query Languages (SQL)Developed by IBM (system R) in the 1970sNeed for a standard
9、since it is used by many vendorsStandards: SQL-86SQL-89 (minor revision)SQL-92 (major revision)SQL-99 (major extensions, current standard). . .Relational Query Languages (SQL)A major strength of the relational model: supports simple, powerful querying of data. Queries can be written intuitively, and
10、 the DBMS is responsible for efficient evaluation.The key: precise semantics for relational queries.Enables: optimizer re-orders operations, yet ensures that the answer does not change.SQL = DDL + DML + (Chap 5)DDL - Creating RelationsCreates Students relation. Observe that type (domain) of each fie
11、ld is specified, and enforced by DBMS whenever tuples are added or modified. As another example, Enrolled table holds information about courses that students take.CREATE TABLE Students(sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL) CREATE TABLE Enrolled(sid: CHAR(20), cid:
12、CHAR(20), grade: CHAR(2) DDL - Destroying and Altering RelationsDestroys the relation Students. The schema information and the tuples are deleted.DROP TABLE Students The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new
13、field.ALTER TABLE Students ADD COLUMN firstYear: integerDML - Query single relationTo find all 18 year old students, we can write:SELECT *FROM Students SWHERE S.age=18To find just names and logins, replace the first line:SELECT S.name, S.loginFROM Students SWHERE S.age=18 DML - Querying Multiple Rel
14、ationsWhat does the following query compute?SELECT S.name, E.cidFROM Students S, Enrolled EWHERE S.sid=E.sid AND E.grade=“A”Given the following instances of Enrolled and Students:we get:DML - Adding and Deleting TuplesCan insert a single tuple using:INSERT INTO Students (sid, name, login, age, gpa)V
15、ALUES (53688, Smith, smithee, 18, 3.2)Can delete all tuples satisfying some condition (e.g., name = Smith):DELETE FROM Students SWHERE S.name = Smith Powerful variants of these commands are available; more later!Integrity Constraints (ICs)IC: condition that must be true for any instance of the datab
16、aseICs are specified when schema is defined.ICs are checked when relations are modified.A legal instance of a relation is one that satisfies all specified ICs. DBMS should not allow illegal instances.Integrity Constraints (ICs)IC include: Fundamental constraints : Key Foreign Key, Domain Constraints
17、General constraints: table constraints (single table)assertions (several tables)Key ConstraintTwo rules for Key constraints:Two distinct tuples in a legal instance cannot have identical values in all columns of keys (unique)No subset of the set of fields in a key is a unique identifier for a tuple (
18、maximal)Example: “No two students can have the same student Id “No two students can have the same student Id and name”CORE IDEA : Minimal subset of columns of the relation that uniquely identify the tuple. KeysLet K RK is a superkey of R if values for K are sufficient to identify a unique tuple of r
19、elation r(R) Example: customer-name, customer-street and customer-name are both superkeys of relation Customer. NO two customers can possibly have the same name.Set of all fields is a super keyK is a candidate key if K is minimalExample: customer-name is a candidate key for Customer.- if superkey, a
20、nd - no subset of it is a superkey.Primary and Candidate Keys in SQLPossibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key.CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) )CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20
21、), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) )Primary and Candidate Keys in SQLCREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) )“For a given student and course, there is a single grade.” vs. “Students can take only one course, and receive a single g
22、rade for that course; further, no two students in a course receive the same grade.”CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) )Used carelessly, an IC can prevent the storage of database instances that arise in practice!Foreign Keys, Refere
23、ntial IntegrityForeign key : Set of fields in one relation that is used to refer to a tuple in another relation. (Like a logical pointer.)Foreign key : FK in referencing relation must match PK of referenced relation. Match = same number of columns, compatible data types (column names can be differen
24、t).Enrolled (referencing relation)Students (referenced relation)Foreign KeyPrimary KeyForeign Keys in SQLOnly students listed in Students relation should be allowed to enroll for courses.CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERE
25、NCES Students ) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references.Enforcing Referential Integritysid in Enrolled is a foreign key that references Students.Insertion: What if a new Student tuple is inserted? Insertion: What should be done if
26、an Enrolled tuple with a non-existent student id is inserted? Reject itEnrolled (referencing relation)Students (referenced relation)Foreign KeyPrimary KeyEnforcing Referential IntegrityDeletion: What if an Enrolled tuple is deleted?Enrolled (referencing relation)Students (referenced relation)Foreign
27、 KeyPrimary KeyEnforcing Referential IntegrityDeletion: What if a Students tuple is deleted?Cascading - Also delete all Enrolled tuples that refer to it.No Action - Disallow deletion of a Students tuple that is referred to.Set Default - Set sid in Enrolled tuples that refer to it to a default sid.Se
28、t Null - Set sid in Enrolled tuples that refer to it to a special value null, denoting unknown or inapplicable. (Not always applicable)Similar if primary key of Students tuple is updated.Enrolled (referencing relation)Students (referenced relation)Foreign KeyPrimary KeyEnforcing Referential Integrit
29、yConsider Students and Enrolled; sid in Enrolled is a foreign key that references Students.Insertion: What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!)Deletion: What should be done if a Students tuple is deleted?Cascading - Also delete all Enrolled tup
30、les that refer to it.No Action - Disallow deletion of a Students tuple that is referred to.Set Default - Set sid in Enrolled tuples that refer to it to a default sid.Set Null - Set sid in Enrolled tuples that refer to it to a special value null, denoting unknown or inapplicable. (Not always applicab
31、le)Similar if primary key of Students tuple is updated.Referential Integrity in SQLSQL/99 supports 4 options on deletes and updates:Default is NO ACTION (delete/update is rejected)CASCADE (also delete all tuples that refer to deleted tuple)SET NULL / SET DEFAULT (sets foreign key value of referencin
32、g tuple)CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students (sid)ON DELETE CASCADEON UPDATE SET DEFAULT )Where do ICs Come From?ICs are based upon semantics of real-world enterprise being described in database relations. Can
33、we check a database instance to see if an IC is violated ?Can we infer that an IC is true by looking at a database instance ?An IC is a statement about all possible instances!From example, we know name is not a key, but the assertion that sid is a key is given to us by domain knowledge !Key and foreign key ICs are the most common; but more general ICs supported in some systems.Views Usef
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业新技术推广应用:策略、实践与未来展望
- 2026年欧盟碳关税背景下生物天然气绿色溢价测算分析
- 2026年颠覆性生物反应器设计选型与操作手册
- 2026年数据经纪合规服务安全审计机构选型手册
- 2026年飞行安全规范雾滴粒径检测方法标准编制指南
- 2026年监管沙盒在数据信托创新中的应用:自贸试验区先行先试
- 2026年农村电商服务设施直播设备配置标准手册
- 2026年小型纯电微耕机电动植保无人机选型与应用实务
- 2026年碳纳米管薄膜透明导电电极柔性电子应用方案
- 2026年转型贷款 生态保护贷组合产品产业修复协同融资方案
- 口腔颌面外科学课件:颌骨骨髓炎
- 东北地区概况农业生产条件及农业发展
- 胃malt淋巴瘤临床与诊治进展
- 上海市初中物理竞赛“大同杯”历年真题分类汇编(共9个)学生版+解析版
- 2023年广东高考英语听说考试真题D录音原文与参考答案
- 《史记》上册注音版
- 承包人实施计划及施工组织设计
- 马克思主义哲学十讲
- 《草船借箭》【市一等奖】
- d-地舒单抗注射液说明书
- GB/T 24245-2009橡胶履带用钢帘线
评论
0/150
提交评论