database concepts chapter 9 - rotaryspin.com€¦ · •chapter 9: mastering mysql •take cis 55...

27
DATABASE CONCEPTS CIS 086 • PHP and MySQL • Mission College 1

Upload: others

Post on 29-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

DATABASE CONCEPTS

CIS 086 • PHP and MySQL • Mission College

1

Page 2: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

FURTHER STUDY

• Chapter 9: Mastering MySQL

• Take CIS 55 and/or 56 for more information about:

• Relational databases

• Multiple tables

• Table Joins

• Foreign Keys

• Normalization

2

Page 3: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

DATABASE CONCEPTS

• A database is an ordered collection of information from which a computer program can quickly access information.

• A record (row) in a database is a single complete set of related information.

• A field (column) is the individual piece of information stored in a record.

3

Page 4: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

FLAT VS. RELATIONAL

• A flat-file database stores information in a single table.

• A relational database stores information across multiple related tables.

4

Page 5: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

RELATIONAL DATABASES

• Relational databases consist of one or more related tables.

• A primary table is the main table in a relationship that is referenced by another table.

• A related table references a primary table in a relational database.

• A primary key is a field that contains a unique identifier for each record in a primary table. It makes retrievals faster.

• A foreign key is a field in a related table that refers to the primary key in a primary table.

5

Page 6: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

NORMALIZATION

• The process of separating your data into tables and creating primary keys is called normalization. Its main goal is to make sure each piece of information appears in the database only once.

• Duplicating data is very inefficient, because it makes databases larger than they need to be and therefore slows down access.

• But, more importantly, the presence of duplicates creates a strong risk that you’ll update only one row of duplicated data, creating inconsistencies in a database and potentially causing serious errors.

6

Page 7: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

INEFFICIENT TABLE DESIGN

7

Page 8: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

FIRST NORMAL FORM

• First Normal Form deals with duplicate data (or redundancy) across multiple columns.

• Three rules:

• There should be no repeating columns containing the same kind of data.

• All columns should contain a single value.

• There should be a primary key to uniquely identify each row.

8

Page 9: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

AFTER FIRST NORMALIZATION

9

Page 10: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

SECOND NORMAL FORM

• Second Normal Form is all about redundancy across multiple rows.

• In order to achieve Second Normal Form, your tables must already be in First Normal Form.

• Once this has been done, we achieve Second Normal Form by identifying columns whose data repeats in different places and then removing them to their own tables.

10

Page 11: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

AFTER SECOND NORMALIZATION

11

Page 12: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

CUSTOMERS AND PURCHASES

12

Page 13: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

THIRD NORMAL FORM

• If you wish to be very strict with your database, you can use Third Normal Form, which requires that data that is not directly dependent on the primary key but is dependent on another value in the table should also be moved into separate tables.

• Ask yourself two questions:

• Is it likely that many new columns will need to be added to this table?

• Could any of this table’s fields require a global update at any point?

• If either of the answers is yes, you should probably consider performing this final stage of normalization.

13

Page 14: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

WHEN NOT TO USE NORMALIZATION

• Normalization requires spreading data across multiple tables, and this means making multiple calls to MySQL for each query.

• On a very popular site, if you have normalized tables, your database access will slow down considerably once you get above a few dozen concurrent users, because they will be creating hundreds of database accesses between them.

• You should denormalize any commonly looked-up data as much as you can.

14

Page 15: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

ONE-TO-ONE RELATIONSHIPS

• A one-to-one relationship exists between two tables when a related table contains exactly one record for each record in the primary table.

• Create one-to-one relationships to break information into multiple, logical sets.

• Information in the tables in a one-to-one relationship can be placed within a single table.

• Make the information in one of the tables confidential and accessible only by certain individuals.

15

Page 16: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

ONE-TO-ONE RELATIONSHIPS

16

Page 17: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

ONE-TO-MANY RELATIONSHIP

• A one-to-many relationship exists in a relational database when one record in a primary table has many related records in a related table.

• Breaking tables into multiple related tables to reduce redundant and duplicate information is called normalization.

• Normalization provides a more efficient and less redundant method of storing this information in a database.

17

Page 18: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

ONE-TO-MANY RELATIONSHIP

18

Page 19: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

MANY-TO-MANY RELATIONSHIP

• A many-to-many relationship exists in a relational database when many records in one table are related to many records in another table.

• A junction table creates a one-to-many relationship for each of the two tables in a many-to-many relationship.

• A junction table contains foreign keys from the two tables.

19

Page 20: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

MANY-TO-MANY RELATIONSHIP

20

Page 21: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

DATABASE MANAGEMENT SYSTEMS

• A database management system (or DBMS) is an application or collection of applications used to access and manage a database.

• A schema is the structure of a database including its tables, fields, and relationships.

• A flat-file database management system is a system that stores data in a flat-file format.

• A relational database management system (or RDBMS) is a system that stores data in a relational format.

21

Page 22: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

IMPORTANT ASPECTS OF DBMS

• The structuring and preservation of the database file

• Ensuring that data is stored correctly in a database’s tables, regardless of the database format

• Querying capability

22

Page 23: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

DATABASE MANAGEMENT SYSTEMS

• A query is a structured set of instructions and criteria for retrieving, adding, modifying, and deleting database information.

• Structured query language (or SQL) is a standard data manipulation language used among many database management systems.

• Open database connectivity (or ODBC) allows ODBC-compliant applications to access any data source for which there is an ODBC driver.

23

Page 24: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

TRANSACTIONS

• You must be using the InnoDB Engine

• Transaction sequence

• BEGIN or START TRANSACTION

• COMMIT

• ROLLBACK (cancel)

• Take CIS 55 and/or 56 for more information

24

Page 25: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

BACKING UP

• mysqldump -u user -p database [table] > mystuff.sql

• If you don’t choose a table, the resulting file will contain all tables in the database.

• You can lock the tables before dumping, and unlock when you are finished. This is only necessary if your database is live when you’re trying to back it up.

• https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#mysqldump-format-options

25

Page 26: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

RESTORING

• You can use the mysql command with the < redirection operator as shown on page 239 of the textbook.

• mysql -u user -p < mydatafile.sql

• You can also use the source command from within mysql monitor:

• source mydatafile.sql;

26

Page 27: Database Concepts Chapter 9 - rotaryspin.com€¦ · •Chapter 9: Mastering MySQL •Take CIS 55 and/or 56 for more information about: • Relational databases • Multiple tables

DUMPING DATA TO CSV

• mysqldump -u user -p --no-create-info --tab=c:/temp --fields-terminated-by=',' databasename

• You must type this all on one line.

• --no-create-info means it will not add table headers or schema.

• --tab means create a text file in the specified folder, and separate the fields with tabs (or other specified string)

• --fields-terminated-by=',’ means separate the fields with commas instead of tabs

27