21/10/2011
1
Introduc.on to Databases
G6921 and G6931 Web Technologies Dr. Séamus Lawless
• Course Structure – 1) Intro to the Web – 2) HTML – 3) HTML and CSS – Essay Informa.on Session – 4) Intro to Databases – 5) Intro to Databases – 6) PHP and MySQL – 7) Reading Week – 8) PHP and MySQL – 9) PHP and XML – 10) CMS – 11) Analy.cs – 12) Visualisa.on
• Course Structure – 1) Intro to the Web – 2) HTML – 3) HTML and CSS – 4) Intro to Databases – 5) Intro to Databases – 6) PHP and MySQL – 7) Reading Week – 8) PHP and MySQL – 9) PHP and XML – 10) CMS – 11) Analy.cs – 12) Visualisa.on
• Course Structure – 1) Intro to the Web – 2) HTML – 3) HTML and CSS – Essay Informa.on Session – 4) Intro to Databases – 5) Intro to Databases – 6) PHP and MySQL – 7) Reading Week – 8) PHP and MySQL – 9) PHP and XML – 10) CMS – 11) Analy.cs – 12) Visualisa.on
Housekeeping
21/10/2011
2
Housekeeping
• You all have a Laptop? • Can you all get connected to the Internet? • Can you all get connected to your webspace through SHH?
• Can you use a URL to view your files? • Assessment – Con.nuous Assessment • Upload the files you create!
Retrieving Web Pages
• Every page has a unique uniform resource locator (URL)
• Web pages are stored on web servers that use HTTP to exchange informa.on with client soaware
• e.g.,
21/10/2011
3
Retrieving Web Pages
• Client program (browser) uses the local machine to connect to a domain name system (DNS) server
• DNS server translates the hostname into an internet protocol (IP) address
• Browser then aeempts to connect to server host using specific port
• Aaer connec.on, browser sends a HTTP request to the web server to request a page – usually a GET request
Exercise 1
• Use the HTML file from last week’s Exercise 4 • Add a Background Colour to the whole page • Using an inline style to do this
21/10/2011
4
Exercise 2
• Take the HTML file from the previous exercise • Change the background colour of each sec.on of the page
• Using an internal style sheet to do this
Exercise 3
• Take the HTML file from the previous exercise • Change the font, font colour and font size in each sec.on of the page
• Use an external style sheet to do this
21/10/2011
5
What is Data? • Data is any informa.on that you want to store and refer to again. Data can be: – Text – Numbers – Dates – Images – Videos – Files – Any other types of informa.on.
• For example, if you sell cakes, you can store the names, pictures, and recipes of your cakes, the prices and quan..es of boxes and the dates of sales…..
What is a Database?
• An organised collec.on of Informa.on, or Data…
• Organised to: – model aspects of reality – in a way that supports processes that require this informa.on • A collec.on of Manuscripts in an archive • Finding manuscripts by a specific author and where those manuscripts are located
– mostly, to make the data more useful!
21/10/2011
6
Advantages • Search and Retrieval – Filtered according to specific needs
• Reduced Data Redundancy – Ease of Update
• Greater Data Integrity • Independence from Applica.ons, Concurrent Access
• Improved Data Security • Reduced Costs for Data Entry, Storage and Retrieval
Databases
• There are many Database Models – Oaen referred to as “Data Models”
• The four most common are: – Hierarchical – Network – Rela.onal • Object Rela.onal
– Graph
ST3001 – MySQL Tutorial 1 -‐ University of Dublin, Trinity
College
21/10/2011
7
Hierarchical Database
• One of the oldest database models – Commonly used in Mainframe compu.ng
• Organised hierarchically with parent and child nodes (like a family tree!)
Network Database
• Also have a hierarchical structure • Uses “members” and “owners” rather than “parents” and “children”.
• Each member can have more than one owner
21/10/2011
8
Rela.onal Database
• Differs from previous models as it is not Hierarchical, but Rela.onal
• More flexible than either the hierarchical or network database models.
• Uses no.ons of: – Rela.ons (Tables) – Tuples (Rows) – Aeributes (Columns)
Rela.onal Databases
• A table is the basic storage structure of a Rela.onal Database.
• A table holds all the data necessary about something in the real world – Name, Age, Address, PPS etc.
• Tables are “Two-‐Dimensional” • Each Rela.onal Database can contain one or many Tables
21/10/2011
9
EMPNO NAME JOB DEPTNO
7856 MCNULTY OFFICER 30
7710 DANIELS LIEUTENANT 40
7992 GREGGS DETECTIVE 10
7428 MORELAND DETECTIVE 20
ST3001 – MySQL Tutorial 1 -‐ University of Dublin, Trinity
College
DEPTNO NAME LOCATION
10 NARCOTICS TOWER 221
20 HOMICIDE CITY CENTRE
30 MARINE DOCKS
40 EVIDENCE DOWNTOWN
EMPNO NAME JOB MGR HIREDATE SALARY COMM DEPTNO
7839 KING PRESIDENT 17-‐NOV-‐81 5000 10
7698 BLAKE MANAGER 7839 01-‐MAY-‐81 2850 30
7782 CLARK MANAGER 7839 09-‐JUN-‐81 2450 10
7566 JONES MANAGER 7839 02-‐APR-‐81 2975 20
7654 MARTIN SALESMAN 7698 28-‐SEP-‐81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-‐FEB-‐81 1600 300 30
7844 TURNER SALESMAN 7698 08-‐SEP-‐81 1500 0 30
7900 JAMES CLERK 7698 03-‐DEC-‐81 950 30
7521 WARD SALESMAN 7698 22-‐FEB-‐81 1250 500 30
7902 FORD ANALYST 7566 03-‐DEC-‐81 3000 20
7369 SMITH CLERK 7902 17-‐DEC-‐80 800 20
7788 SCOTT ANALYST 7566 09-‐DEC-‐82 3000 20
7876 ADAMS CLERK 7788 12-‐JAN-‐83 1100 20
7934 MILLER CLERK 7782 23-‐JAN-‐82 1300 10
ST3001 – MySQL Tutorial 1 -‐ University of Dublin, Trinity
College
1
2
3
4
5
21/10/2011
10
Table Rela.onships
Foreign Key
Primary Key
ST3001 – MySQL Tutorial 1 -‐ University of Dublin, Trinity
College
EMPNO NAME JOB DEPTNO
7856 MCNULTY OFFICER 30
7710 DANIELS LIEUTENANT 40
7992 GREGGS DETECTIVE 10
7428 MORELAND DETECTIVE 20
DEPTNO NAME LOCATION
10 NARCOTICS TOWER 221
20 HOMICIDE CITY CENTER
30 MARINE DOCKS
40 EVIDENCE DOWNTOWN
Rela.onal Database Structure
• A Rela.onal Database can be thought of like a Filing Cabinet – Tables ≈ Drawers – Rows ≈ File Folders – Columns/Fields ≈ Sheets of Paper
– Data Values ≈ Words on the Paper
21/10/2011
11
Graph Database
• Uses a graph structure with: – Nodes – Edges – Proper.es
• Graph databases treat the rela.onship between things as equally important to the things themselves.
En.ty Rela.onship Diagrams
• An abstract and conceptual representa.on of informa.on
• Used to produce a “Conceptual Schema” of a Database – In simpler terms, it is used to iden.fy the requirements for, and support the design of a database
• Describes the type of informa.on that is to be stored in the database
21/10/2011
12
En..es, Rela.onships and Aeributes
• “An en4ty is a business object that represents a group, or category of data.” – Do we know a similar concept? – Each en.ty represents a table in the database
• “A rela4onship captures how two or more en..es are related to one another.”
• “An a6ribute is a sub-‐group of informa.on within an en.ty.” – Do we know a similar concept? – Each aeribute represents a column in a the table
Rela.onship Proper.es
• Rela.onship Link Phrase • Rela.onship Par.cipa.on – Mandatory – Op.onal
• Rela.onship Cardinality – Many-‐to-‐Many – One-‐to-‐Many – One-‐to-‐One – Recursive
21/10/2011
13
Link Phrase
• Provides a short descrip.on of the nature of the rela.onship
• It is always read clockwise with respect to the en..es that it links
Mandatory Rela.onships
21/10/2011
14
Op.onal Rela.onships
Mixed Par.cipa.on
21/10/2011
15
One to One
Many to Many
21/10/2011
16
One to Many
Recursive
21/10/2011
17
Many-‐to-‐Many Rela.onships
• Many-‐to-‐many rela.onships are difficult to encode in a rela.onal database
• They should be avoided where possible. • We can resolve a many-‐to-‐many rela.onship by: – Dividing it into two one-‐to-‐many rela.onships. – Using a linking table
Many-‐to-‐Many Rela.onships
• Think of the following example:
• Try to represent this using two tables and their aeributes
• We have a problem
21/10/2011
18
Many-‐to-‐Many Rela.onships
• We need a linking table:
• These types of rela.onships must be broken into mul.ple one-‐to-‐many rela.onships.
• This allows the data to be modelled correctly in the database
Examples • A customer can rent many DVDs from the LotsoVision store. Each of LotsoVision’s DVDs can be rented to many customers
• An airline company flies many flights, but each flight is flown by only one airline
• The Globex Corpora.on operates many factories. Each of these factories is located in a region. Each region can more than one Globex factory. Each factory employs many employees, but each of these employees is employed by only one factory.
21/10/2011
19
Data Types
• Character – char – varchar – text
• Date and Time – Date – Time – .mestamp
• Number – bit – integer – real – numeric
• Others – blob – boolean
DBMS
• Database Management System • Goal of a DBMS is to simplify the storage of, and access to data
• DBMS support: – Defini.on – Manipula.on – Querying
21/10/2011
20
DBMS
• Well known DBMS: – Proprietary • Oracle • Access (Microsoa) • SQL Server (Microsoa) • DB2 (IBM)
– Open Source • MySQL • SQLite • PostgreSQL
Database Languages
• Programming languages which are used to – Define a database (i.e. its en..es and the rela.onships between them)
– Manipulate its content (i.e. insert new data and update or delete exis.ng data)
– Conduct queries (i.e. request informa.on based upon defined criteria).
• The Structured Query Language (SQL) is the most commonly used language for Rela.onal Databases – Supported by all rela.onal DBMSs and is a standard.
21/10/2011
21
SQL
• SQL is split into four sets of commands which are divided based upon the tasks they are used for: – Data Defini.on Language – Data Modifica.on Language – Data Query Language – Data Control Language
Data Defini.on Language
• SQL uses a collec.on of impera.ve verbs whose effect is to modify the schema of the database
• Can be used to add, change or delete defini.ons of tables or other objects.
• These statements can be freely mixed with other SQL statements – so the DDL is not truly a separate language.
21/10/2011
22
Data Defini.on Language
• CREATE Statement – This Statement is used for crea.ng the database and its objects
• ALTER Statement – This Statement is used for modifying the database and its objects
• DROP Statement – This Statement is used for dele.ng the database and its objects
• TRUNCATE Statement – This Statement is used to delete the data available in a table in a Row-‐By-‐Row manner but with out disturbing its structure
Data Manipula.on Language
• The data manipula.on language comprises the SQL data change statements – Modifies stored data – Does NOT modify the schema or database objects • This is always the responsibility of the Data Defini.on Language
• Used for inser.ng, dele.ng and upda.ng data in the tables of a database
21/10/2011
23
Data Manipula.on Language
• INSERT Statement – This statement is used for inser.ng data into a table
• DELETE Statement – This statement is used to delete data from a table that matches certain criteria
• UPDATE Statement – This statement is used to update data in a table that matches certain criteria
Data Query Language • The data query language allows users of a database to formulate requests and generate reports
• There is one primary command used in SQL to query the database -‐ the SELECT Statement – This statement is used to query or retrieve data from a table in the database.
– A query may retrieve informa.on from specified columns or from all of the columns in the table
– A query may have specified criteria that must be met in order for data to be returned
21/10/2011
24
ST3001 – MySQL Tutorial 1 -‐ University of Dublin, Trinity
College
SELECT location FROM department;
Location ----------- Tower 221 The Docks City Centre Downtown
SQL Statement Entered
Returned Data is Displayed
Statement is sent to Database
Result Data is Returned