Download Lecture16_Databases

Document related concepts

Serializability wikipedia , lookup

DBase wikipedia , lookup

IMDb wikipedia , lookup

Microsoft Access wikipedia , lookup

SQL wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

PL/SQL wikipedia , lookup

Functional Database Model wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational algebra wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Join (SQL) wikipedia , lookup

ContactPoint wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Lecture 16
Introduction to Database Concepts
Differences Between Tables
and Databases
• Database: table of info
– iTunes
– Automobile registrations
– Demography tables
Canada’s Demographic
Information
The Database’s Advantage
• Metadata advantage
– Search by country
– Compute world population
The Database’s Advantage
• Tags for database fulfill two roles
– Identify type of data
• <country>, <population>
– Define affinity of data
• <demogData>
XML:
A Language for Metadata Tags
• XML: Extensible Markup Language
• User creates tags
XML:
A Language for Metadata Tags
• Matching tags
• Use on Web
• Written in plain text editor
Rules for Writing XML
XML
• Similar to HTML
• No spaces
• Case sensitive
• Whitespace b/w tags
XML Example
• Scenario
– Create a database for Windward Islands
archipelago in South Pacific
– Develop tags
<archipelago>
<island>
<iName> Tahiti </iName>
<area>1048</area>
</island>
⁞
</archipelago>
Affinity role
XML
<?xml version = "1.0" encoding="UTF-8" ?>
• Recommended declaration (beginning of
file)
• Version
• Encoding
Expanding the Use of XML
• Adding another archipelago
• Same file
• Same structure
• Additional tag: <a_name>
Expanding the Use of XML
• Root element of XML database
– Encloses all content
– <archipelago> in previous example
New root
element
Attributes in XML
• Attributes (like HTML)
– Use for additional metadata, not for actual
content
– Quotes (single or double)
<book id=“bk109”><title>Divergent</title>…
Effective Design with XML Tags
• Identification Rule
– Label Data with Tags Consistently (E.g. from
previous XML?)
• Affinity Rule
– Group Related Data (E.g.?)
• Collection Rule
– Group Related Instances (E.g.?)
Effective Design with XML Tags
• Difference b/w Collection Rule and Affinity
Rule
– Both for grouping
– Single instance or collection?
Other XML Examples
• Note
• CD catalog
• Breakfast menu
• http://www.w3schools.com/xml/xml_exam
ples.asp
Relational Databases
• Relational databases (RDBs)
– Relationships among different kinds of data
(e.g., Student, Course)
– Description with XML
– Answering queries
Entities
• Entity
– Anything that can be identified by a fixed # of
attributes
• Attributes
– Names and values
Entities
• Entity defines a table (Island)
– Name of entity is name of table
• Column represents attribute
– Values in column are attribute values
• Row
– Entity instance (often just entity)
Island Example
• Island entity as table
Island Example
• Island entity as XML
<island>
<name>Isabela</name>
<area>4588</area>
<elevation>1707</elevation>
</island>
<island>
<name>Fernandina</name>
…
Entities
• Attributes have data types
– Number, date, image, etc.
• <area type=“number”>14</area>
• Ensures proper format
Properties of Entities
• Table can be empty
• Order rows/columns irrelevant
• Each row represents different entity
• No two rows can be same
Keys
• Attributes distinguish rows
– Single attribute
– Multiple attributes
• Candidate key
– Attribute(s) which uniquely defines entity
Keys
• Primary key
– Candidate key used by database
• Keys must distinguish among potential
entities
• Unique IDs can be assigned
– E.g., M#
Keys
• North American Political Units
– Candidate keys?
Country
US
CA
…
MX
Name
Alaska
Alberta
Capital
Largest City
Juneau
Anchorage
Edmonton Calgary
Zacatecas Zacatecas Zacatecas
Atomic Data
• Atomic Data
– Values stored for attributes
– Not separable
• Separate fields for street, city, state, etc.
– Exceptions
• Dates, time, and currency
• E.g., 01/01/1970 (with format dd/mm/yyyy)
Database Schemes
• How to define a table?
– Tags
– Schema
• Table name
• Attributes and data types
• Primary key
Database Table Definition
Database Tables Recap
• Entities: rows; Columns (fields): attributes
• Rows and columns unordered
• Tables and fields should have mnemonic names
• Fields must be atomic
• One or more attributes define the primary key
Operations on Tables
• Relational database: collection of tables
• Key operation of database: search
• User queries db, s/w searches
Operations on Tables
• Operations on tables produce tables (which may
be empty)
• Operations
–
–
–
–
–
–
Select
Project
Union
Difference
Product
Join
Select Operation
• Select
– Takes rows from one table to create new
table
• Select Test From Table
• Test applied to each row
Select Operation
• Test is short formula
• Test uses
– attribute names
– constants
– relational operators <, ≤, ≠, =, ≥
Select Query
• Select Interest=“Beach” From Nations
• Select Lat > 40 AND N_S = ‘N’ From
Nations
Project Operation
• Project
– Builds a new table from the columns of an
existing table
Project Field_List From Table
Project Query
• Project Name, Domain, Interest From
Nations
Union Operation
• Union
– Combine two tables with same attributes
Table1 + Table2
Union Query
• At45OrBelow = (Select Latitude >= 45
AND N_S = ‘S’ From Nations)
• ExtremeGovt = At60OrAbove +
At45OrBelow
Difference Operation
• Difference Operation
– Remove common rows in two tables
– Same fields
Table1 − Table2
– E.g., Nations – At60OrAbove
Product Operation
• Product
– Multiplying tables together
– Creates “super table”
Table1 × Table2
– Each row of Table2 is appended to each row
of Table1
Product Example
Super = Nations × Travelers
Results
in
New table with ten fields
Product Operation
• Merges info that may not “belong together”
• Super table usu. “trimmed down”
Product Example, Trimmed
Super = Nations x Travelers
Assign = (Select N_S=‘S’ AND E_W=‘W’
AND Friend=‘Isabella’ From Super)
+ (Select N_S=‘S’ AND E_W=‘E’
AND Friend=‘Brian’ From Super)
+ (Select N_S=‘N’ AND E_W=‘E’
AND Friend=‘Wen’ From Super)
+ (Select N_S=‘N’ AND E_W=‘W’
AND Friend=‘Clare’ From Super)
Master = Project Name,Friend From Assign
14-50
Join Operation
• Join
– Combines two tables, but it doesn’t
necessarily produce all pairings
– Match on common fields
Table1
Table2 On Match
Join Applied
• Join
– A “lookup” operation on tables
For each row in one table, locate a row (or
rows) in the other table with the same value(s)
in the common field(s)
– If found, combine the two; if not, look up the
next row
Master
Northern On Master.Name = Northern.Name
Structure of a Database
• Two forms of tables
– Physical database
Stored on disk
– Logical database (view)
Created for users on-the-fly
Customized for their needs
• Separate management from presentation
Physical Database
• Physical database
– Designed by database admins
– Efficient storage
– Avoids redundancy
• O/w update problems
Logical Database
• Logical database
– Shows users view of info they want
– Created on demand
– Generated from copy stored in physical
database
Queries
• Query
– Specification using five operations and Join
– Results in new table
– Written in SQL (Structured Query Language)
standard
Defining Physical Tables
• Defining DB: Database Schemes
(schema)
– Metadata specification of a database’s tables
– Gives the table names, fields, types
– Used to analyze a DB design
Connecting Database Tables by
Relationships
• Student and Home_Base tables
– Can have different security access
restrictions
• Access Home_Base data without having
access to more sensitive data in Student
– Separate tables but not independent
• Student_ID connects the two tables
(establishes a relationship between them)
– Primary key
14-62
The Idea of Relationships
• Relationship
– Correspondence between rows of different
tables
Relationship Examples
• Familiar relationship examples
– Father_Of
relationship between a man and his child
– Daughter_Of
relationship between a girl and her parent
– Employed_By
relationships between people and companies
Practical Construction Using
QBE
• Database systems can make development of a
Join easy
• Query By Example (QBE) in Microsoft Access
– Software provides a template of a table, and we fill in
what we want in the fields (example)
– Software generates query
Summary
• XML for metadata
• Database tables
• Table queries
– Select, project, union, product, difference, join
– SQL
Summary
• Relationships
• Physical vs. Logical database
• QBE