SlideShare une entreprise Scribd logo
1  sur  44
Architecture
Request Page
Web Browser

Web Server

Read File

Send
HTML page
Generate
HTML page

Page with
PHP
code

Pass PHP page
and server variables
(GET attributes, Server settings, etc.)

PHP
Interpreter

Interact with
Database

MySQL
Introduction to MySQL




Relational databases
Database design
SQL




Creating databases
Creating tables
Selecting from, deleting, and updating tables



Exercises



You should have






Class notes
Exercise handout
MySQL Pocket Reference

Useful resource: http://dev.mysql.com/doc/
Basics of Databases
A database is made up of:







fields – a compilation of types of information or properties
records – a compilation of individual items with specific
values for the aforementioned information or properties

For example, a student record could contain fields
such as student id, name, rank, street address, city,
state, and zip code.
A specific record might have a student id of
12345678, name of Jane Smith, rank of sophomore,
street address of 9999 Buttermilk Lane, city of
Johnson City, state of Tennessee, and a zip code of
37601.
Relational Databases





A database may contain multiple tables too.
For example, a database used for a section
of a course may need to have a way to
identify a student (student ID), but would not
have to the student's personal information
Therefore, the university's database would
contain multiple tables:




Student information
Course information
Classroom information
Basics of Databases (continued)


All of this information is contained in tables
where the rows represent each record and the
columns represent the fields.
Relational Databases



A database is a collection of tables
Columns define attributes of the data




All data in a column must have the same data type

A record is stored in a row
table name
row

First Name
Nadia
Madhu
Ajuma
Wade
Helen

Employees
Last Name
Li
Charu
Kinsaka
Randal
Clark

Phone
2687
7856
4489
5257
2147

column
"Hey, a table! That's kinda like a
spreadsheet, right?"






Unlike a spreadsheet, the rows (records) of a
database must be independent of one another
Unlike a spreadsheet, the columns (fields) of a
database should be independent of one another
Example: Gradebook with columns for each quiz, test,
and homework grade.




Spreadsheet: one column might be used to compute the final
grade
Database: Cannot have a field for this. Instead, just before
you presented the data (results set), you would calculate a
final grade to be presented. That value is never stored in a
table.
Relational Databases (continued)
Student Table
•ID
•Name
•Z-account
•Rank
•Address
•Phone number

Instructor Table
•ID
•Name
•E-mail address
•Department
•Office location
•Office phone

Course Table
•Course ID
•Catalog description
•Credit hours
•List of topics

Course Section Table
•Course
•Section number
•Instructor
•Students
Use a Relational Database When…



You have a very large dataset
There is redundant data



Wastes disk space
Increases errors




Security is important




Information must be updated in multiple locations

Different users can be granted different permissions

Strict enforcement of data types is important
Spreadsheet Example
Title
A House for Mr. Biswas
Midnight's Children
On the Road

Author
VS Naipaul
Salman Rushdie
Jack Kerouac

Borrower
Sarah

Phone
646.555.1234
Spreadsheet Example
Title
A House for Mr. Biswas
Midnight's Children
On the Road

Author
VS Naipaul
Salman Rushdie
Jack Kerouac

Borrower
Sarah

Phone
646.555.1234

One Flew Over the Cuckoo's Nest
Sula
Villette

Ken Kesey
Toni Morrison
Charlotte Bronte

Sarah

646.555.1244

Jim

646.555.4586

Data is inconsistent!

Now imagine you are designing the New York Public
Library database which has tens of million books and
well over a million cardholders.
Database Design
Entity Relationship Design
Entity (“thing”, “object”)

Table

Attributes (describe entity)

Columns

Entity Instance

Row

Relationships between entities preserved in relationships
between tables.
If you are interested in learning more formal design
methods look up “normalization” and/or “third normal
form”.
Keys




A key is a field by which records may be
sorted.
There are a number of uses for keys:






A primary key can be used to uniquely identify a
record.
A common key is a key shared by two tables in a
relational database.
A foreign key is a common key that can be used
to identify records from another table.
Primary Keys








