230 likes | 421 Views
The Relational Model. CIS 8040 Database Management Systems. J.G. Zheng Jan 2010. Overview. What is the relational model? What are the most important practical elements of relational model?. Introduction. Edgar F. Codd (IBM), 1970
E N D
The Relational Model CIS 8040 Database Management Systems J.G. Zheng Jan 2010
Overview • What is the relational model? • What are the most important practical elements of relational model?
Introduction • Edgar F. Codd (IBM), 1970 • One sentence to summarize relational database model (extremely brief): Data are organized in relations (tables), which are linked (relationship) by keys
Introduction • What’s in a database? • Tables • Relationships • Metadata • How are data organized in a database? • Data are organized in tables, which are linked (relationship) by keys
Relation • A relation is a two-dimensional table that has specific characteristics: • The table consist of rows and columns • Rows contain data about entity instances • All values in a row describes the same entity instance • Columns contain data about attributes of the entity • All values in a column are of the same kind
Relation (continued) • Relation’s specific characteristics go on: • Each row is distinct • A Cell of the table hold a single value • Each column has a unique name • The order of the rows is unimportant • The order of the columns is unimportant
Keys • A key is one or morecolumns of a relation that is used to identify a record • Unique keys • Candidate key • Primary key • Surrogate key • Foreign key • Composite key
Candidate Key/Primary Key • Candidate key • The minimum set of column(s) that uniquely identifies a single record (row) • Primary key • Is one of the candidate keys chosen to be the identifying key • There is only one primary key for a relation/table
Primary Key • Primary key is a column/attribute that is used to uniquely identify a record • The value of this key column uniquely identifies a single record (row) • There is only one primary key for a table • Can the following attribute uniquely indentify a record? • Last Name • Student Number • Social Security Number • ISBN
Artificial Primary Key/Surrogate Key • Very often it is difficult to have a natural attribute to identify one thing • A column is created arbitrarily and assign each record a unique number/id • Product Number, Product Id • Movie Id • Actor Id
Relationship and Foreign Key • Relationship defines how tables (relations) are linked • Two tables are linked by a pair of keys • The primary key of one table • The foreign key in the linked table • These two keys are of the same kind (may be of different name)
Relationship Example Primary Key (PK) Foreign Key (FK) Primary Key (PK)
Foreign Key Example Primary Key Foreign Key Primary Key and Foreign Key are of the same type (string, number, etc.) and length, but they do not necessarily have the same name.
Composite Key • Composite key: A key that contains two or more attributes (columns) • Example: • “FirstName” + “LastName” • “FirstName” + “LastName” + “BirthDate” • “FirstName” + “LastName” + “BirthDate” + “BirthCity” • … • All keys can be composite keys • True or False: The combination of all columns of a relation is a candidate key
Referential Integrity • Every value of a foreign key must match a value of the primary key or be “null” • For example (slide #17) • In “Customer” table, “RepNum” is a foreign key (linked to the “Rep” table where “RepNum” is the primary key). • Then every value of “RepNum” in the “Customer” table must exist in the “Rep” table
“Null” Value • “Null” means • value is inapplicable: e.g., Fax_Number = Null • value is unknown: e.g., Stud_Grade = Null • “Null” does not mean • zero • blank space • No primary key (or part of a composite primary key) can be null
Common Data Types • Numeric • INTEGER, SMALLINT • DECIMAL(i,j), NUMBER(i,j) • Character/String • CHAR(n), VARCHAR(n), Text • Date • DATE, DATETIME • Data types reference • Access 2007: http://msdn.microsoft.com/en-us/library/bb208866.aspx • SQL Server 2005: http://msdn.microsoft.com/en-us/library/ms187752.aspx
Summary • Key concepts • Relation • Keys • Primary key, candidate key, surrogate key • Composite key • Foreign key • Referential integrity • Null