Relational Databases and Mysql: This Work Is Licensed Under A
Relational Databases and Mysql: This Work Is Licensed Under A
This work is licensed under a Creative Commons Attribution- Share Alike 4. 0 International License
Sequentia
OLD l NEW
Sorted Master Sorted
Update
1970s
Merge
Transaction
s
Sorted https://en.wikipedia.org/wiki/IBM_72
9
Random Access
https://en.wikipedia.org/wiki/Hard_disk_drive_platter
Relational Databases
Relational databases model data by storing
rows and columns in tables. The power of
the relational database lies in its ability to
efficiently retrieve data from those tables - in
particular, where the query involves multiple
tables and the relationships between those
tables.
http://en.wikipedia.org/wiki/Relational_database
Structured Query Language
• Structured Query Language
(SQL) came out of a
government / industry
partnership
• National Institute of
Standards and Technology
(NIST)
https://youtu.be/rLUm3vst87g
SQL
Structured Query Language is the
language we use to issue commands
to the database
- Create/Insert data
- Update data
http://en.wikipedia.org/wiki/SQL
- Delete data
https://en.wikipedia.org/wiki/ANSI-SPARC_Architecture
Terminology
• Database - contains one or more tables
Tables /
Relations
Common Database Systems
• Three major Database Management Systems in wide use
- MySQL - Simpler but very fast and scalable - commercial open source
phpMyAdmin SQL
(Browser)
User Database
D.B.A. Server
mysql MySQL
(Command SQL
Line)
• Macintosh Command Line
- /Applications/MAMP/Library/bin/mysql … (see handout for PW)
• Windows
- c:\xampp\mysql\bin\mysql.exe -u root –p
https://www.wa4e.com/lectures/SQL-01-MySQL-Handout.txt
Your First MySQL Command
show databases
Kind of like print('hello world’)
DESCRIBE Users;
SQL: Insert
The INSERT statement inserts a row into a table
• The limit can be a count or a starting row and count (starts from 0).
• The power comes when we have more than one table and we can
exploit the relationships between the tables.
Table Schema
Looking at Data Types
• Numeric fields
• AUTO_INCREMENT fields
String Fields
• CHAR allocates the entire space (faster for small strings where length
is known)
- TEXT up to 65K
- MEDIUMTEXT up to 16M
- LONGTEXT up to 4G
• Generally not used with indexing or sorting - and only then limited to a
prefix
Binary Types (rarely used)
• Character = 8 - 32 bits of information depending on character set
• DATE - 'YYYY-MM-DD'
• TIME - 'HH:MM:SS'
DESCRIBE Users;
MySQL Functions
Many operations in MySQL need to use the built-in functions (like
NOW() for dates).
• http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
• http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
Indexes
• As a table gets large (they always do), scanning all the data to find a
single row becomes very costly
• There are techniques to greatly shorten the scan as long as you create
data structures and maintain those structures - like shortcuts
• Hashes or Trees
MySQL Index Types
• You must tell the JOIN how to use the keys that make the connection
between the tables using an ON clause.
The tables that
hold the data
Joining two tables without an ON clause gives all possible combinations of rows.
SELECT Track.title, Genre.name FROM Track JOIN Genre
ON Track.genre_id = Genre.genre_id
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance (www.dr- Continue new Contributors and Translators here
chuck.com) as part of www.wa4e.com and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change, feel
free to add your name and organization to the list of contributors
on this page as you republish the materials.