The Relational Model

The Relational Model

The Relational Model What is the Relational Model ▪ Relations ▪ Domain Constraints SQL Integrity Constraints Translating an ER diagram to the Relational Model and SQL Views A relational database consists of a collection of tables ▪ Each table has a unique name ▪ Each row represents a single entity or relationship A table corresponds to the mathematical concept of a set ▪ Note that the definition of a relation* is not the same as the usual mathematical definition *per E.F. Codd ▪ Relations correspond to tables a relation is a set of tuples (d1, d2, ..., dn), where each element dj is a ▪ Tuples correspond to rows member of Dj, a data domain The key construct in the relational model is a relation ▪ A DB is a collection of relations Relations consist of a relation instance and a relation schema ▪ A relation instance corresponds to an actual table with a set of rows ▪ A relation schema consists of the column headings and domains of the table A schema consists of ▪ The relation's name ▪ The name of each column in the table, also known as a field, or attribute ▪ The domain of each field ▪ Each domain has a set of associated values like a type For example, the Customer relation ▪ Customer = (sin, firstName, lastName, age, income) ▪ The schema also specifies the domain of each attribute Relation schema: Customer = (sin, firstName, lastName, age, salary) sin firstName lastName age salary 111 Buffy Summers 23 43000.00 222 Xander Harris 22 6764.87 Relation instance 333 Rupert Giles 47 71098.65 444 Dawn Summers 17 4033.32 Each field (attribute) has a domain: char(11), char(20), char(20), integer, real An instance is a (small) subset of the Cartesian product of the domains A domain of a field is similar to the type of a variable ▪ The values that can appear in a field are restricted to the field's domain A relation instance is a subset of the Cartesian product of the list of domains ▪ If a relation schema has domains D1 to Dn ▪ Where the domains are associated with fields f1 tofn ▪ A relation must be a subset of D1 D2 … Dn-1 Dn ▪ In practice a relation instance is usually some very small subset of the Cartesian product A relation instance is a set of tuples, or records ▪ Each tuple has the same number of fields as the schema No two tuples are identical ▪ Relations are defined to be sets of unique tuples ▪ A set is a collection of distinct values ▪ In practice DBMS allow tables to have duplicate records in some circumstances The order of the tuples is not important The degree of a relation is the number of fields ▪ Also referred to as arity ▪ The degree of the Customer relation is 5 The cardinality of a relation instance is the number of tuples it contains ▪ The cardinality of the Customer relation instance is 4 A relational database is a collection of relations with distinct relation names ▪ A relational database schema is the collection of schemas for all relations in the database Relational model Database Terminology ▪ Relation schema ▪ Table schema ▪ Relation instances ▪ Tables (relations) ▪ Columns or attributes ▪ Fields ▪ Records or rows ▪ Tuples SQL stands for Structured Query Language SQL is divided into two parts ▪ Data Manipulation Language (DML) which allows users to create, modify and query data ▪ Data Definition Language (DDL) which is used to define external and conceptual schemas The DDL supports the creation, deletion and modification of tables ▪ Including the specification of domain constraints and other constraints To create a table use the CREATE TABLE statement ▪ Specify the table name, field names and domains CREATE TABLE Customer ( Question – is SQL case sensitive? sin CHAR(11), Answer – SQL keywords (create firstName CHAR(20), and table for example) are not lastName CHAR(20), case sensitive. age INTEGER, Named objects (tables, columns income REAL) etc.) may be. To insert a record into an existing table use the INSERT statement ▪ The list of column names is optional ▪ If omitted the values must be in the same order as the columns INSERT INTO Customer(sin, firstName, lastName, age, income) VALUES ('111', 'Sam', 'Spade', 23, 65234) To delete a record use the DELETE statement ▪ The WHERE clause specifies the record(s) to be deleted DELETE FROM Customer WHERE sin = '111' Be careful, the following SQL query deletes all the records in a table DELETE FROM Customer Use the UPDATE statement to modify a record, or records, in a table ▪ Note that the WHERE statement is evaluated before the SET statement Like DELETE the WHERE clause specifies which records are to be updated UPDATE Customer SET age = 37 WHERE sin = '111' To delete a table use the DROP TABLE statement ▪ This not only deletes all of the records but also deletes the table schema DROP TABLE Customer ▪ http://xkcd.com/327/ Columns can be added or removed to tables using the ALTER TABLE statement ▪ ADD to add a column and ▪ DROP to remove a column ALTER TABLE Customer ADD height INTEGER ALTER TABLE Customer DROP height An integrity constraint restricts the data that can be stored in a DB ▪ To prevent invalid data being added to the DB ▪ e.g. two people with the same SIN or ▪ someone with a negative age When a DB schema is defined, the associated integrity constraints should also be specified A DBMS checks every update to ensure that it does not violate any integrity constraints Domain Constraints ▪ Specified when tables are created by selecting the type of the data ▪ e.g. age INTEGER Key Constraints ▪ Identifies primary keys and other candidate keys Foreign Key Constraints ▪ References primary keys of other tables General constraints A key constraint states that a minimal subset of the fields in a relation is unique ▪ i.e. a candidate key SQL allows two sorts of key constraints The UNIQUE statement identifies candidate keys ▪ Records may not have duplicate values for the set of attributes specified in the UNIQUE statement A PRIMARY KEY identifies the primary key ▪ Records may not have duplicate primary keys and ▪ May not have null values for primary key attributes A primary key identifies the unique set of attributes that will be used to identify a record in a table ▪ In some cases there are obvious primary keys in the problem domain (e.g. SIN) ▪ In other cases, a primary key may be generated by the system Candidate keys are identified to record the fact that a set of attributes should be unique ▪ Preventing duplicate data being entered into the table for this set of attributes Assume that a patient can be uniquely identified by either SIN or MSP number ▪ SIN is chosen as the primary key CREATE TABLE Patient ( sin CHAR(11), msp CHAR(15), firstName CHAR(20), lastName CHAR(20), age INTEGERINTEGER, ) CONSTRAINT unique_msp UNIQUE (msp) PRIMARY KEY (sin) ) Consider the abbreviated ERD shown below ▪ Takes is a many-to-many relationship, so a database table must be created for it (more on this later) ▪ It makes no sense for the Takes table to be allowed to contain the student ID of students who do not exist The student ID in Takes should be identified as a foreign key, that references Student Student takes Course A foreign key constraint references the primary key of another relation The value of the foreign key attribute(s) must match a primary key in the referenced table referencing referenced relation relation Student Takes Course The attributes of the foreign key and the referenced primary key must be consistent ▪ The same number of attributes, with ▪ Compatible attribute types, but ▪ The attribute names may be different A foreign key references the entire primary key ▪ Where the primary key is compound the foreign key must also be compound ▪ And not multiple single attribute foreign keys an account must be owned by a single customer Customer owns Account CREATE TABLE Account( accountNumber INTEGER, type CHAR(5), balance REAL, custSIN CHAR(11), PRIMARY KEY (accountNumber))), CONSTRAINT fk_customer FOREIGN KEY (custSIN) REFERENCES Customer ) A DB may require constraints other than primary keys, foreign keys and domain constraints ▪ Limiting domain values to subsets of the domain ▪ e.g. limit grade to the set of legal grades ▪ Or other constraints involving multiple attributes SQL supports two kinds of general constraint ▪ Table constraints associated with a single table ▪ Assertions which may involve several tables and are checked when any of these tables are modified These will be covered later in the course Whenever a table with a constraint is modified the constraint must be checked ▪ A modification can be a deletion, a change (update) or an insertion of a record ▪ If a transaction violates a constraint what happens? Primary key constraints ▪ A primary key constraint can be violated in two ways ▪ A record can be changed or inserted so that it duplicates the primary key of another record in the table or ▪ A record can be changed or inserted so that (one of) the primary key attribute(s) is null ▪ In either case the transaction is rejected Consider these schema (the domains have been omitted for brevity) ▪ Customer = (sin, firstName, lastName, age, income) ▪ Account = (accountNumber, balance, type, sin) ▪ sin in Account is a foreign key referencing Customer age balance income sin accountNumber Customer owns Account lastName firstName type number balance type sin Inserting {409, 0, CHQ, 761 904.33 CHQ 111 555} into Account violates 856 1011.45 CHQ 333 the foreign key on sin as 903 12.05 CHQ 222 there is no sin 555 in 1042 10000.00 SAV 333 Customer sin first last age salary The insertion is rejected 111 Buffy

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    71 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us