Each record within a table must somehow be uniquely
identifiable.
For example, how can we make sure that we're looking
at the correct student information in the student table?
Answer: No two students share the same student id.
Siblings may have the same parents, roommates may
have the same address, but no one has identical student
IDs.
Therefore, we can use a field containing the student id to
identify a specific record in the student database.
This unique identification is called the Primary Key.
Our tables
• A primary key is a unique identifier for a record in a

table.
• A foreign key in one table refers to the primary key of
another.
Simple Relational Database Example
Course Table
Department

Course

Section

Semester

Year

Instructor

CSCI

2800

001

Spring

2006

2

CSCI

2800

201

Spring

2006

1

CSCI

2910

001

Spring

2006

4

CSCI

2910

201

Spring

2006

3

Primary
keys

Instructor Table
ID

Name

E-mail

Phone

1

Bailes

bailes@etsu.edu

423.439.6958

2

Bailey

baileyg@etsu.edu

423.439.6959

3

Laws

lawsm@etsu.edu

423.439.6952

4

Tarnoff

tarnoff@etsu.edu

423.439.6404
Databases and the Client/Server Model







Database systems typically reside on the server, but
are not as part of the software providing server
functionality.
An interface must exist between server software and
the database.
Three tier architecture – Server/client model adds
middle layer that handles transactions between client
and database server.
Middle layer provides:






