SlideShare a Scribd company logo
Coding Potpourri:
                            MySQL
                                  Nicole C. Engard
               Director of Open Source Education, ByWater Solutions
                 Author of Practical Open Source Software for Libraries

Monday, July 25, 2011
My What?

                    • MySQL: My Structured Query Language
                    • Relational Database Management System
                    • Licensed with the GNU GPL
                     • That means it’s Open Source
                    • Used to organize data in a structured way
Monday, July 25, 2011
Who’s Using It?
                    • MySQL is the database behind many
                        popular web based applications
                        • Wordpress         • YouTube
                        • Drupal            • Flickr
                        • Wikipedia         • Ebay
                        • Facebook          • Google (not searches)
Monday, July 25, 2011
Reading Table Structure
                  CREATE TABLE `branches` (
                    `branchcode` varchar(10) NOT NULL default '',
                    `branchname` mediumtext NOT NULL,
                    `branchaddress1` mediumtext,
                    `branchaddress2` mediumtext,
                    `branchaddress3` mediumtext,
                    `branchphone` mediumtext,
                    `branchfax` mediumtext,
                    `branchemail` mediumtext,
                    UNIQUE KEY `branchcode` (`branchcode`)
                  )
Monday, July 25, 2011
Reading Table Structure
              •         This table’s name is ‘branches’ and it stores the
                        information about libraries or branches in Koha.
              •         Each field is easy to identify because of its name
                        (ex. branchname is the library name).
              •         A field with a number in parens after it is a field
                        that is limited in size.
                    •     For example varchar(10) means the field can
                          have no more than 10 characters in it
              •         Lastly, we see that ‘branchcode’ is the unique key
                        or unique identifier in the table.
Monday, July 25, 2011
Inserting Data
                • Using the branches table enter a branch’s
                        info

                        INSERT INTO branches (branchcode,
                        branchname, branchaddress1,
                        branchaddress2, branchphone, branchemail)
                        VALUES (‘LIB’, ‘My Library’, ‘123 Library
                        Road’, ‘Philadelphia, PA’, ‘215.555.1234’,
                        ‘info@library.com’);
Monday, July 25, 2011
Querying MySQL
                 • To query a single table you will structure your
                        query like this:
                 • SELECT column_names FROM table_name
                        [WHERE ...conditions] [ORDER
                        BY ...conditions];
                 • Statements in brackets are optional
                 • You can also select everything in a table by
                        using an * in place of column_names

Monday, July 25, 2011
Querying MySQL
               • Using the branches table let’s query the data
               • This query will pull out only the Branch Names
                        and Emails and put them in ascending order by
                        name

                        SELECT branchname, branchemail FROM
                        branches ORDER BY branchname ASC;



Monday, July 25, 2011
Querying MySQL
               • Using branches let’s get the address info from
                        one specific branch

                        SELECT branchname, branchaddress1,
                        branchaddress2, branchaddress3, branchemail
                        FROM branches WHERE branchcode=‘LIB’;




Monday, July 25, 2011
Querying MySQL
             • Using branches let’s pull out the branch’s phone
                        and fax in one column

                        SELECT CONCAT(‘ph. ’, branchphone, ‘ fax ’,
                        branchfax) as ‘contact info’ FROM branches
                        WHERE branchcode= ‘LIB’;




Monday, July 25, 2011
Another Table
           CREATE TABLE `issues` (
             `borrowernumber` int(11) default NULL,
             `itemnumber` int(11) default NULL,
             `date_due` date default NULL,
             `branchcode` varchar(10) default NULL,
             `returndate` date default NULL,
             `return` varchar(4) default NULL,
             `renewals` tinyint(4) default NULL,
             `issuedate` date default NULL,
             KEY `issuesborridx` (`borrowernumber`),
             KEY `issuesitemidx` (`itemnumber`),
           )
