relational approach
TRANSCRIPT
-
8/10/2019 Relational Approach
1/28
Relational Approach
-
8/10/2019 Relational Approach
2/28
Basic Structure
Given sets A1, A2, . An a relation r is a subset of A1 x A2 x x An Thus a relation is a set of n-tuples ( a 1, a 2, , a n) wherea i A i
Example: ifcustomer-name = {Jones, Smith, Curry, Lindsay}customer-street = {Main, North, Park}customer-city = {Harrison, Rye, Pittsfield}
Then r = {(Jones, Main, Harrison), (Smith, North, Rye),(Curry, North, Rye), (Lindsay, Park, Pittsfield)} is a relationover customer-name x customer-street x customer-city
-
8/10/2019 Relational Approach
3/28
Relation Schema
A1, A2, , An are attributes R = ( A1, A2, , An ) is a relation schema
Customer-schema - (customer-name,customer-street,customer-city ) r (R ) is a relation on the relation schema R
customer (Customer-schema)
-
8/10/2019 Relational Approach
4/28
Relation Instance
The current values (relation instance)of a relation are specified by a table
An element t of r is a tuple,represented by a row in a table
JonesSmithCurry
Lindsay
customer-name
MainNorthNorthPark
customer-street
HarrisonRyeRye
Pittsfield
customer-city
customer
-
8/10/2019 Relational Approach
5/28
Keys Let K R K is a superkey of R if values for K are sufficient to
identify a unique tuple of each possible relation r(R) bypossible r we mean a relation r that could exist in the
enterprise we are modeling.Example: { customer-name, customer-street } and{customer-name } are both superkeys of Customer , if no
two customers can possibly have the same name.
K is a candidate key if K is minimalExample: { customer-name } is a candidate key forCustomer, since it is a superkey {assuming no twocustomers can possibly have the same name), and nosubset of it is a superkey.
-
8/10/2019 Relational Approach
6/28
Determining Keys from E-RSets
Strong entity set . The primary key of the entity setbecomes the primary key of the relation.
Weak entity set . The primary key of the relationconsists of the union of the primary key of the strongentity set and the discriminator of the weak entity set.
Relationship set . The union of the primary keys of therelated entity sets becomes a super key of the relation.For binary many-to-one relationship sets, the primarykey of the many entity set becomes the relationsprimary key.For one-to- one relationship sets, the relations primarykey can be that of either entity set.
-
8/10/2019 Relational Approach
7/28
Data Definition Language (DDL)
The schema for each relation. The domain of values associated with each
attribute. Integrity constraints The set of indices to be maintained for each
relations. Security and authorization information for each
relation. The physical storage structure of each relation
on disk.
Allows the specification of not only a set of relations but alsoinformation about each relation, including:
-
8/10/2019 Relational Approach
8/28
Domain Types in SQL char(n). Fixed length character string, with user-
specified length n. varchar(n). Variable length character strings, with user-
specified maximum length n. int. Integer (a finite subset of the integers that is
machine-dependent). smallint. Small integer (a machine-dependent subset of
the integer domain type). numeric(p,d). Fixed point number, with user-specified
precision of p digits, with n digits to the right of decimalpoint.
-
8/10/2019 Relational Approach
9/28
Domain Types in SQL (Cont.) real, double precision. Floating point and double-precision
floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision
of at least n digits. date. Dates, containing a (4 digit) year, month and date. time. Time of day, in hours, minutes and seconds.
Null values are allowed in all the domain types. Declaringan attribute to be not null prohibits null values for that
attribute. create domain construct in SQL-92 creates user-defined
domain typescreate domain person-name char (20) not null
-
8/10/2019 Relational Approach
10/28
Built-in Data Types in SQL date: Dates, containing a (4 digit) year, month and date
Example: date 2005 -7-27 time: Time of day, in hours, minutes and seconds.
Example: time 09:00:30 time 09:00:30.75 timestamp : date plus time of day
Example: timestamp 2005 -7-27 09:00:30.75 interval: period of time
Example: interval 1 day Subtracting a date/time/timestamp value from another gives
an interval value Interval values can be added to date/time/timestamp values
-
8/10/2019 Relational Approach
11/28
Build-in Data Types in SQL(Cont.)
Can extract values of individual fields fromdate/time/timestamp Example: extract (year from r.starttime)
Can cast string types to date/time/timestamp Example: cast as
date Example: cast as
time
-
8/10/2019 Relational Approach
12/28
User-Defined Types create type construct in SQL creates user-defined type
create type Dollars as numeric (12,2) final
create domain construct in SQL-92 creates user-defined
domain types
create domain person_name char (20) not null
Types and domains are similar. Domains can haveconstraints, such as not null , specified on them.
-
8/10/2019 Relational Approach
13/28
Large-Object Types
Large objects (photos, videos, CAD files,etc.) are stored as a large object : blob : binary large object -- object is a large
collection of uninterpreted binary data (whoseinterpretation is left to an application outsideof the database system)
clob : character large object -- object is a large
collection of character data When a query returns a large object, a pointer
is returned rather than the large object itself.
-
8/10/2019 Relational Approach
14/28
Create Table Construct An SQL relation is defined using the create tablecommand:
create table r ( A1 D1, A2 D2, ..., An Dn,(integrity-constraint 1),...,
(integrity-constraint k)) r is the name of the relation each A i is an attribute name in the schema of relation r Di is the data type of values in the domain of attribute
A i Example:
create table branch (branch-name char(15) not null,branch-city char(30),assets integer)
-
8/10/2019 Relational Approach
15/28
Drop and Alter Table Constructs The drop table command deletes all information about the
dropped relation from the database. The after table command is used to add attributes to an
existing relation. All tuples in the relation are assigned null
as the value for the new attribute. The form of the altertable command isalter table r add A D
where A is the name of the attribute to be added to relation rand D is the domain of A.
The alter table command can also be used to dropattributes of a relation
alter table r drop A when A is the name of an attribute of relation r .
-
8/10/2019 Relational Approach
16/28
Integrity Constraints in Create Table not null
primary key ( A1, ..., An ) check (P,) where P is a predicate
Example: Declare branch-name as the primary key for branch and ensure that the values of assets are non-negative.
create table branch(branch-name char(15) not null,branch-city char(30)assets integer,primary key (branch-name),
check (assets > - 0))
-
8/10/2019 Relational Approach
17/28
Entity integrity In a relational database , entity integrity is a property that
ensures that no records are duplicated and that no attributes that make up the primary key are NULL. It is one of theproperties necessary to ensure the consistency of thedatabase.
Entity Integrity ensures that there are no duplicate records
within the table and that the field that identifies each recordwithin the table is unique and never null. The existence of the Primary Key is the core of the entity
integrity. If you define a primary key for each entity, theyfollow the entity integrity rule.
Entity integrity specifies that the Primary Keys on everyinstance of an entity must be kept, must be unique and musthave values other than NULL.
http://en.wikipedia.org/wiki/Relational_databasehttp://en.wikipedia.org/wiki/Attributehttp://en.wikipedia.org/wiki/Primary_keyhttp://en.wikipedia.org/wiki/Null_%28SQL%29http://en.wikipedia.org/wiki/Null_%28SQL%29http://en.wikipedia.org/wiki/Primary_keyhttp://en.wikipedia.org/wiki/Attributehttp://en.wikipedia.org/wiki/Relational_database -
8/10/2019 Relational Approach
18/28
Entity integrity Although most relational databases do not specifically dictate that a
table needs to have a Primary Key, it is good practice to design aPrimary Key for each table in the relational model. This mandates noNULL content, so that every row in a table must have a value thatdenotes the row as a unique element of the entity.
Entity Integrity is the mechanism the system provides to maintainprimary keys. The primary key serves as a unique identifier for rows
in the table. Entity Integrity ensures two properties for primary keys: The primary key for a row is unique; it does not match the primary keyof any other row in the table. The primary key is not null, no componentof the primary key may be set to null. The uniqueness property ensuresthat the primary key of each row uniquely identifies it; there are noduplicates. The second property ensures that the primary key hasmeaning, has a value; no component of the key is missing.
The system enforces Entity Integrity by not allowing operations(INSERT, UPDATE) to produce an invalid primary key. Anyoperation that creates a duplicate primary key or one containingnulls is rejected.
-
8/10/2019 Relational Approach
19/28
-
8/10/2019 Relational Approach
20/28
Integrity Constraints Integrity constraints guard against accidental
damage to the database, by ensuring thatauthorized changes to the database do notresult in a loss of data consistency. A checking account must have a balance
greater than $10,000.00 A salary of a bank employee must be at
least $4.00 an hour A customer must have a (non-null) phone
number
-
8/10/2019 Relational Approach
21/28
Not Null Constraint
Declare branch_name for branch is not null branch_name char (15) not null
Declare the domain Dollars to be not null
create domain Dollars numeric (12,2) notnull
h
-
8/10/2019 Relational Approach
22/28
The Unique Constraint
unique ( A1, A2, , Am) The unique specification states that theattributes
A1, A2, Amform a candidate key. Candidate keys are permitted to be null
(in contrast to primary keys).
h h k l
-
8/10/2019 Relational Approach
23/28
The check clause check (P ), where P is a predicate
Example: Declare branch_name as the primary key forbranch and ensure that the values of assets are non-negative.
create table branch(branch_name char (15) ,branch_city char (30),assets integer ,primary key (branch_name ),check (assets >= 0))
-
8/10/2019 Relational Approach
24/28
The check clause (Cont.) The check clause in SQL-92 permits domains to be
restricted: Use check clause to ensure that an hourly_wage
domain allows only values greater than a specifiedvalue.
create domain hourly_wage numeric(5,2)constraint value_test check (value > =
4.00) The domain has a constraint that ensures that the
hourly_wage is greater than 4.00 The clause constraint value_test is optional;
useful to indicate which constraint an updateviolated.
-
8/10/2019 Relational Approach
25/28
Referential integrity Referential integrity in a relational database is consistency
between coupled tables. Referential integrity is usually enforced bythe combination of a primary key or candidate key (alternate key )and a foreign key . For referential integrity to hold, any field in a table that is declared a foreign key can contain only values from a parenttable's primary key or a candidate key. For instance, deleting arecord that contains a value referred to by a foreign key in anothertable would break referential integrity. The relational databasemanagement system (RDBMS) enforces referential integrity,normally either by deleting the foreign key rows as well to maintainintegrity, or by returning an error and not performing the delete.Which method is used would be defined by the definition of thereferential integrity constraint. Example
An employee database stores the department in which eachemployee works. The field "DepartmentNumber" in the Employeetable is declared a foreign key, and it refers to the field "Index" in theDepartment table which is declared a primary key. Referentialintegrity would be broken by deleting a department from theDepartment table if employees listed in the Employee table arelisted as working for that department.
http://en.wikipedia.org/wiki/Relational_databasehttp://en.wikipedia.org/wiki/Primary_keyhttp://en.wikipedia.org/wiki/Candidate_keyhttp://en.wikipedia.org/wiki/Alternate_keyhttp://en.wikipedia.org/wiki/Foreign_keyhttp://en.wikipedia.org/wiki/Table_%28database%29http://en.wikipedia.org/wiki/Relational_database_management_systemhttp://en.wikipedia.org/wiki/Relational_database_management_systemhttp://en.wikipedia.org/wiki/Databasehttp://en.wikipedia.org/wiki/Databasehttp://en.wikipedia.org/wiki/Relational_database_management_systemhttp://en.wikipedia.org/wiki/Relational_database_management_systemhttp://en.wikipedia.org/wiki/Table_%28database%29http://en.wikipedia.org/wiki/Foreign_keyhttp://en.wikipedia.org/wiki/Alternate_keyhttp://en.wikipedia.org/wiki/Candidate_keyhttp://en.wikipedia.org/wiki/Primary_keyhttp://en.wikipedia.org/wiki/Relational_database -
8/10/2019 Relational Approach
26/28
Referential Integrity Ensures that a value that appears in one relation for a given set
of attributes also appears for a certain set of attributes in anotherrelation. Example: If Perryridge is a branch name appearing in one
of the tuples in the account relation, then there exists a tuplein the branch relation for branch Perryridge.
Primary and candidate keys and foreign keys can be specified aspart of the SQL create table statement: The primary key clause lists attributes that comprise the
primary key. The unique key clause lists attributes that comprise a
candidate key. The foreign key clause lists the attributes that comprise the
foreign key and the name of the relation referenced by theforeign key. By default, a foreign key references the primarykey attributes of the referenced table.
f l
-
8/10/2019 Relational Approach
27/28
Referential Integrity in SQL Example
create table customer(customer_name char (20) ,customer_street char (30),customer_city char (30),primary key (customer_name ))
create table branch(branch_name char (15) ,branch_city char (30),assets numeric (12,2),primary key (branch_name ))
-
8/10/2019 Relational Approach
28/28
Referential Integrity in SQL Example (Cont.)
create table account(account_number char (10) ,branch_name char (15),balance integer ,primary key (account_number),foreign key (branch_name ) references branch )
create table depositor(customer_name char (20) ,account_number char (10) ,
primary key (customer_name, account_number),foreign key (account_number ) referencesaccount,foreign key (customer_name ) referencescustomer )