Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
The Relational Model Lecture Topics • Overview of SQL • Underlying relational model • Relational database structure • SQL DDL and DML • Views Textbook • Chapter 3 CS 338 The Relational Model 2-1 Overview of SQL • Structured Query Language (SQL, sometimes pronounced “sequel”) • ISO/IEC 9075, international standard for relational database systems • the standard is evolving: – 1986: SQL1; initial version, 75 pages – 1989: SQL89; most commercial products conform to this version, with extensions – 1992: SQL2; three levels of conformity, ~600 pages – 1999: SQL99 (sometimes called SQL3), ~1200 pages – 2007: under revision again CS 338 The Relational Model 2-2 ...continued Main features: • powerful view definition language • integrity constraints in conceptual schema • DML can be embedded in various programming languages (called “embedded SQL”), or used via programming libraries – object/class libraries in OO environments • transaction control • authorization sublanguage/model CS 338 The Relational Model 2-3 ...continued Application 1 D B M S S Q L Application 2 View A Application 3 View B Conceptual Level Physical Schema Database CS 338 The Relational Model 2-4 Underlying relational model Example relational database for a credit card company Vendor Vno Vname 1 Sears 2 Kmart 3 Esso 4 Esso City Toronto Ottawa Montreal Waterloo Customer AccNum Cname 101 Smith 102 Jones 103 Martin Transaction Tno Vno 1001 2 1002 2 1003 3 1004 4 1005 4 CS 338 Prov Ont BC Que AccNum 101 103 101 102 103 Vbal 200.00 671.05 0.00 2.25 Cbal 25.15 2014.00 150.00 Tdate 070115 070116 070115 070120 070125 The Relational Model Climit 2000 2500 1000 Amount 13.25 19.00 25.00 16.13 33.12 2-5 Structure of a relational database Database: collection of uniquely named tables (relations) Relation: set of rows (tuples) Attribute: column Domain: set of allowed values for an attribute Attribute values must be atomic (single values): no tuples or sets or repetition Row: distinguishable thing Table: set of related things CS 338 The Relational Model 2-6 Diagrammatic conventions Vendor Vno Vname City Vbal or Vendor Vno Vname City Vbal CS 338 The Relational Model 2-7 Pictorial schema Customer AccNum Cname Prov Cbal Climit Vendor Vno Vname City VBal CS 338 Transaction Tno Vno AccNum Tdate Amount The Relational Model 2-8 The SQL DDL • used for defining – tables – views • example of table definition (conceptual schema): create table Vendor (Vno INTEGER not null, Vname VARCHAR(20), City VARCHAR(10), Vbal DECIMAL(10,2), primary key (Vno) ); CS 338 The Relational Model 2-9 ...continued create table Customer (AccNum INTEGER not null, Cname VARCHAR(20) not null, Prov VARCHAR(20), Cbal DECIMAL(6,2) not null, Climit DECIMAL(4,0) not null, primary key (AccNum) ); create table Transaction (Tno INTEGER not null, Vno INTEGER not null, AccNum INTEGER not null, Tdate DATE, Amount DECIMAL(6,2) not null, primary key (Tno), foreign key (Vno) references vendor(Vno), foreign key (AccNum) references Customer(AccNum) ); CS 338 The Relational Model 2-10 Attribute domains in SQL • INTEGER: integers representable with 32 bits • SMALLINT: integers representable with 16 bits • DECIMAL(m,n): fixed point numbers • FLOAT: 32 bit floating point numbers • CHAR(n): fixed length strings • VARCHAR(n): variable length strings • BIT(n): n bits • BIT VARYING(n): variable number of bits CS 338 The Relational Model 2-11 ...continued • DATE (year, month, day) • TIME (hour, minute, second) • TIME(i) (hour, minute, second, second fraction) • TIMESTAMP (date, time, second fraction) • INTERVAL YEAR/MONTH (year month interval) • INTERVAL DAY/TIME (day time interval) • plus many, many product-specific (nonstandard) extensions CS 338 The Relational Model 2-12 Modifying table definitions • Table schemas can be changed after the table has been created: – – – – adding columns removing columns removing constraints (e.g. p-key) some SQL implementations allow • renaming a column • modifying a column • Example: ALTER TABLE Vendor ADD Street VARCHAR(15) CS 338 The Relational Model 2-13 Removing tables • SQL operation is “drop” • Tables can be dropped at any time • Dropping a table deletes the schema and the instance • All views, foreign-key definitions are also removed • Example: DROP TABLE Transaction CS 338 The Relational Model 2-14 The SQL DML SQL has a non-navigational DML: E.g. “Find names and provinces of customers who owe more than $1000 to the company.” select Cname, Prov from Customer where Cbal > 1000; CS 338 The Relational Model 2-15 ...continued • basic querying: select columns from R1,..., Rk [ where filter ]; • result is a relation over columns (columns = “*” means all attributes in R1,..., Rk) • R1,..., Rk: tables from which the data is retrieved • filter : conditions on tuples used to form the result; optional CS 338 The Relational Model 2-16 ...continued • conditions may include: – arithmetic operators +, -, *, / – comparisons =, <>, <, <=, >, >= – logical connectives and, or and not E.g. “List the names of the customers who live in Ontario and whose balance is over 80% of their balance limit.” select Cname from Customer where Prov = 'Ont' and Cbal > 0.8 * Climit; CS 338 The Relational Model 2-17 ...continued • basic insertion: insert into Customer values (104, ‘Anne', 'ON', 0, 4000); • deletion: delete Customer rows for customers named Smith: delete from Customer where Cname = 'Smith'; • delete all transactions: delete from Transaction; CS 338 The Relational Model 2-18 ...continued • modification, changing existing rows • set balance of account 102 to zero: update Customer set Cbal = 0 where AccNum = 102; • add $100 to each customer’s monthly limit: update Customer set Climit = Climit + 100; CS 338 The Relational Model 2-19 SQL external schema • called views • a view is a named query (result is usually computed when the view is used) create view WatVendors as select VNo, VName, VBal from Vendor where City = 'Waterloo'; • views can be used in retrieval exactly like tables (but updates of views are restricted) CS 338 The Relational Model 2-20 Advantages of views • logical data independence • simplified perception of the database • different views for different users • restricting data access CS 338 The Relational Model 2-21 Basic relational concepts Relating to descriptions of data: • Attribute (column): a name denoting a property or characteristic • Relation schema (table header): a finite set of attributes and a mapping of each attribute to a domain (defined below) CS 338 The Relational Model 2-22 ...continued Relating to data: Domain: an “abstract data type” (i.e. a name, a set of values and a number of functions defined over the values) • Null value: a special exception value (meaning “not known”, “not applicable”) • Tuple: a set of attribute/value pairs, with each attribute occurring at most once • Relation (table): a relation schema, and a finite set of tuples • Relational database: a finite set of relation names and a mapping of each relation name to a relation CS 338 The Relational Model 2-23 ...continued Other: • Intention of a relation: the associated relation schema • Extension of a relation: the associated set of tuples The relational model assumes no ordering of either rows or columns for any table. CS 338 The Relational Model 2-24 Basic rules • Domain constraints: the value associated with each attribute in a tuple must occur in the set of values associated with the domain of the attribute; or the value is Null • First normal form: domain values cannot be tuples or relations • Completeness: each tuple in a relation has an attribute/value pair for precisely the set of attributes in the associated relation schema • Closed world: the database “knows of” all tuples in all relations • Unique rows: no two distinct tuples in any given relation consist of the same set of attribute/value pairs CS 338 The Relational Model 2-25 Keys • Relation superkey: a subset of the associated relation schema for which no pair of distinct tuples in the relation will ever agree on the corresponding values. • Relation candidate key: a minimal superkey • Relation primary key: a distinguished candidate key of the relation • Foreign key: primary key of one relation appearing as attributes of another relation • Foreign keys enable capturing more complex entity structure CS 338 The Relational Model 2-26 Integrity of primary and foreign keys • Entity integrity: No component of a primary key value may be the null value, nor may be updated. • Referential integrity: A tuple with a non-null value for a foreign key that does not match the primary key value of a tuple in the referenced relation is not allowed. CS 338 The Relational Model 2-27