SlideShare a Scribd company logo
All Things Open --
Database
Programming for
Newbies
101-2 track
Database Programming for Newbies
This is a very short introduction to database programming and can not cover
all the things you need to know to be effective.
You have to build your skills from many sources including this talk
Example Database
The examples in this talk are
from the MySQL World database.
The content is dated but the
schema/database is still useful.
https://dev.mysql.com/doc/index-o
ther.html
MySQL Workbench
Workbench is the second most
popular FREE download from MySQL.
It is a very useful tool and has
dozens of uses (most can’t be
covered here)
Programming examples -- No, I can not provide examples in everyone’s
favorite programming language. The examples following are in a variety of
languages but all use the same concepts to communicate with a MySQL server.
2
Hello world!
I AM Dave Stokes
I am a MySQL Community Manager for
Oracle
You can find me at:
@stoker
David.Stokes@Oracle.com
Elephantanddolphin.blogger.com
OpensourceDBA.wordpress.com
Slides: slideshare.net/davidmstokes
3
1
The Basics
You have to start
somewhere
4
Cat: Where are you going?
Alice: Which way should I go?
Cat: That depends on where you
are going.
Alice: I don’t know.
Cat: Then it doesn’t matter which
way you go. -- Lewis Carroll
5
Database Generic
▪ The concepts are generic for most
all databases
▪ Implementation are MySQL specific
▪ Differences between version will
bite you
Pay attention to the flow not the
syntax at this point
6
Database Server
You need to know where to
contact the server and how to
connect to it
7
Server Information
IP Address -- You need to connect
via a network to the server (unless
instance is local)
PORT -- Socket on server at IP
Address where database server is
accepting requests
Authentication -- Username,
password or authentication string,
and maybe more 8
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db
database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
IP Address Username Password
Link or Handle -- our conduit to database
9
import mysql.connector
cnx = mysql.connector.connect(user='scott',
password='tiger', host='127.0.0.1',
database='employees')
cnx.close()
Again we have an IP Address, the
username/authentication and set up a
handle
10
We pass query to server
Usually this is in form of
a query written in
Structured Query Language
(SQL)
Sending query to a server
We receive info back
Hopefully we will receive
data back but we may get an
error message back or at
least some return code
11
Structured Query Language
Declarative
SQL is a fairly
simple to learn
declarative
language -- made
up of DDL & DDL.
DDL
Data Description
Language
describes how the
data is to
represented --
data type,
length, default
values, NULLable
...
DML
Data Manipulation
Language is used
to handle the
actual data --
Find all the
Cities in
California with a
population
greater than
500K, delete
records older
than 90 days,
change zip code
to 12345 from
11345
12
What your server does with a query
1. Is your system
allowed to connect
to server?
2. Are you allowed to
access
server/data?
3. Is the query
syntax correct?
4. Generate Query
Plan
5. Return requested
data
13
What the heck is
A Query Plan?!?!?
14
You query ...
- is examined for what data is
needed to fulfill the query.
- statistics from past queries used
to estimate lowest cost.
- every added column to a query is
potentially a new factorial of
complexity.
- Query plan is developed to get
data.
15
Your data
- is returned to your application
(if all goes correctly, or data
is returned, or a status code)
- Many options for reception,
depending on language
16
10,000’ View
Server -- Process
Application -- Data
Application - Query
17
2
A Little More Depth
A little more detail
18
"The time has come," the Walrus
said,
"To talk of many things:
Of shoes--and ships--and
sealing-wax--
Of cabbages--and kings--
And why the sea is boiling hot--
And whether pigs have wings." --
Lewis Carroll 19
We want to get a list of CITY NAMES
with the corresponding COUNTRY NAME
from the World database.
Example Query
20
SHOW CREATE TABLE City;
CREATE TABLE `city` (
`ID` int(11) AUTO_INCREMENT,
`Name` char(35),
`CountryCode` char(3),
`District` char(20),
`Population` int(11)',
`Name_ci` char(35) CHARACTER SET utf8 GENERATED ALWAYS AS (Name)
STORED,
PRIMARY KEY (`ID`),
KEY `CountryCode` (`CountryCode`),
KEY `Name_ci` (`Name_ci`),
CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES
`country` (`Code`))
ENGINE=InnoDB
AUTO_INCREMENT=4080
DEFAULT CHARSET=latin1 21
Sample City Data From Query
ID Name CountryCode District Population Name_ci
1 Kabul AFG Kabol 1780000 Kabul
2 Qandahar AFG Qandahar 237500 Qandahar
3 Herat AFG Herat 186800 Herat
4 Mazar-e-Sharif AFG Balkh 127800 Mazar-e-Sharif
5 Amsterdam NLD
Noord-Holl
and 731200 Amsterdam
22
SHOW CREATE TABLE Country
'CREATE TABLE `country` (
`Code` char(3),
`Name` char(52),
`Continent` enum('Asia','Europe','North
America','Africa','Oceania','Antarctica','South America'),
`Region` char(26),
`SurfaceArea` float(10,2),
`IndepYear` smallint(6),
`Population` int(11),
`LifeExpectancy` float(3,1),
`GNP` float(10,2),
`GNPOld` float(10,2),
`LocalName` char(45),
`GovernmentForm` char(45),
`HeadOfState` char(60),
`Capital` int(11),
`Code2` char(2),
PRIMARY KEY (`Code`))
23
Generic Query (DML)
SELECT City.Name,
Country.Name
FROM City
JOIN Country ON
(City.CountryCode = Country.Code)
The desired data
From which table
How to connect the tables
24
Generic Query (DML) as a string
$query = “SELECT City.Name,
Country.Name
FROM City
JOIN Country ON
(City.CountryCode =
Country.Code)”;
25
Do your older self a big favor and go for clarity over space
savings when you write queries in your code.
Send $query to server
$result = $conn->query($query);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "City: " . $row[0]. " Country: " .
$row[1] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
26
Roughly same thing in Python
import datetime
import mysql.connector
cnx = mysql.connector.connect(user='dave', database='world')
cursor = cnx.cursor()
query = ("SELECT City.Name, Country.Name FROM City JOIN
Country ON (City.CountryCode = Country.Code)")
cursor.execute(query)
for (city_name, country_name in cursor:
print("{}, {}".format(
city_name, country_name, hire_date))
cursor.close()
cnx.close()
27
Many Options to retrieve data
while($row = $result->fetch_assoc()) {
echo "City: " . $row[0]. " Country: " . $row[1] . "<br>";
}
28
mysqli_result::fetch_all — Fetches all result rows as an associative
array, a numeric array, or both
mysqli_result::fetch_array — Fetch a result row as an associative, a
numeric array, or both
mysqli_result::fetch_assoc — Fetch a result row as an associative array
mysqli_result::fetch_field_direct — Fetch meta-data for a single field
mysqli_result::fetch_field — Returns the next field in the result set
mysqli_result::fetch_fields — Returns an array of objects representing
the fields in a result set
mysqli_result::fetch_object — Returns the current row of a resultset as
an object
mysqli_result::fetch_row — Get a result row as an enumerated array
3
SQL -- Structure Query
Language Basics
Five minutes on a
Relatively simple
language that can drive
you crazy for years!!
29
“But I don’t want to go among mad
people," Alice remarked.
"Oh, you can’t help that," said
the Cat: "we’re all mad here. I’m
mad. You’re mad."
"How do you know I’m mad?" said
Alice.
"You must be," said the Cat, "or
you wouldn’t have come here.” 30
DDL - data definition
language
CREATE
DROP
ALTER
TRUNCATE
RENAME
31
The Major Verbs of Structured Query Language
DML - data manipulation
language
SELECT
INSERT
UPDATE
DELETE
Usually used by DBAs
to set up the data
Used to access
the data by just
about everyone
Check your manpage for details on each verb
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[MAX_STATEMENT_TIME = N]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
32
99% of your SELECTs will be much simpler
SELECT
select_expr [, select_expr
...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name}
[ORDER BY {col_name}
[LIMIT {row_count]
33
SELECT Name,
District,
ID
FROM City
WHERE ID < 100000
GROUP BY District
ORDER BY Name
LIMIT 5
Most of the VERBs simplify down to a few,
more common qualifiers. Best way to learn is
to follow examples in a manual or book.
output
34
You DESCRIBE what you want,
sort of like ordering a
pizza!
35
SQL is a Declarative Language
The bad part:
Like a pizza, you have to
know the ingredients to be
able to order what you
want!
You can’t look at an SQL
query and tell if it is bad
…
Just like a menu does not
tell you if the cheese has
gone fuzzy and the
pepperoni meows
You now know
-Client server database
model
-Queries passed to server
-Data returned (sometimes)
-A little DML & DDL
-The MySQL authentical
system
-Basic query & SQL VERBs
36
Running summary
What to add
-JOINs
-SQL Injection
-N+1 Problem
-Transactions
-Indexes
-Wrap-up
-Q/A
4
JOINing tables
A good glimpse at the
Relational model
37
“Everything is funny, if you can
laugh at it.”
38
JOINs
JOINs allow you to connect
two tables on (hopefully) a
common element.
-Depends on data
normalization (weeks can be
spent on just that!) -- For
now just accept that some
has broken up all the data
into component data -- For
our example we have a table
with City information,
Country information, and
Languages spoke in a
Country information and
that someone architected
the data so that one field
can help tie them together.
39
40
CountryCode
All three
tables in our
example
database can
be linked
easily by these
columns
Also note that
each of the
columns used to
link are INDEXed
or Keys
41
A database index is a data structure that
improves the speed of data retrieval operations
on a database table at the cost of additional
writes and storage space to maintain the index
data structure. Indexes are used to quickly
locate data without having to search every row
in a database table every time a database table
is accessed. Indices can be created using one
or more columns of a database table, providing
the basis for both rapid random lookups and
efficient access of ordered records. --
Wikipedia
42
Remember the Query Plan???
The query plan tried to find
efficient indexes to help retrieve
data.
Optimizer Trace
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "5132.14"
},
"nested_loop": [
{
"table": {
"table_name": "Country",
"access_type": "ALL",
"possible_keys": [
"PRIMARY"
],
"rows_examined_per_scan": 239,
"rows_produced_per_join": 239,
"filtered": "100.00",
"cost_info": {
"read_cost": "6.00",
"eval_cost": "47.80",
"prefix_cost": "53.80",
"data_read_per_join": "61K"
},
"used_columns": [
"Code",
"Name"
]
}
},
{
"table": {
"table_name": "City",
"access_type": "ref",
"possible_keys": [
"CountryCode"
],
"key": "CountryCode",
"used_key_parts": [
"CountryCode"
],
"key_length": "3",
"ref": [
"world.Country.Code"
],
"rows_examined_per_scan": 17,
"rows_produced_per_join": 4231,
"filtered": "100.00",
"cost_info": {
"read_cost": "4231.95",
"eval_cost": "846.39",
"prefix_cost": "5132.14",
"data_read_per_join": "727K"
},
"used_columns": [
"Name",
"CountryCode"
]
}
}
]
}
}
43
The KEY used
to perform the
join on our
example
query is in
both tables
Optimizer
What fields are needed?
-- Cost
-- Can a INDEX(es) be used
-- Which INDEX
-- WHERE clause (later)
-- SORT (later)
44
EXPLAIN -- prepend ‘EXPLAIN’ to query
Explain details what the optimizer wants to do to run the query.
For the City table the CountryCode index is used to link to the
Code column in the Country Table.
There is a possible key on the Country table but it is not
needed/used for this query
45
Visual Explain -- MySQL Workbench
46
47
5
Problems
Where the train
Starts to go
Off the tracks
48
She generally gave herself very
good advice, (though she very
seldom followed it)
49
1. Network connection
2. Is host okay to connect?
3. Does user have proper access?
4. Syntax check
5. Query Plan Generation
6. Return data
50
Each Connection to the database has a cost
Therefore seek to use the
fewest calls to the database to
get the needed data!!!
N+1 Example -- Ride to work
1. Ask database for a list
of employees.
2. Do any of these
employees have a parking
permit.
3. Do any of those in step
two live in same zip
code.
51
1. Ask database for a list
of employes with a
parking permit living in
your zipcode.
Think of data in big sets -- Let the
database do the heavy lifting
Which is more efficient -- Sales staff 20% raise
foreach (sale_emp in sales_employees)
$pay += $pay * .20;
UPDATE employees
SET pay_rate = pay_rate + pay_rate * .20
WHERE department = ‘sales’;
52
START TRANSACTION;
COMMIT;
What is easier to recover in case of a
crash of a PHB correction?
SQL Injection
SQL Injection is where you
-- the programmer -- take
raw, unfiltered data from
the end user and plop it
into an SQL query.
The ‘injection’ is SQL code
put into your query that
can give the end user
access to your data.
NEVER, NEVER put raw input
data from end user into
your SQL queries.
Check for validity -- Are
numbers really numbers? Are
there special characters
that you do not want? Are
you really certain that the
inputs are in the write
format.
53
Little Bobby Drop Tables
54
SELECT * FROM customer WHERE id=$x
Expected an Integer
Received ‘TRUE’
Expected an Integer
Received ‘TRUE’;SELECT * FROM
mysql.user;
55
EXAMPLE of vulnerable code
Last BIG PROBLEM
for today
56
Check return codes -- Do
not assume all went
well. Be prepared to
roll back pr resubmit or
handle and error
BOOKS YOU REALLY REALLY NEED if you want
CJ Date Bill Karwin
57
BOOKS YOU REALLY REALLY NEED if you want to be GOOD
Any of the top 20 AMAZON
books on {Your Favorite
Programming Language Here}
and {Your Database Of
Choice Here}
Bill Karwin
58
What if I do not want to do SQL?!?!
Key/Value Pairs
JSON - Document Storage
Graph Databases
Other NoSQL approaches
Keep everything in memory all the
time 59
Thanks!
ANY QUESTIONS?
You can find me at:
@stoker
david.stokes@oracle.com
elephantanddolphin.blogger.com
60

More Related Content

PPTX
Slick: Bringing Scala’s Powerful Features to Your Database Access
ODP
Patterns for slick database applications
PDF
Everything About PowerShell
PDF
MongoDB .local Paris 2020: La puissance du Pipeline d'Agrégation de MongoDB
PDF
veracruz
PDF
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
PDF
Advanced Querying with CakePHP 3
PDF
MongoDB Aggregation Framework
Slick: Bringing Scala’s Powerful Features to Your Database Access
Patterns for slick database applications
Everything About PowerShell
MongoDB .local Paris 2020: La puissance du Pipeline d'Agrégation de MongoDB
veracruz
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Advanced Querying with CakePHP 3
MongoDB Aggregation Framework

What's hot (17)

PDF
New in cakephp3
PDF
PHP Data Objects
KEY
MongoDB Aggregation Framework
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
PPTX
Aggregation Framework
PPTX
Aggregation in MongoDB
PDF
Future of HTTP in CakePHP
PPT
DB2 Native XML
PDF
Using web2py's DAL in other projects or frameworks
PPT
Sqlxml vs xquery
PDF
Embedding a language into string interpolator
PDF
Web2py Code Lab
KEY
Php 101: PDO
PPTX
MongoDB Aggregation
PPTX
ETL for Pros: Getting Data Into MongoDB
PDF
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
New in cakephp3
PHP Data Objects
MongoDB Aggregation Framework
From mysql to MongoDB(MongoDB2011北京交流会)
Aggregation Framework
Aggregation in MongoDB
Future of HTTP in CakePHP
DB2 Native XML
Using web2py's DAL in other projects or frameworks
Sqlxml vs xquery
Embedding a language into string interpolator
Web2py Code Lab
Php 101: PDO
MongoDB Aggregation
ETL for Pros: Getting Data Into MongoDB
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Ad

Viewers also liked (17)

PPTX
Why Your Database Queries Stink -SeaGl.org November 11th, 2016
PDF
MySQL as a Document Store
PDF
MySQL's JSON Data Type and Document Store
PDF
MySQL Replication Update -- Zendcon 2016
PDF
MySQL Replication Basics -Ohio Linux Fest 2016
PPTX
MySQL Replication Overview -- PHPTek 2016
PDF
Five Database Mistakes and how to fix them -- Confoo Vancouver
PDF
Scaling MySQL -- Swanseacon.co.uk
PPTX
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
PPTX
Polyglot Database - Linuxcon North America 2016
PDF
MySQL 8.0: Common Table Expressions
PDF
How to Analyze and Tune MySQL Queries for Better Performance
PDF
MySQL 8.0: Common Table Expressions
PPTX
What Your Database Query is Really Doing
PDF
How to analyze and tune sql queries for better performance vts2016
PDF
SQL window functions for MySQL
PDF
Using Optimizer Hints to Improve MySQL Query Performance
Why Your Database Queries Stink -SeaGl.org November 11th, 2016
MySQL as a Document Store
MySQL's JSON Data Type and Document Store
MySQL Replication Update -- Zendcon 2016
MySQL Replication Basics -Ohio Linux Fest 2016
MySQL Replication Overview -- PHPTek 2016
Five Database Mistakes and how to fix them -- Confoo Vancouver
Scaling MySQL -- Swanseacon.co.uk
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
Polyglot Database - Linuxcon North America 2016
MySQL 8.0: Common Table Expressions
How to Analyze and Tune MySQL Queries for Better Performance
MySQL 8.0: Common Table Expressions
What Your Database Query is Really Doing
How to analyze and tune sql queries for better performance vts2016
SQL window functions for MySQL
Using Optimizer Hints to Improve MySQL Query Performance
Ad

Similar to All Things Open 2016 -- Database Programming for Newbies (20)

PDF
Php summary
PPT
PHP - Getting good with MySQL part II
PPSX
DIWE - Working with MySQL Databases
PPTX
lecture 7 - Introduction to MySQL with PHP.pptx
PPTX
Learn PHP Lacture2
PPT
Synapse india reviews on php and sql
PPTX
Introduction databases and MYSQL
PPTX
PHP mysql Introduction database
PDF
Service discovery and configuration provisioning
PPTX
UNIT V (5).pptx
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
MySQL with PHP
PDF
RMySQL Tutorial For Beginners
PDF
PHP with MySQL
PPTX
harry presentation
PPT
Php with MYSQL Database
PPTX
PHP Database Programming Basics -- Northeast PHP
ODP
MySQL Without the MySQL -- Oh My!
PPT
Lecture6 display data by okello erick
Php summary
PHP - Getting good with MySQL part II
DIWE - Working with MySQL Databases
lecture 7 - Introduction to MySQL with PHP.pptx
Learn PHP Lacture2
Synapse india reviews on php and sql
Introduction databases and MYSQL
PHP mysql Introduction database
Service discovery and configuration provisioning
UNIT V (5).pptx
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
MySQL with PHP
RMySQL Tutorial For Beginners
PHP with MySQL
harry presentation
Php with MYSQL Database
PHP Database Programming Basics -- Northeast PHP
MySQL Without the MySQL -- Oh My!
Lecture6 display data by okello erick

More from Dave Stokes (20)

PDF
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
PPTX
Locking Down Your MySQL Database.pptx
PPTX
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
PDF
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
PDF
Windowing Functions - Little Rock Tech fest 2019
PDF
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
PPTX
Develop PHP Applications with MySQL X DevAPI
PDF
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
PDF
The Proper Care and Feeding of MySQL Databases
PDF
MySQL without the SQL -- Cascadia PHP
PDF
MySQL 8 Server Optimization Swanseacon 2018
PDF
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
PDF
Presentation Skills for Open Source Folks
PPTX
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
PPTX
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
PPTX
ConFoo MySQL Replication Evolution : From Simple to Group Replication
PDF
Advanced MySQL Query Optimizations
PPTX
Making MySQL Agile-ish
ODP
MySQL 101 PHPTek 2017
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Locking Down Your MySQL Database.pptx
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
MySQL Indexes and Histograms - RMOUG Training Days 2022
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Windowing Functions - Little Rock Tech fest 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Develop PHP Applications with MySQL X DevAPI
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
The Proper Care and Feeding of MySQL Databases
MySQL without the SQL -- Cascadia PHP
MySQL 8 Server Optimization Swanseacon 2018
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
Presentation Skills for Open Source Folks
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Advanced MySQL Query Optimizations
Making MySQL Agile-ish
MySQL 101 PHPTek 2017

Recently uploaded (20)

PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PPTX
innovation process that make everything different.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
CSharp_Syntax_Basics.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Testing WebRTC applications at scale.pdf
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PPTX
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
Digital Literacy And Online Safety on internet
Triggering QUIC, presented by Geoff Huston at IETF 123
innovation process that make everything different.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Job_Card_System_Styled_lorem_ipsum_.pptx
Module 1 - Cyber Law and Ethics 101.pptx
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
522797556-Unit-2-Temperature-measurement-1-1.pptx
CSharp_Syntax_Basics.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxx
RPKI Status Update, presented by Makito Lay at IDNOG 10
Tenda Login Guide: Access Your Router in 5 Easy Steps
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Testing WebRTC applications at scale.pdf
The Internet -By the Numbers, Sri Lanka Edition
QR Codes Qr codecodecodecodecocodedecodecode
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Digital Literacy And Online Safety on internet

All Things Open 2016 -- Database Programming for Newbies

  • 1. All Things Open -- Database Programming for Newbies 101-2 track
  • 2. Database Programming for Newbies This is a very short introduction to database programming and can not cover all the things you need to know to be effective. You have to build your skills from many sources including this talk Example Database The examples in this talk are from the MySQL World database. The content is dated but the schema/database is still useful. https://dev.mysql.com/doc/index-o ther.html MySQL Workbench Workbench is the second most popular FREE download from MySQL. It is a very useful tool and has dozens of uses (most can’t be covered here) Programming examples -- No, I can not provide examples in everyone’s favorite programming language. The examples following are in a variety of languages but all use the same concepts to communicate with a MySQL server. 2
  • 3. Hello world! I AM Dave Stokes I am a MySQL Community Manager for Oracle You can find me at: @stoker [email protected] Elephantanddolphin.blogger.com OpensourceDBA.wordpress.com Slides: slideshare.net/davidmstokes 3
  • 4. 1 The Basics You have to start somewhere 4
  • 5. Cat: Where are you going? Alice: Which way should I go? Cat: That depends on where you are going. Alice: I don’t know. Cat: Then it doesn’t matter which way you go. -- Lewis Carroll 5
  • 6. Database Generic ▪ The concepts are generic for most all databases ▪ Implementation are MySQL specific ▪ Differences between version will bite you Pay attention to the flow not the syntax at this point 6
  • 7. Database Server You need to know where to contact the server and how to connect to it 7
  • 8. Server Information IP Address -- You need to connect via a network to the server (unless instance is local) PORT -- Socket on server at IP Address where database server is accepting requests Authentication -- Username, password or authentication string, and maybe more 8
  • 9. <?php $link = mysqli_connect("127.0.0.1", "my_user", "my_password"); if (!$link) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit; } echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL; echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL; mysqli_close($link); ?> IP Address Username Password Link or Handle -- our conduit to database 9
  • 10. import mysql.connector cnx = mysql.connector.connect(user='scott', password='tiger', host='127.0.0.1', database='employees') cnx.close() Again we have an IP Address, the username/authentication and set up a handle 10
  • 11. We pass query to server Usually this is in form of a query written in Structured Query Language (SQL) Sending query to a server We receive info back Hopefully we will receive data back but we may get an error message back or at least some return code 11
  • 12. Structured Query Language Declarative SQL is a fairly simple to learn declarative language -- made up of DDL & DDL. DDL Data Description Language describes how the data is to represented -- data type, length, default values, NULLable ... DML Data Manipulation Language is used to handle the actual data -- Find all the Cities in California with a population greater than 500K, delete records older than 90 days, change zip code to 12345 from 11345 12
  • 13. What your server does with a query 1. Is your system allowed to connect to server? 2. Are you allowed to access server/data? 3. Is the query syntax correct? 4. Generate Query Plan 5. Return requested data 13
  • 14. What the heck is A Query Plan?!?!? 14
  • 15. You query ... - is examined for what data is needed to fulfill the query. - statistics from past queries used to estimate lowest cost. - every added column to a query is potentially a new factorial of complexity. - Query plan is developed to get data. 15
  • 16. Your data - is returned to your application (if all goes correctly, or data is returned, or a status code) - Many options for reception, depending on language 16
  • 17. 10,000’ View Server -- Process Application -- Data Application - Query 17
  • 18. 2 A Little More Depth A little more detail 18
  • 19. "The time has come," the Walrus said, "To talk of many things: Of shoes--and ships--and sealing-wax-- Of cabbages--and kings-- And why the sea is boiling hot-- And whether pigs have wings." -- Lewis Carroll 19
  • 20. We want to get a list of CITY NAMES with the corresponding COUNTRY NAME from the World database. Example Query 20
  • 21. SHOW CREATE TABLE City; CREATE TABLE `city` ( `ID` int(11) AUTO_INCREMENT, `Name` char(35), `CountryCode` char(3), `District` char(20), `Population` int(11)', `Name_ci` char(35) CHARACTER SET utf8 GENERATED ALWAYS AS (Name) STORED, PRIMARY KEY (`ID`), KEY `CountryCode` (`CountryCode`), KEY `Name_ci` (`Name_ci`), CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1 21
  • 22. Sample City Data From Query ID Name CountryCode District Population Name_ci 1 Kabul AFG Kabol 1780000 Kabul 2 Qandahar AFG Qandahar 237500 Qandahar 3 Herat AFG Herat 186800 Herat 4 Mazar-e-Sharif AFG Balkh 127800 Mazar-e-Sharif 5 Amsterdam NLD Noord-Holl and 731200 Amsterdam 22
  • 23. SHOW CREATE TABLE Country 'CREATE TABLE `country` ( `Code` char(3), `Name` char(52), `Continent` enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America'), `Region` char(26), `SurfaceArea` float(10,2), `IndepYear` smallint(6), `Population` int(11), `LifeExpectancy` float(3,1), `GNP` float(10,2), `GNPOld` float(10,2), `LocalName` char(45), `GovernmentForm` char(45), `HeadOfState` char(60), `Capital` int(11), `Code2` char(2), PRIMARY KEY (`Code`)) 23
  • 24. Generic Query (DML) SELECT City.Name, Country.Name FROM City JOIN Country ON (City.CountryCode = Country.Code) The desired data From which table How to connect the tables 24
  • 25. Generic Query (DML) as a string $query = “SELECT City.Name, Country.Name FROM City JOIN Country ON (City.CountryCode = Country.Code)”; 25 Do your older self a big favor and go for clarity over space savings when you write queries in your code.
  • 26. Send $query to server $result = $conn->query($query); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "City: " . $row[0]. " Country: " . $row[1] . "<br>"; } } else { echo "0 results"; } $conn->close(); 26
  • 27. Roughly same thing in Python import datetime import mysql.connector cnx = mysql.connector.connect(user='dave', database='world') cursor = cnx.cursor() query = ("SELECT City.Name, Country.Name FROM City JOIN Country ON (City.CountryCode = Country.Code)") cursor.execute(query) for (city_name, country_name in cursor: print("{}, {}".format( city_name, country_name, hire_date)) cursor.close() cnx.close() 27
  • 28. Many Options to retrieve data while($row = $result->fetch_assoc()) { echo "City: " . $row[0]. " Country: " . $row[1] . "<br>"; } 28 mysqli_result::fetch_all — Fetches all result rows as an associative array, a numeric array, or both mysqli_result::fetch_array — Fetch a result row as an associative, a numeric array, or both mysqli_result::fetch_assoc — Fetch a result row as an associative array mysqli_result::fetch_field_direct — Fetch meta-data for a single field mysqli_result::fetch_field — Returns the next field in the result set mysqli_result::fetch_fields — Returns an array of objects representing the fields in a result set mysqli_result::fetch_object — Returns the current row of a resultset as an object mysqli_result::fetch_row — Get a result row as an enumerated array
  • 29. 3 SQL -- Structure Query Language Basics Five minutes on a Relatively simple language that can drive you crazy for years!! 29
  • 30. “But I don’t want to go among mad people," Alice remarked. "Oh, you can’t help that," said the Cat: "we’re all mad here. I’m mad. You’re mad." "How do you know I’m mad?" said Alice. "You must be," said the Cat, "or you wouldn’t have come here.” 30
  • 31. DDL - data definition language CREATE DROP ALTER TRUNCATE RENAME 31 The Major Verbs of Structured Query Language DML - data manipulation language SELECT INSERT UPDATE DELETE Usually used by DBAs to set up the data Used to access the data by just about everyone
  • 32. Check your manpage for details on each verb SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [MAX_STATEMENT_TIME = N] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [PARTITION partition_list] [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]] 32
  • 33. 99% of your SELECTs will be much simpler SELECT select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name} [ORDER BY {col_name} [LIMIT {row_count] 33 SELECT Name, District, ID FROM City WHERE ID < 100000 GROUP BY District ORDER BY Name LIMIT 5 Most of the VERBs simplify down to a few, more common qualifiers. Best way to learn is to follow examples in a manual or book.
  • 35. You DESCRIBE what you want, sort of like ordering a pizza! 35 SQL is a Declarative Language The bad part: Like a pizza, you have to know the ingredients to be able to order what you want! You can’t look at an SQL query and tell if it is bad … Just like a menu does not tell you if the cheese has gone fuzzy and the pepperoni meows
  • 36. You now know -Client server database model -Queries passed to server -Data returned (sometimes) -A little DML & DDL -The MySQL authentical system -Basic query & SQL VERBs 36 Running summary What to add -JOINs -SQL Injection -N+1 Problem -Transactions -Indexes -Wrap-up -Q/A
  • 37. 4 JOINing tables A good glimpse at the Relational model 37
  • 38. “Everything is funny, if you can laugh at it.” 38
  • 39. JOINs JOINs allow you to connect two tables on (hopefully) a common element. -Depends on data normalization (weeks can be spent on just that!) -- For now just accept that some has broken up all the data into component data -- For our example we have a table with City information, Country information, and Languages spoke in a Country information and that someone architected the data so that one field can help tie them together. 39
  • 40. 40 CountryCode All three tables in our example database can be linked easily by these columns Also note that each of the columns used to link are INDEXed or Keys
  • 41. 41 A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indices can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records. -- Wikipedia
  • 42. 42 Remember the Query Plan??? The query plan tried to find efficient indexes to help retrieve data.
  • 43. Optimizer Trace { "query_block": { "select_id": 1, "cost_info": { "query_cost": "5132.14" }, "nested_loop": [ { "table": { "table_name": "Country", "access_type": "ALL", "possible_keys": [ "PRIMARY" ], "rows_examined_per_scan": 239, "rows_produced_per_join": 239, "filtered": "100.00", "cost_info": { "read_cost": "6.00", "eval_cost": "47.80", "prefix_cost": "53.80", "data_read_per_join": "61K" }, "used_columns": [ "Code", "Name" ] } }, { "table": { "table_name": "City", "access_type": "ref", "possible_keys": [ "CountryCode" ], "key": "CountryCode", "used_key_parts": [ "CountryCode" ], "key_length": "3", "ref": [ "world.Country.Code" ], "rows_examined_per_scan": 17, "rows_produced_per_join": 4231, "filtered": "100.00", "cost_info": { "read_cost": "4231.95", "eval_cost": "846.39", "prefix_cost": "5132.14", "data_read_per_join": "727K" }, "used_columns": [ "Name", "CountryCode" ] } } ] } } 43 The KEY used to perform the join on our example query is in both tables
  • 44. Optimizer What fields are needed? -- Cost -- Can a INDEX(es) be used -- Which INDEX -- WHERE clause (later) -- SORT (later) 44
  • 45. EXPLAIN -- prepend ‘EXPLAIN’ to query Explain details what the optimizer wants to do to run the query. For the City table the CountryCode index is used to link to the Code column in the Country Table. There is a possible key on the Country table but it is not needed/used for this query 45
  • 46. Visual Explain -- MySQL Workbench 46
  • 47. 47
  • 48. 5 Problems Where the train Starts to go Off the tracks 48
  • 49. She generally gave herself very good advice, (though she very seldom followed it) 49
  • 50. 1. Network connection 2. Is host okay to connect? 3. Does user have proper access? 4. Syntax check 5. Query Plan Generation 6. Return data 50 Each Connection to the database has a cost Therefore seek to use the fewest calls to the database to get the needed data!!!
  • 51. N+1 Example -- Ride to work 1. Ask database for a list of employees. 2. Do any of these employees have a parking permit. 3. Do any of those in step two live in same zip code. 51 1. Ask database for a list of employes with a parking permit living in your zipcode. Think of data in big sets -- Let the database do the heavy lifting
  • 52. Which is more efficient -- Sales staff 20% raise foreach (sale_emp in sales_employees) $pay += $pay * .20; UPDATE employees SET pay_rate = pay_rate + pay_rate * .20 WHERE department = ‘sales’; 52 START TRANSACTION; COMMIT; What is easier to recover in case of a crash of a PHB correction?
  • 53. SQL Injection SQL Injection is where you -- the programmer -- take raw, unfiltered data from the end user and plop it into an SQL query. The ‘injection’ is SQL code put into your query that can give the end user access to your data. NEVER, NEVER put raw input data from end user into your SQL queries. Check for validity -- Are numbers really numbers? Are there special characters that you do not want? Are you really certain that the inputs are in the write format. 53
  • 54. Little Bobby Drop Tables 54
  • 55. SELECT * FROM customer WHERE id=$x Expected an Integer Received ‘TRUE’ Expected an Integer Received ‘TRUE’;SELECT * FROM mysql.user; 55 EXAMPLE of vulnerable code
  • 56. Last BIG PROBLEM for today 56 Check return codes -- Do not assume all went well. Be prepared to roll back pr resubmit or handle and error
  • 57. BOOKS YOU REALLY REALLY NEED if you want CJ Date Bill Karwin 57
  • 58. BOOKS YOU REALLY REALLY NEED if you want to be GOOD Any of the top 20 AMAZON books on {Your Favorite Programming Language Here} and {Your Database Of Choice Here} Bill Karwin 58
  • 59. What if I do not want to do SQL?!?! Key/Value Pairs JSON - Document Storage Graph Databases Other NoSQL approaches Keep everything in memory all the time 59
  • 60. Thanks! ANY QUESTIONS? You can find me at: @stoker [email protected] elephantanddolphin.blogger.com 60