ability to access more than one database with a single
transaction
ability connect to many different types of data sources
ability to prioritize requests before they reach the data base
improved security
What is SQL?
(Adapted from material found at http://www.atlasindia.com/sql.htm)
 Dr. Edgar F. Codd created a model for data





storage that used a simple programming
language to access the stored data
In 1971, IBM used Dr. Codd's work to created
a simple non-procedural language called
Structured English Query Language
(SEQUEL)
In the late 80's, two standardization
organizations (ANSI and ISO) developed a
standardized version called Structured Query
Language or SQL.
What is SQL? (continued)
(Adapted from material found at http://www.atlasindia.com/sql.htm)






SQL is the language used to query all databases.
It is a generic way to access the information in a
database.
Understanding SQL is vital to creating a
database application such as a web interface.

Web
Application

SQL

Database
Different SQL Implementations






There are multiple vendors of database
products, each with their own implementation
of SQL
Each product should be compliant with ANSI
standard
Added features or commands do exists.
These are called extensions.
Using SQL




Assume that a database structure already
exists, i.e., someone has already created
tables for us containing fields and records.
What sort of things might we want to do to
this database?






Start/end a session with a specific database
Read a record
Insert a new record
Delete an existing record
Edit and restore an existing record
Basic SQL Syntax
➔ Data Definition Language (DDL)
• CREATE TABLE / DATABASE / VIEW / etc.....
• ALTER ...
• DROP ...
➔ Data Manipulation Language (DML)
•
•
•
•

SELECT ... FROM / INTO … WHERE ...
INSERT INTO ... VALUES ...
UPDATE … SET … WHERE ...
DELETE FROM … WHERE ...
Querying Records






A query is an inquiry to the database for information.
This is done with SELECT.
Syntax:
SELECT *| fieldname [, fieldnames]
FROM tablename [, tablenames] WHERE
fieldname=value ORDER BY fieldname [,
fieldnames]
Example:
SELECT FirstName FROM Customers WHERE
LastName='Smith'
Data Manipulation
There are three basic commands to
manipulate data:




INSERT
DELETE
UPDATE
Adding a Record


Syntax:
INSERT INTO tablename (fieldname [,
fieldnames]) VALUES (value [,
values])



Example:
INSERT INTO Customers (FirstName,
LastName) VALUES ('Jane','Smith')
Removing a Record


Syntax:
DELETE FROM tablename WHERE
fieldname=value



Example:
DELETE FROM Customers WHERE
LastName='Jones'
Updating a Record


Syntax:
UPDATE tablename SET fieldname=value
WHERE fieldname=value



Example:
UPDATE Customers SET
FirstName='Jeff' WHERE
LastName='Smith'
What Is Database Normalization?






Cures the ‘SpreadSheet Syndrome’
Store only the minimal amount of
information.
Remove redundancies.
Restructure data.
What are the Benefits
of Database Normalization?







Decreased storage requirements!
1 VARCHAR(20)
converted to
1 TINYINT UNSIGNED
in a table of
1 million rows
is a savings of
~20 MB
Faster search performance!





Smaller file for table scans.
More directed searching.

Improved data integrity!
What are the Normal Forms?







First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)
Boyce-Codd Normal Form (BCNF)
Fourth Normal Form (4NF)
Fifth Normal Form (5NF)
Our Table
user

name
nickname
phone1
phone2
phone3
cell
pager
address
city
province
postal_code
country
email1
email2
web_url
company
department
picture
notes
email_format

name

phone1

phone2

email1

email2

Mike
Hillyer

403-5551717

403-5551919

mike@hoppen.c
om

mhillyer@mysite.
com

Tom
Jensen

403-5551919

403-5551313

tom@openwin.o
rg

tom@supersite.o
rg

Ray Smith

403-5551919

403-5551111

ray@cpma.com
First Normal Form


Remove horizontal redundancies





Each row must be unique




No two columns hold the same information
No single column holds more than a single
item
Use a primary key

Benefits




Easier to query/sort the data
More scalable
Each row can be identified for updating
One Solution
user

first_name last_name phone
first_name
last_name
nickname
phone
cell
pager
address
city
province
postal_code
country
web_url
department
picture
notes

email

Mike

Hillyer

403-555-1717

mike@hoppen.com

Mike

Hillyer

403-555-1919

mhillyer@mysite.com

Tom

Jensen

403-555-1919

tom@openwin.org

Tom

Jensen

403-555-1313

tom@supersite.org

Ray

Smith

403-555-1919

ray@cpma.com

Ray

Smith

403-555-1111

•
•
•

Multiple rows per user
Emails are associated with only one other phone
Hard to Search
Satisfying 1NF
user
PK

user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
company
department
picture
notes

email
PK

email_id
address

phone
PK

phone_id
country_code
number
extension
Forming Relationships


Three Forms






One to One




Same Table?

One to Many




One to (zero or) One
One to (zero or) Many
Many to Many

Place PK of the One in the Many

Many to Many


Create a joining table
Joining Tables
user
PK

user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
email_format

phone

user_phone
PK,FK1 phone_id
PK
user_id
type

email
PK

address

FK1

user_id

PK

phone_id
country_code
number
extension
Our User Table
first_name last_name company

department

Mike

Hillyer

MySQL

Documentation

Tom

Jensen

CPNS

Finance

Ray

Smith

CPNS

Documentation

user
PK

user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes
email_format

phone

user_phone
PK,FK1 phone_id
PK
user_id
type

email
PK

address

FK1

user_id

PK

phone_id
country_code
number
extension
Second Normal Form



Table must be in First Normal Form
Remove vertical redundancy




Composite keys




The same value should not repeat across rows
All columns in a row must refer to BOTH parts
of the key

Benefits



Increased storage efficiency
Less data repetition
Satisfying 2NF

email
email
PK address
PK address
type
FK1 user_id
FK1 user_id

user
user
PK user_id
PK user_id
first_name
last_name
first_name
nickname
last_name
address
nickname
city
address
province
city
postal_code
province
country
postal_code
web_url
country
picture
web_url
notes
picture
email_format
notes

phone

user_phone
PK,FK1 user_id
PK,FK2 phone_id

PK

phone_id
country_code
number
extension
type

user_company
PK,FK1 user_id
PK,FK2 company_id
department

company
PK

company_id
name
Third Normal Form


Table must be in Second Normal Form






If your table is 2NF, there is a good chance it is
3NF

All columns must relate directly to the
primary key
Benefits


No extraneous data
Satisfying 3NF
user_phone
user
PK

email
PK

address

FK1

user_id
format

user_id
first_name
last_name
nickname
address
city
province
postal_code
country
web_url
picture
notes

PK,FK1
PK,FK2

user_id
phone_id

phone
PK

phone_id
country_code
number
type

extension

user_company
PK,FK1
PK,FK2

user_id
company_id
department

company
PK

company_id
name
Finding Balance
user

user_phone

PK

user_id

FK1

first_name
last_name
nickname
unit
street_number
street_name
street_type
quadrant
web_url
picture
notes
postal_code

PK,FK1 user_id
PK,FK2 phone_id
extension

email
PK

user_id
format

PK

phone_id

FK1

type_id
area_code
NXX
NCX
country_id

FK2

country

type
PK

type_id

PK

country_id
Name
phone_code

type

address

FK1

phone

user_department
PK,FK1
PK,FK2

user_id
department_id

department

PK

postal_code

FK1

city

city_id

PK

city_id

FK1

department_id

FK1
postal_code

PK

name
company_id

province

name
province_id

PK

province_id

FK1

Name
Abbreviation
country_id

company
PK

company_id
name
Joining Tables


Two Basic Joins





Equi-Join






Equi-Join
Outer Join (LEFT JOIN)
SELECT user.first_name, user.last_name, email.address
FROM user, email
WHERE user.user_id = email.user_id

LEFT JOIN




SELECT user.first_name, user.last_name, email.address
FROM user LEFT JOIN email
ON user.user_id = email.user_id
De-Normalizing Tables





Use with caution
Normalize first, then de-normalize
Use only when you cannot optimize
Try temp tables, UNIONs, VIEWs,
subselects first

Contenu connexe

Tendances

Lect 30 dbms_fundamentals
Lect 30  dbms_fundamentalsLect 30  dbms_fundamentals
Lect 30 dbms_fundamentalsProtik Roy
 
Database Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance ProfessionalsDatabase Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance ProfessionalsBrad Adams
 
Unit 4 rdbms study_material
Unit 4  rdbms study_materialUnit 4  rdbms study_material
Unit 4 rdbms study_materialgayaramesh
 
Introduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQLIntroduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQLHarmony Kwawu
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Beat Signer
 
9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMSkoolkampus
 
MS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database ConceptsMS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database ConceptsDataminingTools Inc
 
Computer science 2nd year short questions notes (1)
Computer science 2nd year short questions notes (1)Computer science 2nd year short questions notes (1)
Computer science 2nd year short questions notes (1)umarsajjad18
 
Intro To Databases Y7
Intro To Databases Y7Intro To Databases Y7
Intro To Databases Y7Edificat0r
 
Relational D Bs3
Relational D Bs3Relational D Bs3
Relational D Bs3kumar2
 

Tendances (16)

Lect 30 dbms_fundamentals
Lect 30  dbms_fundamentalsLect 30  dbms_fundamentals
Lect 30 dbms_fundamentals
 
Database Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance ProfessionalsDatabase Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance Professionals
 
Unit 4 rdbms study_material
Unit 4  rdbms study_materialUnit 4  rdbms study_material
Unit 4 rdbms study_material
 
Introduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQLIntroduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQL
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 
9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS
 
MS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database ConceptsMS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database Concepts
 
Interaction
InteractionInteraction
Interaction
 
603s129
603s129603s129
603s129
 
Computer science 2nd year short questions notes (1)
Computer science 2nd year short questions notes (1)Computer science 2nd year short questions notes (1)
Computer science 2nd year short questions notes (1)
 
Intro To Databases Y7
Intro To Databases Y7Intro To Databases Y7
Intro To Databases Y7
 
Database
DatabaseDatabase
Database
 
Database management system session 6
Database management system session 6Database management system session 6
Database management system session 6
 
Relational D Bs3
Relational D Bs3Relational D Bs3
Relational D Bs3
 
Ordbms
OrdbmsOrdbms
Ordbms
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 

Similaire à My sql

Similaire à My sql (20)

Advanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptxAdvanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptx
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptx
 
Info systems databases
Info systems databasesInfo systems databases
Info systems databases
 
DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEM
 
Dbms Basics
Dbms BasicsDbms Basics
Dbms Basics
 
dbms-1.pptx
dbms-1.pptxdbms-1.pptx
dbms-1.pptx
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-converted
 
Database Systems - Lecture Week 1
Database Systems - Lecture Week 1Database Systems - Lecture Week 1
Database Systems - Lecture Week 1
 
Training MS Access 2007
Training MS Access 2007Training MS Access 2007
Training MS Access 2007
 
RDMS AND SQL
RDMS AND SQLRDMS AND SQL
RDMS AND SQL
 
Database Concepts and Components
Database Concepts and ComponentsDatabase Concepts and Components
Database Concepts and Components
 
Database concepts presentation version 2010 revised
Database concepts presentation version 2010 revisedDatabase concepts presentation version 2010 revised
Database concepts presentation version 2010 revised
 
Data base.ppt
Data base.pptData base.ppt
Data base.ppt
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Dbms Lec Uog 02
Dbms Lec Uog 02Dbms Lec Uog 02
Dbms Lec Uog 02
 
Databases and its representation
Databases and its representationDatabases and its representation
Databases and its representation
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
 
Pl sql content
Pl sql contentPl sql content
Pl sql content
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Database_Introduction.pdf
Database_Introduction.pdfDatabase_Introduction.pdf
Database_Introduction.pdf
 

Dernier

Global Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride ConsultingGlobal Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride Consultingswastiknandyofficial
 
Gender and caste discrimination in india
Gender and caste discrimination in indiaGender and caste discrimination in india
Gender and caste discrimination in indiavandanasingh01072003
 
Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024Devarsh Vakil
 
The AES Investment Code - the go-to counsel for the most well-informed, wise...
The AES Investment Code -  the go-to counsel for the most well-informed, wise...The AES Investment Code -  the go-to counsel for the most well-informed, wise...
The AES Investment Code - the go-to counsel for the most well-informed, wise...AES International
 
The Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance LeaderThe Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance LeaderArianna Varetto
 
Liquidity Decisions in Financial management
Liquidity Decisions in Financial managementLiquidity Decisions in Financial management
Liquidity Decisions in Financial managementshrutisingh143670
 
AnyConv.com__FSS Advance Retail & Distribution - 15.06.17.ppt
AnyConv.com__FSS Advance Retail & Distribution - 15.06.17.pptAnyConv.com__FSS Advance Retail & Distribution - 15.06.17.ppt
AnyConv.com__FSS Advance Retail & Distribution - 15.06.17.pptPriyankaSharma89719
 
10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdfglobusfinanza
 
2024 Q1 Crypto Industry Report | CoinGecko
2024 Q1 Crypto Industry Report | CoinGecko2024 Q1 Crypto Industry Report | CoinGecko
2024 Q1 Crypto Industry Report | CoinGeckoCoinGecko
 
Financial analysis on Risk and Return.ppt
Financial analysis on Risk and Return.pptFinancial analysis on Risk and Return.ppt
Financial analysis on Risk and Return.ppttadegebreyesus
 
Kempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdfKempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdfHenry Tapper
 
Hello this ppt is about seminar final project
Hello this ppt is about seminar final projectHello this ppt is about seminar final project
Hello this ppt is about seminar final projectninnasirsi
 
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...beulahfernandes8
 
Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.Precize Formely Leadoff
 
NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...
NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...
NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...Amil baba
 
Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward
 
Financial Preparation for Millennia.pptx
Financial Preparation for Millennia.pptxFinancial Preparation for Millennia.pptx
Financial Preparation for Millennia.pptxsimon978302
 
Banking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxBanking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxANTHONYAKINYOSOYE1
 
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...Amil baba
 

Dernier (20)

Global Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride ConsultingGlobal Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride Consulting
 
Gender and caste discrimination in india
Gender and caste discrimination in indiaGender and caste discrimination in india
Gender and caste discrimination in india
 
Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024
 
The AES Investment Code - the go-to counsel for the most well-informed, wise...
The AES Investment Code -  the go-to counsel for the most well-informed, wise...The AES Investment Code -  the go-to counsel for the most well-informed, wise...
The AES Investment Code - the go-to counsel for the most well-informed, wise...
 
The Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance LeaderThe Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance Leader
 
Liquidity Decisions in Financial management
Liquidity Decisions in Financial managementLiquidity Decisions in Financial management
Liquidity Decisions in Financial management
 
AnyConv.com__FSS Advance Retail & Distribution - 15.06.17.ppt
AnyConv.com__FSS Advance Retail & Distribution - 15.06.17.pptAnyConv.com__FSS Advance Retail & Distribution - 15.06.17.ppt
AnyConv.com__FSS Advance Retail & Distribution - 15.06.17.ppt
 
10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf
 
2024 Q1 Crypto Industry Report | CoinGecko
2024 Q1 Crypto Industry Report | CoinGecko2024 Q1 Crypto Industry Report | CoinGecko
2024 Q1 Crypto Industry Report | CoinGecko
 
Financial analysis on Risk and Return.ppt
Financial analysis on Risk and Return.pptFinancial analysis on Risk and Return.ppt
Financial analysis on Risk and Return.ppt
 
Kempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdfKempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdf
 
Hello this ppt is about seminar final project
Hello this ppt is about seminar final projectHello this ppt is about seminar final project
Hello this ppt is about seminar final project
 
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
 
Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.
 
NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...
NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...
NO1 Certified kala jadu karne wale ka contact number kala jadu karne wale bab...
 
Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024
 
Financial Preparation for Millennia.pptx
Financial Preparation for Millennia.pptxFinancial Preparation for Millennia.pptx
Financial Preparation for Millennia.pptx
 
Banking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxBanking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptx
 
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
 

My sql

  • 1. Architecture Request Page Web Browser Web Server Read File Send HTML page Generate HTML page Page with PHP code Pass PHP page and server variables (GET attributes, Server settings, etc.) PHP Interpreter Interact with Database MySQL
  • 2. Introduction to MySQL    Relational databases Database design SQL    Creating databases Creating tables Selecting from, deleting, and updating tables  Exercises  You should have     Class notes Exercise handout MySQL Pocket Reference Useful resource: http://dev.mysql.com/doc/
  • 3. Basics of Databases A database is made up of:     fields – a compilation of types of information or properties records – a compilation of individual items with specific values for the aforementioned information or properties For example, a student record could contain fields such as student id, name, rank, street address, city, state, and zip code. A specific record might have a student id of 12345678, name of Jane Smith, rank of sophomore, street address of 9999 Buttermilk Lane, city of Johnson City, state of Tennessee, and a zip code of 37601.
  • 4. Relational Databases    A database may contain multiple tables too. For example, a database used for a section of a course may need to have a way to identify a student (student ID), but would not have to the student's personal information Therefore, the university's database would contain multiple tables:    Student information Course information Classroom information
  • 5. Basics of Databases (continued)  All of this information is contained in tables where the rows represent each record and the columns represent the fields.
  • 6. Relational Databases   A database is a collection of tables Columns define attributes of the data   All data in a column must have the same data type A record is stored in a row table name row First Name Nadia Madhu Ajuma Wade Helen Employees Last Name Li Charu Kinsaka Randal Clark Phone 2687 7856 4489 5257 2147 column
  • 7. "Hey, a table! That's kinda like a spreadsheet, right?"    Unlike a spreadsheet, the rows (records) of a database must be independent of one another Unlike a spreadsheet, the columns (fields) of a database should be independent of one another Example: Gradebook with columns for each quiz, test, and homework grade.   Spreadsheet: one column might be used to compute the final grade Database: Cannot have a field for this. Instead, just before you presented the data (results set), you would calculate a final grade to be presented. That value is never stored in a table.
  • 8. Relational Databases (continued) Student Table •ID •Name •Z-account •Rank •Address •Phone number Instructor Table •ID •Name •E-mail address •Department •Office location •Office phone Course Table •Course ID •Catalog description •Credit hours •List of topics Course Section Table •Course •Section number •Instructor •Students
  • 9. Use a Relational Database When…   You have a very large dataset There is redundant data   Wastes disk space Increases errors   Security is important   Information must be updated in multiple locations Different users can be granted different permissions Strict enforcement of data types is important
  • 10. Spreadsheet Example Title A House for Mr. Biswas Midnight's Children On the Road Author VS Naipaul Salman Rushdie Jack Kerouac Borrower Sarah Phone 646.555.1234
  • 11. Spreadsheet Example Title A House for Mr. Biswas Midnight's Children On the Road Author VS Naipaul Salman Rushdie Jack Kerouac Borrower Sarah Phone 646.555.1234 One Flew Over the Cuckoo's Nest Sula Villette Ken Kesey Toni Morrison Charlotte Bronte Sarah 646.555.1244 Jim 646.555.4586 Data is inconsistent! Now imagine you are designing the New York Public Library database which has tens of million books and well over a million cardholders.
  • 12. Database Design Entity Relationship Design Entity (“thing”, “object”) Table Attributes (describe entity) Columns Entity Instance Row Relationships between entities preserved in relationships between tables. If you are interested in learning more formal design methods look up “normalization” and/or “third normal form”.
  • 13. Keys   A key is a field by which records may be sorted. There are a number of uses for keys:    A primary key can be used to uniquely identify a record. A common key is a key shared by two tables in a relational database. A foreign key is a common key that can be used to identify records from another table.
  • 14. Primary Keys       Each record within a table must somehow be uniquely identifiable. For example, how can we make sure that we're looking at the correct student information in the student table? Answer: No two students share the same student id. Siblings may have the same parents, roommates may have the same address, but no one has identical student IDs. Therefore, we can use a field containing the student id to identify a specific record in the student database. This unique identification is called the Primary Key.
  • 15. Our tables • A primary key is a unique identifier for a record in a table. • A foreign key in one table refers to the primary key of another.
  • 16. Simple Relational Database Example Course Table Department Course Section Semester Year Instructor CSCI 2800 001 Spring 2006 2 CSCI 2800 201 Spring 2006 1 CSCI 2910 001 Spring 2006 4 CSCI 2910 201 Spring 2006 3 Primary keys Instructor Table ID Name E-mail Phone 1 Bailes bailes@etsu.edu 423.439.6958 2 Bailey baileyg@etsu.edu 423.439.6959 3 Laws lawsm@etsu.edu 423.439.6952 4 Tarnoff tarnoff@etsu.edu 423.439.6404
  • 17. Databases and the Client/Server Model     Database systems typically reside on the server, but are not as part of the software providing server functionality. An interface must exist between server software and the database. Three tier architecture – Server/client model adds middle layer that handles transactions between client and database server. Middle layer provides:     ability to access more than one database with a single transaction ability connect to many different types of data sources ability to prioritize requests before they reach the data base improved security
  • 18. What is SQL? (Adapted from material found at http://www.atlasindia.com/sql.htm)  Dr. Edgar F. Codd created a model for data   storage that used a simple programming language to access the stored data In 1971, IBM used Dr. Codd's work to created a simple non-procedural language called Structured English Query Language (SEQUEL) In the late 80's, two standardization organizations (ANSI and ISO) developed a standardized version called Structured Query Language or SQL.
  • 19. What is SQL? (continued) (Adapted from material found at http://www.atlasindia.com/sql.htm)    SQL is the language used to query all databases. It is a generic way to access the information in a database. Understanding SQL is vital to creating a database application such as a web interface. Web Application SQL Database
  • 20. Different SQL Implementations    There are multiple vendors of database products, each with their own implementation of SQL Each product should be compliant with ANSI standard Added features or commands do exists. These are called extensions.
  • 21. Using SQL   Assume that a database structure already exists, i.e., someone has already created tables for us containing fields and records. What sort of things might we want to do to this database?      Start/end a session with a specific database Read a record Insert a new record Delete an existing record Edit and restore an existing record
  • 22. Basic SQL Syntax ➔ Data Definition Language (DDL) • CREATE TABLE / DATABASE / VIEW / etc..... • ALTER ... • DROP ... ➔ Data Manipulation Language (DML) • • • • SELECT ... FROM / INTO … WHERE ... INSERT INTO ... VALUES ... UPDATE … SET … WHERE ... DELETE FROM … WHERE ...
  • 23. Querying Records    A query is an inquiry to the database for information. This is done with SELECT. Syntax: SELECT *| fieldname [, fieldnames] FROM tablename [, tablenames] WHERE fieldname=value ORDER BY fieldname [, fieldnames] Example: SELECT FirstName FROM Customers WHERE LastName='Smith'
  • 24. Data Manipulation There are three basic commands to manipulate data:    INSERT DELETE UPDATE
  • 25. Adding a Record  Syntax: INSERT INTO tablename (fieldname [, fieldnames]) VALUES (value [, values])  Example: INSERT INTO Customers (FirstName, LastName) VALUES ('Jane','Smith')
  • 26. Removing a Record  Syntax: DELETE FROM tablename WHERE fieldname=value  Example: DELETE FROM Customers WHERE LastName='Jones'
  • 27. Updating a Record  Syntax: UPDATE tablename SET fieldname=value WHERE fieldname=value  Example: UPDATE Customers SET FirstName='Jeff' WHERE LastName='Smith'
  • 28. What Is Database Normalization?     Cures the ‘SpreadSheet Syndrome’ Store only the minimal amount of information. Remove redundancies. Restructure data.
  • 29. What are the Benefits of Database Normalization?       Decreased storage requirements! 1 VARCHAR(20) converted to 1 TINYINT UNSIGNED in a table of 1 million rows is a savings of ~20 MB Faster search performance!    Smaller file for table scans. More directed searching. Improved data integrity!
  • 30. What are the Normal Forms?       First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) Boyce-Codd Normal Form (BCNF) Fourth Normal Form (4NF) Fifth Normal Form (5NF)
  • 32. First Normal Form  Remove horizontal redundancies    Each row must be unique   No two columns hold the same information No single column holds more than a single item Use a primary key Benefits    Easier to query/sort the data More scalable Each row can be identified for updating
  • 33. One Solution user first_name last_name phone first_name last_name nickname phone cell pager address city province postal_code country web_url department picture notes email Mike Hillyer 403-555-1717 mike@hoppen.com Mike Hillyer 403-555-1919 mhillyer@mysite.com Tom Jensen 403-555-1919 tom@openwin.org Tom Jensen 403-555-1313 tom@supersite.org Ray Smith 403-555-1919 ray@cpma.com Ray Smith 403-555-1111 • • • Multiple rows per user Emails are associated with only one other phone Hard to Search
  • 35. Forming Relationships  Three Forms     One to One   Same Table? One to Many   One to (zero or) One One to (zero or) Many Many to Many Place PK of the One in the Many Many to Many  Create a joining table
  • 37. Our User Table first_name last_name company department Mike Hillyer MySQL Documentation Tom Jensen CPNS Finance Ray Smith CPNS Documentation user PK user_id first_name last_name nickname address city province postal_code country web_url picture notes email_format phone user_phone PK,FK1 phone_id PK user_id type email PK address FK1 user_id PK phone_id country_code number extension
  • 38. Second Normal Form   Table must be in First Normal Form Remove vertical redundancy   Composite keys   The same value should not repeat across rows All columns in a row must refer to BOTH parts of the key Benefits   Increased storage efficiency Less data repetition
  • 39. Satisfying 2NF email email PK address PK address type FK1 user_id FK1 user_id user user PK user_id PK user_id first_name last_name first_name nickname last_name address nickname city address province city postal_code province country postal_code web_url country picture web_url notes picture email_format notes phone user_phone PK,FK1 user_id PK,FK2 phone_id PK phone_id country_code number extension type user_company PK,FK1 user_id PK,FK2 company_id department company PK company_id name
  • 40. Third Normal Form  Table must be in Second Normal Form    If your table is 2NF, there is a good chance it is 3NF All columns must relate directly to the primary key Benefits  No extraneous data
  • 42. Finding Balance user user_phone PK user_id FK1 first_name last_name nickname unit street_number street_name street_type quadrant web_url picture notes postal_code PK,FK1 user_id PK,FK2 phone_id extension email PK user_id format PK phone_id FK1 type_id area_code NXX NCX country_id FK2 country type PK type_id PK country_id Name phone_code type address FK1 phone user_department PK,FK1 PK,FK2 user_id department_id department PK postal_code FK1 city city_id PK city_id FK1 department_id FK1 postal_code PK name company_id province name province_id PK province_id FK1 Name Abbreviation country_id company PK company_id name
  • 43. Joining Tables  Two Basic Joins    Equi-Join     Equi-Join Outer Join (LEFT JOIN) SELECT user.first_name, user.last_name, email.address FROM user, email WHERE user.user_id = email.user_id LEFT JOIN    SELECT user.first_name, user.last_name, email.address FROM user LEFT JOIN email ON user.user_id = email.user_id
  • 44. De-Normalizing Tables     Use with caution Normalize first, then de-normalize Use only when you cannot optimize Try temp tables, UNIONs, VIEWs, subselects first