Monday, July 25, 2011
Joining Tables
              •         Using branches and issues let’s find out how many
                        items circulated at each branch in a specific time
                        period

                        SELECT b.branchname, count(i.branchcode) as
                        count FROM issues i LEFT JOIN branches b ON
                        (i.branchcode=b.branchcode) WHERE i.issuedate
                        BETWEEN ‘2011-06-01’ AND ‘2011-07-01’ GROUP
                        BY b.branchcode ORDER BY count DESC;




Monday, July 25, 2011
Date & Time Functions
                    • The most common use for reports is for
                        end of the month or end of the year
                        statistics
                    • The MySQL manual on Date & Time
                        functions is essential for these queries
                        • http://dev.mysql.com/doc/refman/5.6/en/
                          date-and-time-functions.html

Monday, July 25, 2011
String Functions
                    • Often you want to join two or more
                        strings together, find a part of a string or
                        even change a part of a string
                    • String functions are defined in the MySQL
                        manual
                        • http://dev.mysql.com/doc/refman/5.6/en/
                          string-functions.html

Monday, July 25, 2011
Math Functions
                    • Using MySQL you can get fancy with
                        statistics by using the number related
                        functions
                    • The Numeric Functions section of the
                        manual can help you here
                        • http://dev.mysql.com/doc/refman/5.6/en/
                          numeric-functions.html

Monday, July 25, 2011
Common Functions
                    •   COUNT(FIELD) or SUM(FIELD)
                        •Counts the number of or adds up the total
                         value of results in a column
                    •   CURDATE()
                        •Is the current date (not time, just date)
                    •   MONTH(FIELD) and YEAR(FIELD)
                        •Return the month and year from a field
                    •   DATE_SUB(DATE, INTERVAL)
                        •Subtract a period of time from a date
Monday, July 25, 2011
Thank You
                                   Nicole C. Engard
                                  nengard@gmail.com

                        Slides: web2learning.net > Publications &
                                      Presentations

Monday, July 25, 2011

More Related Content

PDF
Sequel @ madrid-rb
PDF
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
PPTX
Database Programming
PPT
9780538745840 ppt ch08
PDF
Module 3 design and implementing tables
PDF
Training Librarians: Tips from the Trenches
PDF
Open Source for Libraries
Sequel @ madrid-rb
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
Database Programming
9780538745840 ppt ch08
Module 3 design and implementing tables
Training Librarians: Tips from the Trenches
Open Source for Libraries

Viewers also liked (11)

PDF
Libraries Developing Openly
PDF
The New Age of Librarianship
ODP
Open Source: Freedom and Community
ODP
Training Koha Libraries - KohaCon13
PDF
Practical Open Source Software for Libraries
PDF
The Accidental Systems Librarian
PDF
Panning for Gold: Sifting through Emerging Technologies to Find the Real Trea...
PDF
Teaching and Learning Technology
PDF
Web 2.0 Tools & Applications in Libraries
ODP
Training on Koha
PDF
Implementing Open Source
Libraries Developing Openly
The New Age of Librarianship
Open Source: Freedom and Community
Training Koha Libraries - KohaCon13
Practical Open Source Software for Libraries
The Accidental Systems Librarian
Panning for Gold: Sifting through Emerging Technologies to Find the Real Trea...
Teaching and Learning Technology
Web 2.0 Tools & Applications in Libraries
Training on Koha
Implementing Open Source
Ad

Similar to Coding Potpourri: MySQL (20)

