310 likes | 324 Views
Learn database fundamentals, schema design, SQL implementation, and basic SQL functions hands-on with PHP and MySQL. Understand how to build, query, and update relational databases effectively.
E N D
INFM 603: Session 6Database Implementation Using PHP and MySQL Paul Jacobs The iSchool University of Maryland Wednesday, Mar. 13, 2019 This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United StatesSee http://creativecommons.org/licenses/by-nc-sa/3.0/us/ for details
Today’s Topics • Review of Database Fundamentals • How to Build/Query/Update Online Relational Databases • Hands-on Practice
Database Fundamentals - Review • What is a database? A computer mechanism for online persistent storage, organization and access to information (a technology) • Why do we need them? To maintain information in a consistent, convenient, reliable way that enables applications (a tier or architectural component) • How do we use them? Building a (usually) relational model called a schema that organizes data in tables and supports needed functionality; writing code for interfaces (implementation) Database Fundamentals Implementation Hands-on Practice
Database Implementation – Types of Database Problems • Design and Document a Database • Figure out what data has to be in there • How is the data structured? What are the types of data (e.g., names, dates, strings, etc.)? • Build the schema • Document it (so that you or someone else can implement it) • Build a Database • Create the database (tables), users, security, etc. (usually done by designer and/or DBA – administrator using various interfaces and/or ODBC/SQL) • Validate that it works • Populate the database (enter records) Database Fundamentals Implementation Hands-on Practice
Database Implementation –Database Problems (cont’d) • Program a Database • Develop interfaces for end users (e.g. to search, access information from databases – e.g., Testudo) • Develop interfaces for other systems (e.g., to place orders, perform updates, etc. – e.g., Canvas, Amazon) • Query a Database • Access data (records) already in the database • Can be done using all kinds of interfaces, or by programs (often using ODBC/SQL) • Very often done by users who are not allowed to update or change the data themselves Database Fundamentals Implementation Hands-on Practice
Documenting a Database • Usually specifies the schema, especially the tables (or object classes in an object-oriented format), but can be at various levels and description and abstraction • Can be used to communicate among different parts of a technical project team (e.g., user groups, programmers, database designers, etc.) • Can use any number of documentation methods • E-R diagrams • Class diagrams with annotations • Sketches and drawings of all sorts (such as what we used last time) • Actual database output from some stage of implementation • Best done relatively early in project because of need to build and test code to interface with the database Database Fundamentals Implementation Hands-on Practice
What is SQL (Structured Query Language)? • The “interlingua” of relational databases (e.g., Oracle, SQLServer, MySQL, Postgres, DB2, etc.) • An international standard (ISO 9075) • A common method of building and querying databases • A common method of building database interfaces (e.g., using ASP, JSP, PHP) • A common method of building connectors from applications to databases (e.g., using XML, SOAP and ODBC/JDBC) Database Fundamentals Implementation Hands-on Practice
Basic SQL functions (on records) • SELECT – get records from a database that meet certain criteria • INSERT – add records to a database • UPDATE • DELETE Database Fundamentals Implementation Hands-on Practice
Basic SQL functions - SELECT SELECT CustomerAddress FROM Customers WHERE CustomerName = ‘Will E. Coyote’; SELECT * FROM SalesOrders LIMIT 0,30; Database Fundamentals Implementation Hands-on Practice
PHPMyAdmin Output - SELECT Database Fundamentals Implementation Hands-on Practice
SELECT Using JOIN SELECT SalesOrders.SalesOrderNo, Customers.CustomerName FROM SalesOrders JOIN Customers ON (SalesOrders.CustomerNo = Customers.CustomerNo) ; Database Fundamentals Implementation Hands-on Practice
PHPMyAdmin Output - Join Database Fundamentals Implementation Hands-on Practice
Basic SQL functions - INSERT INSERT INTO `inclassdemo`.`OrderItems` ( `SalesOrderNo` , `ItemNo` , `ItemQuantity` , `ItemUnitPrice` )VALUES ( '1', '1', '12', '10.50'); Database Fundamentals Implementation Hands-on Practice
Basic SQL functions - UPDATE UPDATE `inclassdemo`.`SalesOrders` SET `CustomerNo` = '5', `ClerkNo` = '2' WHERE `SalesOrders`.`SalesOrderNo` =1 LIMIT 1 ; Database Fundamentals Implementation Hands-on Practice
Basic SQL functions - DELETE DELETE FROM Customers WHERE CustomerName = 'Peter Piper'; Database Fundamentals Implementation Hands-on Practice
Basic SQL functions (on tables) • CREATE – make a new table • DROP – delete a table • ALTER – change (e.g., fields) of a table Database Fundamentals Implementation Hands-on Practice
Basic SQL functions - CREATE CREATE TABLE `SalesOrders` ( `SalesOrderNo` INT( 6 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,`Date` DATE NOT NULL ,`CustomerNo` INT NOT NULL ,`ClerkNo` INT NOT NULL ,`Total` FLOAT( 9, 2 ) NOT NULL ) [in MySQL requires separate constraint PRIMARY KEY (SalesOrderNo)] Database Fundamentals Implementation Hands-on Practice
Basic SQL functions – DROP AND ALTER DROP TABLE Pets; ALTER TABLE SalesOrders ADD Season VARCHAR (10); Database Fundamentals Implementation Hands-on Practice
Example Database (Final Tables) Table: SalesOrders • SalesOrderNo • Date • CustomerNo • ClerkNo • Total Table: OrderItems • SalesOrderNo • ItemNo • ItemQuantity • ItemUnitPrice Table: InventoryItems • ItemNo • ItemDescription Table: Customers • CustomerNo • CustomerName • CustomerAddress Table: Clerks • ClerkNo • ClerkName Database Fundamentals Implementation Hands-on Practice
Building the Sample With PHPMyadmin – Create Tables Database Fundamentals Implementation Hands-on Practice
Building the Sample With PHPMyadmin – Add Data Database Fundamentals Implementation Hands-on Practice
Building the Sample With PHPMyadmin – Add Data (SQL) Database Fundamentals Implementation Hands-on Practice
Building the Sample With PHPMyadmin – Build Queries Database Fundamentals Implementation Hands-on Practice
Hands On Practice Build and populate the sample database
SQL is Quite Powerful • Data types – Most common are INT (Integer), VARCHAR (Variable length string), and DATE, but there are many other related types, e.g. TIMESTAMP, BLOB (binary large object), FLOAT (floating point) • Functions – There are functions for almost any data type, e.g. – • Date conversion and formatting, such as DATE_FORMAT(Date, ‘%b . %m, %Y) TIMESTAMPDIFF(YEAR, BirthDate,CURDATE()) • All kinds of arithmetic, like SQRT() and POW() • Numerous tools and tricks for manipulating, sorting, comparing, etc. Database Fundamentals Implementation Hands-on Practice
Example: Typical Contact DB Database Fundamentals Implementation Hands-on Practice
Example: Age Query Database Fundamentals Implementation Hands-on Practice
Example: Birthday Query Database Fundamentals Implementation Hands-on Practice
Database Implementation - Recap • Reviewed the role of databases in [web] applications • Introduced SQL as the “interlingua” of relational databases • Presented and tried out one type of database server using PHPMyAdmin/MySQL • Next up: Integrating with web pages via PHP and other advanced methods, tying things together Database Fundamentals Implementation Hands-on Practice