Database Systems Database Systems
1
Database Systems Database Systems 1 Creating a Database System - - PowerPoint PPT Presentation
Database Systems Database Systems 1 Creating a Database System Design Construction Application Usage 2 Step 0 Analyze Analyze the domain Learn what things the system is intended to handle. Learn what the constraints of
1
Creating a Database System
Design Construction Application Usage
2
Step 0 – Analyze
– Learn what things the system is intended to handle. – Learn what the constraints of the domain are.
– Learn what the system is intended to do, what
3
Step 1 – Design
– Create a schema that captures the constraints
schemas. schemas.
– Note any constraints that are not directly captured by the structure of the schema.
4
Course Objectives – Design
When the course is through, you should
– Given a domain, know how to design a database that correctly models the domain and its constraints.
”We want a database that we can use for scheduling courses and lectures. This is how it’s supposed to work: …”
5
The Relational Data Model
– Tuples are sets of data. – All tuples in a relation have the same set of attributes with values. – A database schema is a set of interconnected relation – A database schema is a set of interconnected relation schemas.
to how we tend to ”think” about data.
6
The Entity-Relationship approach
– Allows us to sketch the design of a database informally (which is good when communi- informally (which is good when communi- cating with customers).
convert your diagram to relations.
– This means that the diagram can be a formal specification as well.
7
E-R diagram for Scheduler
code name Given
GivenCourse
teacher period #students
Course
In
Lecture
Of weekday time
Room
name #seats 8
Translate to relations
Courses(code, name) GivenCourses(course, period, #students, teacher) course -> Courses.code Lectures(course, period, room, weekday, hour) (course, period) -> GivenCourses.(course, period) room -> Rooms.name Rooms(name, #seats)
9
E-R summary
10
Step 1b – Verification
with respect to the constraints of the domain!
– Functional dependencies, independencies
your design captures the constraint.
– For those constraints that are not captured, argue why not.
make a correct schema…
11
Functional dependencies (FDs)
– ”X determines A”, ”X gives A” – ”A depends on X”
attribute.
– code name – code, period teacher
12
Quiz!
What’s the point of functional dependencies? Why are E-R diagrams not enough?
– E-R diagrams can be wrong.
relations.
– Dependencies are an extra safe-guard against errors, as well as a formal proof that your design is correct. – Dependencies represent ”extra” constraints that your diagram cannot handle.
13
Anomalies
– Same course name stored several times. – Redundancy in a schema leads to possible anomalies. Courses(code, period, name, teacher)
– If we update the course name, we must remember to update all tuples.
– If a course is not scheduled to be given in any period this year, we lose its name!
14
Using FDs to detect redundancy
but X is not a key for R, then values of A will be redundantly repeated!
Courses(code, period, name, teacher)
{(’TDA356’, 2, ’Databases’, ’Niklas Broberg’), (’TDA356’, 4, ’Databases’, ’Rogardt Heldal’)}
code name code, period teacher
15
Decomposition
– Create one relation with the attributes from the offending FD, in this case code and name.
Courses(code, period, name, teacher)
code name code, period teacher this case code and name. – Keep the original relation, but remove all attributes from the RHS
key in the first.
Courses(code, name) GivenCourses(code, period, teacher) code -> Courses.code
16
Boyce-Codd Normal Form
Form (BCNF) if, whenever a nontrivial FD X A holds on R, X is a superkey of R.
– Remember: nontrivial means A is not part of X – Remember: nontrivial means A is not part of X – Remember: a superkey is any superset of a key (including the keys themselves).
Courses(code, name) GivenCourses(code, period, teacher)
17
Quiz!
Why not use BCNF decomposition for designing database schemas? Why go via E-R diagrams?
– Decomposition doesn’t handle all situations
– E-R diagrams are graphical, hence easier to sell than some mathematical formulae.
18
Other normal forms
3NF is a relaxation of BCNF
– Accept a relation even if there are dependencies X A where X is not a superkey, as long as A is in some key (prime). superkey, as long as A is in some key (prime).
– Requires us to handle not only FDs, but also independencies (INDs).
19
Properties of normalization
– Lossless join – No redundancy (in most cases)
– Lossless join – Dependencies are preserved
– Lossless join – No redundancy
20
Summary – dependencies and normal forms
– X A, ”X determines A”, ”A depends on X” – Trivial FDs, computing closures (X+, F+), finding all implied FDs of a domain.
↠
– X ↠ Y | Z, ”Y and Z are independent with respect to X” – X ↠ Y, ”Y is independent from everything else w.r.t X”
– BCNF, 3NF, 4NF
21
Summary – Design
schemas, forming a schema for the schemas, forming a schema for the database.
forms that your design is correct.
22
Creating a Database System
Design Construction Application Usage
23
Step 2 – Construction
DBMS.
– Relations become tables, attributes become columns. columns. – Constraints on relations.
24
Course Objectives – Construction
When the course is through, you should
– Given a database schema with related constraints, implement the database in a constraints, implement the database in a relational (SQL) DBMS. – SQL Data Definition Language
25
SQL Data Definition Language
– PRIMARY KEY – FOREIGN KEY … REFERENCES CHECK – CHECK
– Persistent queries – Virtual tables
26
Step 2b – Help constructs
the core database (the tables) in order to simplify the application.
– Triggers to ensure consistency and simplify – Triggers to ensure consistency and simplify complex modifications. – Views to simplify complex queries (and modifications) and present a proper interface. – Privileges to ensure that things are done correctly.
27
Triggers
some event occurs.
– Events: insertion, deletion, update – Condition: test whether to run the action or – Condition: test whether to run the action or not. – Action: The stuff to actually do.
A trigger is sometimes referred to as an ECA – Event-Condition-Action
28
Basic trigger structure
CREATE TRIGGER name [BEFORE|AFTER] [INSERT|DELETE|UPDATE] ON tablename REFERENCING [NEW|OLD] [ROW|TABLE] AS variable FOR EACH [ROW|STATEMENT] WHEN condition WHEN condition action to perform
Decide whether to run the trigger or not. What should happen when the trigger is triggered.
29
Views
– Persistent queries.
”contents” of the view are fetched by the query from other tables. from other tables.
– Virtual tables.
”contents” are stored in other tables.
was used to define it.
30
Summary – Construction
DBMS.
application simpler and/or safer.
31
Creating a Database System
Design Construction Application Usage
32
Step 3 – Operate
the application.
– Exact queries might not be known until runtime, but the structure would probably be runtime, but the structure would probably be known.
the application will need to perform.
33
Course Objectives – Usage
When the course is through, you should
– Know how to query a database for relevant data using SQL. data using SQL. – Know how to change the contents of a database using SQL. – Relational Algebra – SQL Data Manipulation Language
34
Relational Algebra
(or variables representing relations).
– Selecting rows () – Projecting columns () – Composing (joining) relations
⋈ ⋈ ⋈C, ⋈ ⋈ ⋈ ⋈)
35
SELECT-FROM-WHERE
SELECT attributes FROM tables WHERE tests over rows WHERE tests over rows SELECT X FROM T WHERE C
πX(σC(T))
36
SELECT-FROM-WHERE- GROUPBY-HAVING-ORDERBY
SELECT attributes FROM tables WHERE tests over rows GROUP BY attributes
Only the SELECT and FROM clauses must be included.
GROUP BY attributes HAVING tests over groups ORDER BY attributes SELECT X, G FROM T WHERE C GROUP BY Y HAVING D ORDER BY Z;
Z’(πX,G’(σD’(Y,G’(σC(T)))))
X must be a subset of Y. Primes ’ mean we need some renaming.
Note the difference! Note the difference!
37
Relations as sets
– More operators:
∖
– SQL uses bags, except for set operations. – DISTINCT (), ALL
38
Summary SQL and Relational Algebra
relational algebra.
– Operations over relations
WHERE-GROUPBY- HAVING-ORDERBY
– Grouping and aggregation
COUNT
– Set operations
HAVING-ORDERBY
– Selection of rows () – Projection of columns () – Combining tables
join (⋈
⋈ ⋈ ⋈C, ⋈ ⋈ ⋈ ⋈, ⋈ ⋈ ⋈ ⋈)
– Miscellaneous
– Sequencing – (Views)
˚
39
SQL Modifications
– INSERT INTO table (…) VALUES (…) – INSERT INTO table query
– DELETE FROM table WHERE test on rows
– UPDATE table SET attribute = value WHERE test on rows
40
Summary – Usage
modifications that the application will need to perform.
level interface.
41
Creating a Database System
Design Construction Application Usage
42
Step 4 – Application
– Write the application in a ”real” programming language. – Communicate with the database via some special interface.
respect to concurrency and system crashes.
43
Course Objectives – Applications
When the course is through, you should
– Know how to connect to and use a database from external applications. from external applications.
44
Two approaches
applications that use SQL, we can either
– Extend SQL with ”ordinary” programming – Extend SQL with ”ordinary” programming language constructs.
– Extend an ordinary programming language to support database operations through SQL.
45
SQL/PSM
– Parameters, local declarations – Functions return values – Calling (CALL procedures, use functions as values)
– SET – SELECT … INTO …
– Declaring, fetching values
– if, while, repeat, exceptions and handlers
46
Embedded SQL
– Prepend SQL with EXEC SQL
– Prepend with colon in SQL code. – Prepend with colon in SQL code.
– Uses control structures of the host language.
procedure calls of the host language.
47
JDBC
DBMS’s.
– A library with operations that give full access – A library with operations that give full access to relational databases, including:
48
Objects in JDBC
– Register drivers, create connections.
– Create statements or prepared statements. – Close when finished.
– Execute queries or modifications.
– Execute a particular query or modification, possibly parametrized.
– Iterate through the result set of a query.
49
ACID Transactions
processes don’t interfere with each other!
– ”ACID”: Atomicity, Consistency, Isolation, Durability. – The isolation levels of transactions may vary.
– Isolation level affects only that transaction!
50
Summary – Applications
interference and system crasches.
51
Creating a Database System
0. Analyze 1. Design
2. Construct
3. Operate 4. Write application Note that the order given here is artificial. 1 should come first, but the rest will probably be mixed, and typically depend on some larger-scale system development plan.
52
Examination
– 60 points (3/4/5 = 24/36/48, G/VG = 24/42) – Allowed aids: One handwritten A4 paper containing anything you like (on both sides), to be handed in together with the exam. – Divided into 7 distinct blocks:
(12)
(12)
(8)
(8)
(6)
(6)
(8)
53
Non-standard Exam Structure
and possibly C. Everyone is expected to know the A questions, while B and C questions are intended for those seeking higher grades.
each block!
– Less time spent on blocks that you know well. – Harder to get ”stray” points. – A’s give ~35, B’s ~40, C’s 60.
54
Block 1 – E-R diagrams (12)
”A small train company wants to design a booking system for their customers. …”
diagram. diagram.
Note that all parts of E-R modelling belong in A! B&C: More complex domain, analyze a diagram, reason about benefits and drawbacks of different designs for the domain, …
55
Block 2 – FDs and NFs (12)
”A car rental company has the following, not very success- ful, database. They want your help to improve it. …”
in the domain.
with respect to the relations in the database.
the resulting relations are in BCNF. B&C: More complex domain, decompose to 3NF or 4NF, argue about the meaning of dependencies, analyze benefits and drawbacks of different normal forms for the domain, …
56
Block 3 – SQL DDL and Modifications (8)
”A grocery store wants a database to store information. After studying their domain you have come up with the following database schema. …”
tables in a DBMS, with all constraints. B&C: Write a trigger for something, argue about the effects
together achieve some effect, more complex variations, argue about indexes or authorization,… You will not be required to write PSM code on the exam! But you may if you want to.
57
Block 4 – SQL Queries (8)
”The grocery store wants your help in getting proper information from their database. …”
about what they should return. about what they should return. Note that all parts of SQL belong in A! B&C: more complex queries, argue about the differences of queries, rewrite a query where you must use some specific construct, …
58
Block 5 – Relational Algebra (6)
”Here is a schema for a database over persons and their
expression will return.
Note that all operators of relational algebra belong in A! B&C: More complex expressions, write a relational- algebraic expression given a text description, translate an SQL query into relational algebra, …
59
Block 6 – Transactions (6)
”Here are some transactions that run in parallel. …”
what could happen if they were not run as transactions? transactions?
ensure … ? B&C: Show a transaction that would interfere with this under isolation level …, argue about benefits and drawbacks of using a particular isolation level for some transaction, …
60
Block 7 –XML (8)
”A medical research facility wants a database that uses a semi- structured model to represent different degrees of knowledge regarding the outbreak of epidemic diseases. …”
compute?
B&C: More complex queries to interpret, write an XQuery expression that computes something, discuss the benefits of the semi- structured data model for this particular domain, …
61
Things that will not appear
62
63