Databases
-
Upload
farrah-diaz -
Category
Documents
-
view
29 -
download
0
description
Transcript of Databases
![Page 1: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/1.jpg)
DatabasesDatabases
Flat FilesFlat Files
&&
Relational DatabasesRelational Databases
![Page 2: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/2.jpg)
Learning ObjectivesLearning Objectives
Describe flat files and databases.
Explain the advantages that using a relational database gives over flat files.
![Page 3: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/3.jpg)
Flat FilesFlat Files
![Page 4: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/4.jpg)
A Flat FileA Flat File
All the data is held in a single file which is All the data is held in a single file which is two dimensional.two dimensional.
Rows for records and columns for fields.Rows for records and columns for fields.
![Page 5: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/5.jpg)
E.g. A stock flat file E.g. A stock flat file
Field Name Data Type
Description String
Cost Price Currency
Selling Price Currency
Number in Stock Integer
Reorder Level Integer
Supplier Name String
Supplier Address String
![Page 6: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/6.jpg)
A Set / Series of Flat FilesA Set / Series of Flat Files
Flat files which have at least one common Flat files which have at least one common field which is duplicated but not linked.field which is duplicated but not linked.
![Page 7: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/7.jpg)
E.g. A Set / Series of Flat FilesE.g. A Set / Series of Flat Files
Purchasing Department
Programs to place orders when
stocks are low
File containing Stock Code, Description, Re-order level,
Cost Price, Sale Price, Supplier name and address, etc
Sales Department
Programs to record orders
from customers
File containing Stock Code, Description, Number sold,
Sale Price, Customer name and address, etc.
Accounts Department
Programs to record accounts
of customers
File containing Customer name and address, amount owing, dates of orders, etc.
![Page 8: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/8.jpg)
Limitations of Flat FilesLimitations of Flat Files
![Page 9: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/9.jpg)
Separation and isolation of dataSeparation and isolation of data
Which customers have bought parts Which customers have bought parts produced by a particular supplier?produced by a particular supplier? Find the parts supplied by a particular supplier Find the parts supplied by a particular supplier
from one file and then use a second file to find from one file and then use a second file to find which customers have bought those parts. which customers have bought those parts.
Problem compounded if data is needed Problem compounded if data is needed from more than two files.from more than two files.
![Page 10: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/10.jpg)
Duplication of dataDuplication of data
Supplier details duplicated if a supplier supplies Supplier details duplicated if a supplier supplies more than one part. more than one part. Customer details held in two different files.Customer details held in two different files.Data entered more than once, therefore time, Data entered more than once, therefore time, space and money are wasted.space and money are wasted.Loss of data integrity:Loss of data integrity: What happens if a customer changes his address? What happens if a customer changes his address?
The Sales Department may update their files but the The Sales Department may update their files but the Accounts Department may not do this at the same time. Accounts Department may not do this at the same time.
If the Order Department order some parts and there is If the Order Department order some parts and there is an increase in price. an increase in price.
The Order Department increases the Cost and Sale prices The Order Department increases the Cost and Sale prices but the Accounts Department do not, there is now a but the Accounts Department do not, there is now a discrepancy.discrepancy.
![Page 11: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/11.jpg)
Data dependenceData dependence
If there is a need to change data formats, If there is a need to change data formats, whole programs have to be changed. whole programs have to be changed.
Different applications may hold the data in Different applications may hold the data in different forms, again causing a problem. different forms, again causing a problem.
If an extra field is needed in a file, all If an extra field is needed in a file, all applications using that file have to be applications using that file have to be changed, even if they do not use that new changed, even if they do not use that new item of data.item of data.
![Page 12: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/12.jpg)
Incompatibility of filesIncompatibility of files
If one department writes its applications in If one department writes its applications in COBOL and another in C.COBOL and another in C. Departments cannot read each other’s files.Departments cannot read each other’s files.
![Page 13: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/13.jpg)
Fixed queries and the proliferation Fixed queries and the proliferation of application programsof application programs
Each time a new query is needed, a new Each time a new query is needed, a new program has to be written. program has to be written.
Difficult if the data needed to answer the Difficult if the data needed to answer the query is in more than one file.query is in more than one file. Compounded if some files are incompatible.Compounded if some files are incompatible.
![Page 14: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/14.jpg)
Relational DatabasesRelational Databases
Note: Note: The term database on its own means a The term database on its own means a relational database.relational database.
![Page 15: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/15.jpg)
Database Management System Database Management System ((DBMSDBMS))
Relational Databases use DBMS to Relational Databases use DBMS to provide a structure at three different provide a structure at three different levels:levels: Internal Level Internal Level Conceptual LevelConceptual Level External LevelExternal Level
![Page 16: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/16.jpg)
Conceptual LevelConceptual Level
1.1. Create two or more Create two or more logical/virtual tables logical/virtual tables or views and define or views and define one or more common one or more common linked fields called linked fields called relationships.relationships.
2.2. Set the data types Set the data types and write descriptions and write descriptions of each field.of each field.
3.3. Set validation.Set validation.
A manager uses a A manager uses a data description/definition language data description/definition language ((DDLDDL) to:) to:
http://databases.about.com/od/sql/a/sqlfundamentals_2.htm
![Page 17: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/17.jpg)
External LevelExternal Level
Users use a Users use a Data Manipulation LanguageData Manipulation Language ((DMLDML) to store, access, change, delete and ) to store, access, change, delete and search for data.search for data.
![Page 18: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/18.jpg)
DDataata M Manipulationanipulation L Language (anguage (DMLDML))INSERT
To add a new employee to a “personal_info” table:INSERT INTO personal_info values('bart','simpson',12345,$45000)
SELECTSelect everything from a “personal_info” table”:
SELECT * FROM personal_infoExtract a list of the last names of all employees in the company:
SELECT last_name FROM personal_infoWho has a salary value greater than $50,000?
SELECT * FROM personal_info WHERE salary > $50000
UPDATE Gives all employees a 3% cost-of-living increase in their salary:
UPDATE personal_info SET salary = salary * 1.03 An employee (ID = 12345) is due a $5,000 raise.
UPDATE personal_info SET salary = salary + $5000 WHERE employee_id = 12345
DELETE An employee (ID = 12345) has been laid off.
DELETE FROM personal_info WHERE employee_id = 12345
http://databases.about.com/od/sql/a/sqlfundamentals_3.htm http://en.wikipedia.org/wiki/Data_manipulation_language
![Page 19: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/19.jpg)
Internal Level Internal Level
The relational database The relational database physicallyphysically stores on stores on some form of storage medium all data in one file some form of storage medium all data in one file structure as a structure as a physical table physical table withwith aa data data dictionarydictionary.. Physical table:Physical table:
Contains all fields for all types of users Contains all fields for all types of users (e.g. departments)(e.g. departments).. Data dictionary:Data dictionary:
Holds information about the database necessary to enable it Holds information about the database necessary to enable it to work in different views for different types of users e.g. to work in different views for different types of users e.g. departments.departments.
Structures of logical / virtual tables or views, Relationships, Structures of logical / virtual tables or views, Relationships, Fields (names, data types, validation, descriptions etc…).Fields (names, data types, validation, descriptions etc…).
![Page 20: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/20.jpg)
Advantages of Relational Advantages of Relational DatabasesDatabases
Through the use of DBMS:Through the use of DBMS: Different logical/virtual tables or user views of Different logical/virtual tables or user views of
data can be created.data can be created.Views:Views:
A virtual or logical table composed of the result set of a A virtual or logical table composed of the result set of a query. query.
Unlike ordinary tables a view is not part of the physical Unlike ordinary tables a view is not part of the physical schema: it is a dynamic, virtual table computed or schema: it is a dynamic, virtual table computed or collated from data in the database.collated from data in the database.
Easier to access the data because data is Easier to access the data because data is now accessible through queries using a now accessible through queries using a DData ata MManipulation anipulation LLanguage (anguage (DMLDML).).
![Page 21: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/21.jpg)
Advantages of Relational Advantages of Relational DatabasesDatabases
All data can be accessed through this one All data can be accessed through this one relational database but different tables can relational database but different tables can be suited to different requirements (shows be suited to different requirements (shows only relevant information). only relevant information). Less duplication of data held because data Less duplication of data held because data is not duplicated across different files, is not duplicated across different files, consequently there is less danger of data consequently there is less danger of data integrity being compromised.integrity being compromised. Less chance of one copy of data altered when Less chance of one copy of data altered when
another is not.another is not.
![Page 22: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/22.jpg)
Advantages of Relational Advantages of Relational DatabasesDatabases
Data manipulation / input can be achieved Data manipulation / input can be achieved more quickly as there is only one copy of more quickly as there is only one copy of each piece of data.each piece of data.
Flat files need to be compatible, this Flat files need to be compatible, this problem does not arise with a database.problem does not arise with a database.
![Page 23: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/23.jpg)
Vocabulary for relational databasesVocabulary for relational databases
Fields Fields / columns/ columns = = attributesattributes
Records Records / rows/ rows = = tuplestuples
Tables = Tables = relationsrelations
![Page 24: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/24.jpg)
Table: Table: EMP EMP ((EmpIDEmpID,, NINumber NINumber,, Name Name,, Address Address))
The words in brackets separated by a The words in brackets separated by a ,, = Fields / Columns / Attributes = Fields / Columns / Attributes
Table Table descriptiondescription / / shorthand notationshorthand notation / / designdesign::
EmpID NINumber Name Address
Example table:Example table: Employee (Employee (EMPEMP) table for a company.) table for a company. Note that an NINumber is a unique number given by the UK government to every Note that an NINumber is a unique number given by the UK government to every
person who works in the UK, for tax purposes.person who works in the UK, for tax purposes.
![Page 25: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/25.jpg)
KeysKeys
![Page 26: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/26.jpg)
Primary KeysPrimary Keys
Primary Key:Primary Key: Unique attribute (or set of attributes) used to Unique attribute (or set of attributes) used to
identify the record or tuple.identify the record or tuple.E.g. EMP (E.g. EMP (EmpIDEmpID, NINumber, Name, Address), NINumber, Name, Address) EmpID & NINumber could be primary keys as they are EmpID & NINumber could be primary keys as they are
unique for each employee. However, it would be unique for each employee. However, it would be standard to choose standard to choose EmpIDEmpID to be the primary key. to be the primary key.
Note that it is conventional to underline the primary Note that it is conventional to underline the primary key and I will do so on the next few slides.key and I will do so on the next few slides.
![Page 27: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/27.jpg)
Foreign KeyForeign Key
An non-primary key attribute/field in one An non-primary key attribute/field in one table which is also the primary key in another table which is also the primary key in another table and so links the two tables together.table and so links the two tables together. E.g.E.g.
CINEMA (CINEMA (CIDCID, Cname, Loc, MID), Cname, Loc, MID) CID = Cinema ID, Cname = Cinema name, Loc = Location, MID = Manager IDCID = Cinema ID, Cname = Cinema name, Loc = Location, MID = Manager ID
&&
MANAGER (MANAGER (MIDMID, Mname), Mname) Mname = Manager nameMname = Manager name
MID occurs in CINEMA and is the primary key in MANAGER. MID occurs in CINEMA and is the primary key in MANAGER. When discussing CINEMA we say MID is the When discussing CINEMA we say MID is the foreign keyforeign key..
![Page 28: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/28.jpg)
Secondary KeysSecondary Keys
A different attribute A different attribute (other than the primary key)(other than the primary key) that allows the data that allows the data (the same record as the primary key or other groups of related records – (the same record as the primary key or other groups of related records – so so not necessarily uniquenot necessarily unique)) to be accessed in a different way. to be accessed in a different way.
E.g. EMP (E.g. EMP (EmpIDEmpID, NINumber, FName, LName, Address, Dept), NINumber, FName, LName, Address, Dept)NINumber could be a secondary key to allow access to the Employee’s NINumber could be a secondary key to allow access to the Employee’s record in a different way e.g. if EmpID is not known.record in a different way e.g. if EmpID is not known.Dept could also be a secondary key as it allows access to all employees in a Dept could also be a secondary key as it allows access to all employees in a particular department.particular department.
The setup of one or more secondary keys allows these fields to be The setup of one or more secondary keys allows these fields to be “indexed” so that searching by these fields is faster than ones that are “indexed” so that searching by these fields is faster than ones that are not set to be possible secondary keys.not set to be possible secondary keys.
For example, it is very unlikely that we would search the EMP table above by For example, it is very unlikely that we would search the EMP table above by FName only, so we would not set FName to be a secondary key FName only, so we would not set FName to be a secondary key (even though (even though it would still be possible to search by FName, it just would be slower than searching by it would still be possible to search by FName, it just would be slower than searching by a secondary key such as NINumber or Dept)a secondary key such as NINumber or Dept)..However, Secondary keys can be a combination of fields e.g. However, Secondary keys can be a combination of fields e.g. FName+LName to allow a record to be accessed if neither the EmpID or the FName+LName to allow a record to be accessed if neither the EmpID or the NINumber is known NINumber is known (but note that it is possible that two or more records share the (but note that it is possible that two or more records share the same FName+LName)same FName+LName)..
![Page 29: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/29.jpg)
PlenaryPlenary
What is meant by a flat file?What is meant by a flat file?
![Page 30: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/30.jpg)
PlenaryPlenary
All the data is held in a single file which is All the data is held in a single file which is two dimensional.two dimensional.
Rows for records and columns for fields.Rows for records and columns for fields.
![Page 31: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/31.jpg)
PlenaryPlenary
Describe the advantages of using a Describe the advantages of using a relational database over flat files. relational database over flat files.
![Page 32: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/32.jpg)
PlenaryPlenary
All data can be accessed through this one All data can be accessed through this one relational database but different tables can be relational database but different tables can be suited to different requirements (shows only suited to different requirements (shows only relevant information). relevant information).
Less duplication of data held because data is Less duplication of data held because data is not duplicated across different files, not duplicated across different files, consequently there is less danger of data consequently there is less danger of data integrity being compromised.integrity being compromised. Less chance of one copy of data altered when Less chance of one copy of data altered when
another is not.another is not.
![Page 33: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/33.jpg)
PlenaryPlenary
Data manipulation / input can be achieved Data manipulation / input can be achieved more quickly as there is only one copy of more quickly as there is only one copy of each piece of data.each piece of data.Flat files need to be compatible, this Flat files need to be compatible, this problem does not arise with a database.problem does not arise with a database.Through the use of DBMS:Through the use of DBMS: Different user views of data can be createdDifferent user views of data can be created Easier to access the data because data is Easier to access the data because data is
now accessible through relations / queries.now accessible through relations / queries.
![Page 34: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/34.jpg)
PlenaryPlenary
What is meant by terms primary key, What is meant by terms primary key, secondary key and foreign key?secondary key and foreign key?
![Page 35: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/35.jpg)
Primary KeysPrimary Keys
Primary Key:Primary Key: Unique attribute (or set of attributes) used to Unique attribute (or set of attributes) used to
identify the record or tuple.identify the record or tuple.E.g. EMP(EmpID, NINumber, Name, Address)E.g. EMP(EmpID, NINumber, Name, Address) EmpID & NINumber could be primary keys as they are EmpID & NINumber could be primary keys as they are
unique for each employee. unique for each employee.
![Page 36: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/36.jpg)
Secondary KeysSecondary Keys
SecondarySecondary A different attribute that allows the data to be A different attribute that allows the data to be
accessed in a different order.accessed in a different order.E.g. EMP(EmpID, NINumber, Name, Address)E.g. EMP(EmpID, NINumber, Name, Address)
If If EmpID is the primary key then NINumber is the EmpID is the primary key then NINumber is the secondary key.secondary key.
![Page 37: Databases](https://reader031.fdocuments.us/reader031/viewer/2022020417/5681355f550346895d9cc63c/html5/thumbnails/37.jpg)
Foreign KeysForeign Keys
ForeignForeign The primary key of another file / table / The primary key of another file / table /
relation that is used to link files / tables / relation that is used to link files / tables / relations together.relations together.
E.g.E.g. CINEMA(CINEMA(CIDCID, Cname, Loc, MID), Cname, Loc, MID)
&& MANAGER(MANAGER(MIDMID, MName), MName)
MID occurs in CINEMA and is the primary key in MID occurs in CINEMA and is the primary key in MANAGER. When discussing CINEMA we say MID MANAGER. When discussing CINEMA we say MID is the is the foreign keyforeign key..