PPTX
CVJ531: Intro to MySQL
PDF
Presenter manual php and mysql with cms (specially for summer interns)
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
PDF
My sql102
PPT
SQL Reports in Koha
PPT
Unit04 dbms
ODP
Mysql1
PPTX
Session 2 - "MySQL Basics & Schema Design"
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT V (5).pptx
PDF
Database Design most common pitfalls
PPT
ODP
PDF
Getting started with MySQL
PDF
MySQL for beginners
PPTX
Mis04
PPT
Lecture 15 - MySQL- PHP 1.ppt
CVJ531: Intro to MySQL
Presenter manual php and mysql with cms (specially for summer interns)
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
My sql102
SQL Reports in Koha
Unit04 dbms
Mysql1
Session 2 - "MySQL Basics & Schema Design"
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT V (5).pptx
Database Design most common pitfalls
Getting started with MySQL
MySQL for beginners
Mis04
Lecture 15 - MySQL- PHP 1.ppt
Ad

More from Nicole C. Engard (17)

PDF
Intro to Wordpress
PDF
Open Source in Libraries: Freedom and Community
PDF
Open Source Tools for Libraries
PDF
Using Wordpress To Create Your Website
PDF
Open Source for Libraries
PDF
Why Should I Care? New Technologies for Libraries & Librarians
PDF
Open Source Software for Libraries
PDF
Koha: Participation is Key
KEY
Intoduction to Koha Technical Services
KEY
Koha Advanced Functions
PDF
Why Should I Care? New Technologies for Libraries & Librarians
PDF
Providing Services to our Remote Users: Open Source Solutions
PDF
Practical Open Source Software for Libraries (part 2)
PDF
Practical Open Source Software for Libraries (part 1)
PDF
Operating on a Budget: Ubuntu for Libraries
PDF
Open Source Technology for Libraries
PDF
Library mashups: Exploring new ways to deliver library data
Intro to Wordpress
Open Source in Libraries: Freedom and Community
Open Source Tools for Libraries
Using Wordpress To Create Your Website
Open Source for Libraries
Why Should I Care? New Technologies for Libraries & Librarians
Open Source Software for Libraries
Koha: Participation is Key
Intoduction to Koha Technical Services
Koha Advanced Functions
Why Should I Care? New Technologies for Libraries & Librarians
Providing Services to our Remote Users: Open Source Solutions
Practical Open Source Software for Libraries (part 2)
Practical Open Source Software for Libraries (part 1)
Operating on a Budget: Ubuntu for Libraries
Open Source Technology for Libraries
Library mashups: Exploring new ways to deliver library data

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PPT
Teaching material agriculture food technology
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
A Presentation on Artificial Intelligence
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Approach and Philosophy of On baking technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Modernizing your data center with Dell and AMD
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Cloud computing and distributed systems.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
Teaching material agriculture food technology
NewMind AI Monthly Chronicles - July 2025
A Presentation on Artificial Intelligence
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Modernizing your data center with Dell and AMD
The AUB Centre for AI in Media Proposal.docx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
Unlocking AI with Model Context Protocol (MCP)
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Bridging biosciences and deep learning for revolutionary discoveries: a compr...

