Your SlideShare is downloading. ×
0
Loose Ends• Homework      – Only Readings were assigned• Example from the class• http://www.hotscripts.comMurachs PHP and ...
Chapter 3                    Introduction              to relational databases                     and MySQL              ...
A web-based clientMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.                                      ...
The Welcome pageMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.                                        ...
How to start phpMyAdmin on a local computer1. From the XAMPP Control Panel, start the Apache and MySQL   servers if they a...
How to change your password• Go to the Home page by clicking on the Home button. Then, click  the Change Password link.• O...
If you are/were following along, the Murach databases have been   loaded – ‘my_guitar_shop1’ and ‘my_guitar_shop2’       T...
The Structure tab for a database    Click the Browse icon…..Murachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, ...
The Browse tab for the categories tableCongratulations! You just ran your first SELECT query!Murachs PHP and MySQL, C3   ©...
The SQL tab with a statement ready to run   Using my_guitar_shop1, enter the above…Murachs PHP and MySQL, C3   © 2010, Mik...
The SQL tab after a SQL statement has been runMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.          ...
Database Administration / SecurityCreating a user with limited privilegeson one table     GRANT SELECT     ON my_guitar_sh...
Common privilegesPrivilege                    DescriptionSELECT                       Lets the user select data.UPDATE    ...
The SQL tab for mgs_tester after an UPDATE       statement failedMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associa...
• Let’s look more at the tablesMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.                         ...
Tables• A relational database consists of tables.• Tables consist of rows and columns (or records and fields)• A table is ...
A products table with 5 columns and 10 rows       (primary key= productID)Primary key, Columns, RowsMurachs PHP and MySQL,...
The categories table (primary key = categoryID)Murachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.         ...
Table Relationships                          (Linking Tables)• Simple Table Relationships create most any complex  informa...
The products table (foreign key = categoryID)Murachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.           ...
• My hand drawn table relationship diagramsMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.             ...
Creating Tables               (Data/Schema Definition)• Need to be more formal in table definition• Data type and size mus...
Column definitions for the products tableMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.               ...
Common MySQL data types              CHAR              VARCHAR              INT              DECIMAL              DATE, TI...
Interacting with Tables                (Database Manipulation)•   SELECT•   INSERT•   UPDATE•   DELETEMurachs PHP and MySQ...
The SELECT statement syntax for all columns     SELECT *     FROM table-1     [WHERE selection-criteria]     [ORDER BY col...
The syntax for selected columns     SELECT column-1 [, column-2] ...     FROM table-1     [WHERE selection-criteria]     [...
Linking Multiple Tables• It would be nice for the programmer if all an  application’s data could be stored in ONE BIG  tab...
The syntax that joins two tables     SELECT column-1 [, column-2] ...     FROM table-1        {INNER | LEFT OUTER | RIGHT ...
Complex Joins  We can even join across more than two  tables:  SELECT column-1  FROM table-1       INNER JOIN products    ...
Multiple Table Example• Get the category, product, and list price for only  those items that are over $800Murachs PHP and ...
INNER JOINA statement that gets data from two related tables  SELECT categoryName, productName, listPrice  FROM categories...
LEFT OUTER JOIN ExampleFirst need to add a category to the categories tablethat is not in the product tableMurachs PHP and...
The rest of the DML• INSERT• UPDATE• DELETEMurachs PHP and MySQL, C3   © 2010, Mike Murach & Associates, Inc.             ...
The syntax for the INSERT statement  INSERT INTO table-name [(column-list)]  VALUES (value-list)A statement that adds one ...
Back to LEFT OUTER JOIN ExampleA statement that gets data from two related tables  SELECT categoryName, productName, listP...
The syntax for the UPDATE statement    UPDATE table-name    SET expression-1 [, expression-2] ...    WHERE selection-crite...
The syntax for the DELETE statement    DELETE FROM table-name    WHERE selection-criteriaA statement that deletes one row ...
Homework• If you have not installed the Murach databases  for the guitar shop, DO IT ALREADY!• P.122, Exercises 8, 9, 10, ...
Upcoming SlideShare
Loading in...5
×

Part 2

445

Published on

Part 2

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
445
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • SELECT * FROM products WHERE categoryID = 2
  • SELECT productName, listPrice FROM products WHERE listPrice < 500 ORDER BY listPrice ASC
  • SELECT categoryName, productName, listPrice FROM categories INNER JOIN products ON categories.categoryID = products.categoryID WHERE listPrice > 800 ORDER BY listPrice ASC
  • SELECT categoryName, productName, listPrice FROM categories LEFT OUTER JOIN products ON categories.categoryID = products.categoryID
  • SELECT categoryName, productName, listPrice FROM categories LEFT OUTER JOIN products ON categories.categoryID = products.categoryID
  • Transcript of "Part 2"

    1. 1. Loose Ends• Homework – Only Readings were assigned• Example from the class• http://www.hotscripts.comMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 1
    2. 2. Chapter 3 Introduction to relational databases and MySQL CONTINUEDMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 2
    3. 3. A web-based clientMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 3
    4. 4. The Welcome pageMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 4
    5. 5. How to start phpMyAdmin on a local computer1. From the XAMPP Control Panel, start the Apache and MySQL servers if they aren’t already running.2. Click the Admin button for the MySQL module to start the phpMyAdmin tool in your default web browser.How to log in• Enter your username and password.How to log out and return to the Welcome page• Click the Log out toolbar button (the Exit sign) that’s in the sidebar of most pages. Or, go the Home page by clicking on the Home button, and then click the Log out link.Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 5
    6. 6. How to change your password• Go to the Home page by clicking on the Home button. Then, click the Change Password link.• On the Change Password page, enter and re-enter your new password, and click the Go button. I can’t find the “Change Password” link, but I never “closed” my XAMPP installation. I would have to change the <config.inc.php> file in the /phpmyadmin subdirectory. OR run (on a Mac) /Applications/XAMPP/xamppfiles/xampp security This will start an interactive program.Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 6
    7. 7. If you are/were following along, the Murach databases have been loaded – ‘my_guitar_shop1’ and ‘my_guitar_shop2’ The Databases tabMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 7
    8. 8. The Structure tab for a database Click the Browse icon…..Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 8
    9. 9. The Browse tab for the categories tableCongratulations! You just ran your first SELECT query!Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 9
    10. 10. The SQL tab with a statement ready to run Using my_guitar_shop1, enter the above…Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 10
    11. 11. The SQL tab after a SQL statement has been runMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 11
    12. 12. Database Administration / SecurityCreating a user with limited privilegeson one table GRANT SELECT ON my_guitar_shop1.products TO mgs_tester@localhost IDENTIFIED BY pa55wordCreating a user with limited privilegeson all tables GRANT SELECT, INSERT, DELETE, UPDATE ON my_guitar_shop1.* TO mgs_user@localhost IDENTIFIED BY pa55wordMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 12
    13. 13. Common privilegesPrivilege DescriptionSELECT Lets the user select data.UPDATE Lets the user update data.INSERT Lets the user insert data.DELETE Lets the user delete data.CREATE TABLE Lets the user create a table.DROP TABLE Lets the user drop a table.Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 13
    14. 14. The SQL tab for mgs_tester after an UPDATE statement failedMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 14
    15. 15. • Let’s look more at the tablesMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 15
    16. 16. Tables• A relational database consists of tables.• Tables consist of rows and columns (or records and fields)• A table is modeled after a real entity (product, customer, etc.)• A column is some attribute of the entity• A row is a set of values for one instance of an entity• The intersection of a row and column can be called a cell• Most tables have a primary key that uniquely defines each row in the table• The primary key is usually a single column, but can consist of two or more columns• If another column can uniquely identify each row, it is called a non-primary or unique keyMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 16
    17. 17. A products table with 5 columns and 10 rows (primary key= productID)Primary key, Columns, RowsMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 17
    18. 18. The categories table (primary key = categoryID)Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 18
    19. 19. Table Relationships (Linking Tables)• Simple Table Relationships create most any complex information relationships needed – One to many table relationship – One to one table relationship – Many to many table relationship• Relations are created using foreign key columns – A foreign key identifies a related row in a different table – A table can have a more than one foreign key – You cannot add a row to a table with a foreign key unless there is a matching primary key in the other tableMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 19
    20. 20. The products table (foreign key = categoryID)Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 20
    21. 21. • My hand drawn table relationship diagramsMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 21
    22. 22. Creating Tables (Data/Schema Definition)• Need to be more formal in table definition• Data type and size must be specified• Column definition indicates whether it can or cannot have null values• A column can also be defined with a default value.• A column can be defined to auto-increment.Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 22
    23. 23. Column definitions for the products tableMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 23
    24. 24. Common MySQL data types CHAR VARCHAR INT DECIMAL DATE, TIME, DATETIME MySQL Database Definition Language CREATE DATABASE DROP DATABASE CREATE TABLE ALTER TABLEMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 24
    25. 25. Interacting with Tables (Database Manipulation)• SELECT• INSERT• UPDATE• DELETEMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 25
    26. 26. The SELECT statement syntax for all columns SELECT * FROM table-1 [WHERE selection-criteria] [ORDER BY column-1 [ASC|DESC] [, column-2 [ASC|DESC] ...] ]A SELECT statement that gets all columns SELECT * FROM products WHERE categoryID = 2The result table Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 26
    27. 27. The syntax for selected columns SELECT column-1 [, column-2] ... FROM table-1 [WHERE selection-criteria] [ORDER BY column-1 [ASC|DESC] [, column-2 [ASC|DESC] ...] ]A statement that gets selected columns and rows SELECT productName, listPrice FROM products WHERE listPrice < 500 ORDER BY listPrice ASCThe result tableMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 27
    28. 28. Linking Multiple Tables• It would be nice for the programmer if all an application’s data could be stored in ONE BIG table. – Always use a (simple) SELECT query, pick off the desired columns…• Database architects, however, follow a process that normalizes database tables – In essence, this creates MORE (than one), SMALLER tables that are related (one-to-many, one-to-one, many-to-many)• Need the ability to SELECT across tablesMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 28
    29. 29. The syntax that joins two tables SELECT column-1 [, column-2] ... FROM table-1 {INNER | LEFT OUTER | RIGHT OUTER} JOIN table-2 ON table-1.column-1 {=|<|>|<=|>=|<>} table-2.column-2 [WHERE selection-criteria] [ORDER BY column-1 [ASC|DESC] [, column-2 [ASC|DESC] ...] ]INNER JOIN: Return those rows such that the key of the row ofthe first table is equal to the key of the row of the second table.OUTER JOIN: Return all the rows from one table[(LEFT / first) or (RIGHT / second) even if the other tabledoes not contain a matching row.LEFT OUTER JOIN: Return all the rows from the from the first table(left) AND only the data from the second (right) table that matches. Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 29
    30. 30. Complex Joins We can even join across more than two tables: SELECT column-1 FROM table-1 INNER JOIN products ON criteria INNER JOIN orderitems On criteriaMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 30
    31. 31. Multiple Table Example• Get the category, product, and list price for only those items that are over $800Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 31
    32. 32. INNER JOINA statement that gets data from two related tables SELECT categoryName, productName, listPrice FROM categories INNER JOIN products ON categories.categoryID = products.categoryID WHERE listPrice > 800 ORDER BY listPrice ASCThe result table Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 32
    33. 33. LEFT OUTER JOIN ExampleFirst need to add a category to the categories tablethat is not in the product tableMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 33
    34. 34. The rest of the DML• INSERT• UPDATE• DELETEMurachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 34
    35. 35. The syntax for the INSERT statement INSERT INTO table-name [(column-list)] VALUES (value-list)A statement that adds one row to a table INSERT INTO products (categoryID, productCode, productName, listPrice) VALUES (1, tele, Fender Telecaster, 599.00)A statement that uses the MySQL NOW functionto get the current date INSERT INTO orders (customerID, orderDate) VALUES (1, NOW()) INSERT INTO categories (categoryName) VALUES ( ‘Saxophones’) Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 35
    36. 36. Back to LEFT OUTER JOIN ExampleA statement that gets data from two related tables SELECT categoryName, productName, listPrice FROM categories LEFT OUTER JOIN products ON categories.categoryID = products.categoryIDThe result table Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 36
    37. 37. The syntax for the UPDATE statement UPDATE table-name SET expression-1 [, expression-2] ... WHERE selection-criteriaA statement that updates a column in one row UPDATE products SET productName = Ludwig 5-Piece Kit with Zildjian Cymbals WHERE productCode = ludwigA statement that updates multiple rows UPDATE products SET listPrice = 299 WHERE categoryID = 1 Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 37
    38. 38. The syntax for the DELETE statement DELETE FROM table-name WHERE selection-criteriaA statement that deletes one row from a table DELETE FROM products WHERE productID = 1A statement that deletes multiple rows DELETE FROM products WHERE listPrice > 200 DELETE FROM categories WHERE categoryName = ‘Saxophones’ Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 38
    39. 39. Homework• If you have not installed the Murach databases for the guitar shop, DO IT ALREADY!• P.122, Exercises 8, 9, 10, 11, 12, 13• Continue to explore the Guitar Shop – In particular, Chapter 20 application• And, modify the future_value.js code to use an is_input_valid function• And, if necessary, debug calculate_click()Murachs PHP and MySQL, C3 © 2010, Mike Murach & Associates, Inc. Slide 39
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×