Databases & SQL
description
Transcript of Databases & SQL
1DB&SQL 1-
Databases & SQL
Teacher: Henny Klein
contact:
wednesday 14-16
room 11 - 426
All material and assignments on Nestor
2DB&SQL 1-
Content of the course
• First block
CH 1 t.m. 5
Basic knowledge for the design and manipulation of relational databases
• Second block
CH 6 t.m. 10
other types of databases
processes in a DBMS
distributed systems
3DB&SQL 1-
Content of practical sessions
• Access: basic manipulations: tables, relations, QBE, forms, reports
• Access- SQL– data definition– data manipulation– queries
• Programming in Access– functions, event handling (VBA, DAO)
• Building an application
4DB&SQL 1-
Your own design!
• You will train with example databases but every student has also to build his/her own database
• during lectures we will discuss your design
• in practical exercises you can build your database, formulate queries, etc.
Assignment for this week (details later):
Think of a theme for your database!
5DB&SQL 1-
The book
• Gives the essential information, but concise, so in lectures time is spent on explanation and examples.
• Gives exercises, use them as a method of self assessment
6DB&SQL 1-
Grading rules
Design+Project SQL/VBA Exam30% 30% 40%
Design exercises as homework
Individual project
SQL and VBA: practical exercises
Tentamination: mostly theory, some SQL, no VBA
7DB&SQL 1-
Card catalogue, textfile, DBMS
• How did people search books in a library before the computer era?
• What are advantages of an electronic system?
• What are advantages of a database system compared to a textfile?
• How did departments of organizations communicate before the computer era?
• What are advantages of a DBMS here?
8DB&SQL 1-
Database management systems
Systematic, structured data storage• each data item is stored once, no redundancy• data integrity and security ensured• data available for distinct applications• concurrent use of data• differents views on data
DBMS used in many environments:• administration (products, clients, employees)• information (catalogues)• research (data storage, data mining)• online applications
9DB&SQL 1-
Information Science and databases
• DBMS:the standard system to store information, so often important in your work, and even beforehand:
– many students have to handle databases in their ….. in public or private organizations
– in a research …….. or in in your own research project
10DB&SQL 1-
The multi-layer system
Presentation of the data(views)
Access: Forms, reports, VBA, SQLOutside Access: (web)applicaties
Logical description of the data(conceptual level)
DBMS
Operating system (host)
Physical storage of data
11DB&SQL 1-
Types of database systems
• early types:– Hierarchical database– Netwerk database
• currently most common:– Relational database– Integration of XML
• developing:– Object oriented database
12DB&SQL 1-
Database: conceptual model of reality
???Which information items (attributes) may be useful for the entity
• BOOK• TREE
???
The choice of attributes dependson the properties of the entities
but alsoon the context
A database is a model of reality
13DB&SQL 1-
Relational db: example of a table
CODE LEV_NAAM ADRES WOONPLAATS004 Hovenier G.H. Zandweg 50 Lisse009 Baumgarten R. Taksstraat 13 Hillegom011 Struik BV Bessenlaan 1 Lisse013 Spitman en Zn. Achtertuin 9 Aalsmeer014 Dezaaier L.J.A. De Gronden 101 Lisse019 Mooiweer FA. Verlengde Zomerstr. 24 Aalsmeer
Each row represents the data of 1 supplierEach row is uniqueData are split up in simple items (comments??)
Tabel Leveranciers (=suppliers)
Scheme: Leveranciers (code, lev_naam, adres, woonplaats)
14DB&SQL 1-
The anatomy of a table / relation
Naam Adres Woonplaats
J.Jansen Poolweg 13 Gorkum
P.Putten Kaaplaan 6 Breda
J.Jansen Weerd 18 Breda
record, entiteitrecord, entityrij, tupelrow, tuple
kolomcolumn
attribuutwaardeattribute value
attribuut, veldnaamattribute, field namekolomkopcolumn heading
tabelkop, schema table heading, scheme
gegevensdata, recordset, body
NB An attribute has a data type and a domain
15DB&SQL 1-
Identification required
In a relational database, duplication of data must be prevented.Why?? What are the problems??
It is important to choose a sound identification, for current but also for possible future
Think of a appropriate identification for
students (in Progress)books (in the library)members of a hockey club?
16DB&SQL 1-
Identification problems
Which problems may occur by using
NAW-data (Naam Adres Woonplaats)ISBNday of birth?
Often, a system-created unique number is used as the primary key (primaire sleutel).It is easy and makes searching faster.But does it really discern your entities?
17DB&SQL 1-
Introduction of database design
• In most cases, one table is not enough for structured data storage
• The next slides show basic principles of database design• Later on, in Chapter 4, relational database design is
discussed at length
18DB&SQL 1-
Library as a flat table: redundant data
ISBN Title PubName PubPhone Price1-1111-1111-1 C++ Big House 123-456-7890 $29.950-99-999999-9 Emma Big House 123-456-7890 $20.000-91-335678-7 Fairie Queene Big House 123-456-7890 $15.000-91-045678-5 Hamlet Alpha Press 999-999-9999 $20.000-103-45678-9 Iliad Big House 123-456-7890 $25.000-12-345678-6 Jane Eyre Small House 714-000-0000 $49.000-99-777777-7 King Lear Alpha Press 999-999-9999 $49.000-555-55555-9 Macbeth Alpha Press 999-999-9999 $12.000-11-345678-9 Moby Dick Small House 714-000-0000 $49.000-12-333433-3 On Liberty Big House 123-456-7890 $25.000-321-32132-1 Balloon Small House 714-000-0000 $34.000-55-123456-9 Main Street Small House 714-000-0000 $22.950-123-45678-0 Ulysses Alpha Press 999-999-9999 $34.001-22-233700-0 Visual Basic Big House 123-456-7890 $25.00
Authordata
19DB&SQL 1-
Preventing redundancy
The table asserts several times that Big House has Phone xxx
Problems with data redundancy:file sizedata integrity (update/insert anomalies)
Solution:The phone number is an attribute of the publisher, not of the bookSo publisher is an entity on its ownBut how are book and publisher related??
20DB&SQL 1-
Relationship Publisher-Book: 1 to many
21DB&SQL 1-
Establishing a relationship
ISBN Title Price PubID0-321-32132-1 Balloon $34.00 31-1111-1111-1 C++ $29.95 10-99-999999-9 Emma $20.00 10-91-335678-7 Fairie Queene $15.00 10-91-045678-5 Hamlet $20.00 20-103-45678-9 Iliad $25.00 10-12-345678-6 Jane Eyre $49.00 30-99-777777-7 King Lear $49.00 2
BOOKS
PubID PubName PubPhone1 Big House 123-456-78902 Alpha Press 999-999-99993 Small House 714-000-0000
PUBLISHERS Publisher and book are distinct entity classes, they need distinct tables
Primary key / primaire sleutel
referencing key / verwijzende sleutel
22DB&SQL 1-
Author: a multivalued attributeprovisional solutions
ISBN Title Author Field1 Field20-321-32132-1 Balloon Sleepy A B0-321-32132-1 Balloon Snoopy A B0-321-32132-1 Balloon Grumpy A B
ISBN Title Author(s) Field1 Field20-321-32132-1 Balloon Sleepy, Snoopy, Grumpy A B
ISBN Title Author1 Author2 Author3 Field10-321-32132-1 Balloon Sleepy Snoopy Grumpy A
a multivalued field? a repeated field? repeating the bookrecord?
23DB&SQL 1-
... and the problems
ISBN Title Author(s) Field1 Field20-321-32132-1 Balloon Sleepy, Snoopy, Grumpy A B
Problems: find an author, sort on an author
ISBN Title Author1 Author2 Author3 Field10-321-32132-1 Balloon Sleepy Snoopy Grumpy A
Problems: find an author, sort on an author, number of author fields? empty fields
ISBN Title Author Field1 Field20-321-32132-1 Balloon Sleepy A B0-321-32132-1 Balloon Snoopy A B0-321-32132-1 Balloon Grumpy A B
Problems: redundancy: integrity problems, filesize
24DB&SQL 1-
The relational solution for multiple values
Authors are split up in an additional tableEach record connects one AUTHOR to a BOOK
ISBN Title Field1 Field20-321-32132-1 Balloon A B .. .. .. ..
Table BOOKS
ISBN Author0-321-32132-1 Sleepy0-321-32132-1 Snoopy0-321-32132-1 Grumpy0-91-045678-5 Shakespeare0-555-55555-9 Shakespeare.. ..
Table BOOK-AUTHOR
Rule: attributes contain only one simple value
25DB&SQL 1-
complex data
An author may have more attributes:
first namefamily namebirthday ….
how can we design a database for books and authors as separate entities?
what about the relationship?
26DB&SQL 1-
Relationship BOOK – AUTHOR: many to many
A1
A4
A2
A3
B1
B2
B3
B4
B5
27DB&SQL 1-
Library: Books and authorsAuID AuName AuPhone1 Austen 111-111-11112 Melville 222-222-22223 Homer 333-333-33334 Roman 444-444-44445 Shakespeare 555-555-55556 Joyce 666-666-66667 Spencer 777-777-77778 Mill 888-888-88889 Smith 123-222-222210 Jones 123-333-333311 Snoopy 321-321-222212 Grumpy 321-321-000013 Sleepy 321-321-1111
ISBN AuID0-99-999999-9 10-99-777777-7 50-11-345678-9 20-12-345678-6 10-91-335678-7 70-12-333433-3 81-22-233700-0 40-55-123456-9 90-55-123456-9 100-555-55555-9 50-123-45678-0 60-103-45678-9 30-91-045678-5 50-321-32132-1 110-321-32132-1 120-321-32132-1 131-1111-1111-1 4
ISBN Title … 0-321-32132-1 Balloon 0-91-045678-5 Hamlet 0-555-55555-9 Iliad .. ..
2 entities and a link table(tussentabel)
28DB&SQL 1-
Database design
Which entities, which attributes? Which primary key?Book: ISBN, Title, PricePublisher: PubID, PubName, PubPhoneAuthor: AuID, AuName, AuPhone
Which relations?A book has 1publisherA publisher publishes 1 or more books : infinite many
A book has 1 or more authorsAn author writes 1 of more books
U B1 -
S B -
29DB&SQL 1-
Entity-Relation Diagram (Ch 2)
Author
Book Publisherpublished by/
publishes
written by/writes
1
MethodStart from one record in a table and write the
relationship type at the other table (1 or )
30DB&SQL 1-
Homework
1. Read and note your questions about– Rolland: Ch 1, Ch 3.1Challenge: table 1.1 is not the right representation of the data in fig
1.5 and 1.6. Try to correct it!– Additional info: Brookshear 9.1 en 9.2
2. Think of a theme for your own database project (hobby, useful data, ..) and write down– which entities you discern (at least 3)– which attributes they have– what relationships exist among themCopy your notes and bring them next lecture to discuss and to hand
in!
Next week: chapter 2: ER diagrams