Coding Potpourri: MySQL

  • 1. Coding Potpourri: MySQL Nicole C. Engard Director of Open Source Education, ByWater Solutions Author of Practical Open Source Software for Libraries Monday, July 25, 2011
  • 2. My What? • MySQL: My Structured Query Language • Relational Database Management System • Licensed with the GNU GPL • That means it’s Open Source • Used to organize data in a structured way Monday, July 25, 2011
  • 3. Who’s Using It? • MySQL is the database behind many popular web based applications • Wordpress • YouTube • Drupal • Flickr • Wikipedia • Ebay • Facebook • Google (not searches) Monday, July 25, 2011
  • 4. Reading Table Structure CREATE TABLE `branches` ( `branchcode` varchar(10) NOT NULL default '', `branchname` mediumtext NOT NULL, `branchaddress1` mediumtext, `branchaddress2` mediumtext, `branchaddress3` mediumtext, `branchphone` mediumtext, `branchfax` mediumtext, `branchemail` mediumtext, UNIQUE KEY `branchcode` (`branchcode`) ) Monday, July 25, 2011
  • 5. Reading Table Structure • This table’s name is ‘branches’ and it stores the information about libraries or branches in Koha. • Each field is easy to identify because of its name (ex. branchname is the library name). • A field with a number in parens after it is a field that is limited in size. • For example varchar(10) means the field can have no more than 10 characters in it • Lastly, we see that ‘branchcode’ is the unique key or unique identifier in the table. Monday, July 25, 2011
  • 6. Inserting Data • Using the branches table enter a branch’s info INSERT INTO branches (branchcode, branchname, branchaddress1, branchaddress2, branchphone, branchemail) VALUES (‘LIB’, ‘My Library’, ‘123 Library Road’, ‘Philadelphia, PA’, ‘215.555.1234’, ‘info@library.com’); Monday, July 25, 2011
  • 7. Querying MySQL • To query a single table you will structure your query like this: • SELECT column_names FROM table_name [WHERE ...conditions] [ORDER BY ...conditions]; • Statements in brackets are optional • You can also select everything in a table by using an * in place of column_names Monday, July 25, 2011
  • 8. Querying MySQL • Using the branches table let’s query the data • This query will pull out only the Branch Names and Emails and put them in ascending order by name SELECT branchname, branchemail FROM branches ORDER BY branchname ASC; Monday, July 25, 2011
  • 9. Querying MySQL • Using branches let’s get the address info from one specific branch SELECT branchname, branchaddress1, branchaddress2, branchaddress3, branchemail FROM branches WHERE branchcode=‘LIB’; Monday, July 25, 2011
  • 10. Querying MySQL • Using branches let’s pull out the branch’s phone and fax in one column SELECT CONCAT(‘ph. ’, branchphone, ‘ fax ’, branchfax) as ‘contact info’ FROM branches WHERE branchcode= ‘LIB’; Monday, July 25, 2011
  • 11. Another Table CREATE TABLE `issues` ( `borrowernumber` int(11) default NULL, `itemnumber` int(11) default NULL, `date_due` date default NULL, `branchcode` varchar(10) default NULL, `returndate` date default NULL, `return` varchar(4) default NULL, `renewals` tinyint(4) default NULL, `issuedate` date default NULL, KEY `issuesborridx` (`borrowernumber`), KEY `issuesitemidx` (`itemnumber`), ) Monday, July 25, 2011
  • 12. Joining Tables • Using branches and issues let’s find out how many items circulated at each branch in a specific time period SELECT b.branchname, count(i.branchcode) as count FROM issues i LEFT JOIN branches b ON (i.branchcode=b.branchcode) WHERE i.issuedate BETWEEN ‘2011-06-01’ AND ‘2011-07-01’ GROUP BY b.branchcode ORDER BY count DESC; Monday, July 25, 2011
  • 13. Date & Time Functions • The most common use for reports is for end of the month or end of the year statistics • The MySQL manual on Date & Time functions is essential for these queries • http://dev.mysql.com/doc/refman/5.6/en/ date-and-time-functions.html Monday, July 25, 2011
  • 14. String Functions • Often you want to join two or more strings together, find a part of a string or even change a part of a string • String functions are defined in the MySQL manual • http://dev.mysql.com/doc/refman/5.6/en/ string-functions.html Monday, July 25, 2011
  • 15. Math Functions • Using MySQL you can get fancy with statistics by using the number related functions • The Numeric Functions section of the manual can help you here • http://dev.mysql.com/doc/refman/5.6/en/ numeric-functions.html Monday, July 25, 2011
  • 16. Common Functions • COUNT(FIELD) or SUM(FIELD) •Counts the number of or adds up the total value of results in a column • CURDATE() •Is the current date (not time, just date) • MONTH(FIELD) and YEAR(FIELD) •Return the month and year from a field • DATE_SUB(DATE, INTERVAL) •Subtract a period of time from a date Monday, July 25, 2011
  • 17. Thank You Nicole C. Engard nengard@gmail.com Slides: web2learning.net > Publications & Presentations Monday, July 25, 2011