CHAPTER 8 Database: SQL, MySQL
Topics Introduction Relational Database Model Relational Database Overview: Books.mdb Database SQL (Structured Query Language)Basic SELECT Query WHERE Clause ORDER BY Clause Merging Data from Multiple Tables: INNER JOIN Joining Data from Tables Authors, AuthorISBN, Titles and Publishers INSERT Statement UPDATE Statement DELETE Statement
Introduction Database –Integrated collection of data –Database management system (DBMS) Store and organize data consistent with database’s format Relational database –SQL (Structured Query Language) »Queries »Manipulate data
Relational Database Model Composed of tables Row –Number column –Primary key Reference data in the table A column or set of columns in table contains unique data
Relational Database Model 5 Fig. 22.1Relational database structure of an Employee table.
6 Relational Database Model departmentlocation 413New Jersey 642Los Angeles 611Orlando Fig. 22.2Table formed by selecting department and location data from the Employee table.
Relational Database Model Primary key uniquely identifies each row –Rule of Entity Integrity Composite primary key Lines connecting tables –Relationships One-to-many relationship Foreign key –Join multiple tables –Rule of Referential Integrity
8 Employee Staff_IDNameDepartment 123AhmadGraphic 456ArdenHR 789ZikryEngineering 222SafiaEngineering 111HendraFinance Department Graphic HR Engineering Finance Primary Key Foreign Key When these two tables are linked, Department is a foreign key in table Employee. Data in Department column may be repeatable. Department is a primary key in table Department. Data in Department column must be unique.
MySQL Data Types When creating a MySQL table, specifying a data type for every field is necessary. This data type plays an important role in enforcing the integrity of the data in a MySQL database, and in making this data easier to use and manipulate.
MySQL Data Types TypeUsed For TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT Integer values FLOATSingle-precision floating-point values DOUBLEDouble-precision floating-point values DECIMALDecimal values CHARFixed-length strings up to 255 characters VARCHARVariable-length strings up to 255 characters TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB Large blocks of binary data TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT Longer blocks of text data DATEDate values TIMETime values or durations YEARYear values DATETIMECombined date and time values TIMESTAMPTimestamps ENUM Fields that must contain one of a set of predefined mutually exclusive values SETFields that can contain zero, one, or more of a set of predefined values
MySQL Data Types If you try to use a value that’s too big for the filed you’re placing it in, MySQL will automatically truncate or round the value down to the maximum allowed value for that field. The data types different with each others in term of size of values that they can store.
12 Relational Database Overview: Books.mdb Database
13 Relational Database Overview: Books.mdb Database
14 Relational Database Overview: Books.mdb Database
15 Relational Database Overview: Books.mdb Database
16 Relational Database Overview: Books.mdb Database
17 Relational Database Overview: Books.mdb Database
18 Relational Database Overview: Books.mdb Database Fig Table relationships in Books.mdb Foreign key- can appears many times in its own table Appears exactly once as primary key of other table
19 SQL (Structured Query Language)
20 Basic SELECT Query SELECT * FROM tableName –SELECT * FROM Authors –SELECT authorID, lastName FROM Authors
21 Basic SELECT Query
22 WHERE Clause Specify selection criteria for query –SELECT columnName1, columnName2, … FROM tableName WHERE criteria SELECT title, editionNumber, copyright FROM Titles WHERE copyright > 1999 –LIKE Pattern matching –Asterisk ( * ) »SELECT authorID, firstName, lastName FROM Authors WHERE lastName LIKE ‘D*’ –Question mark ( ? ) »SELECT authorID, firstName, lastName FROM Authors WHERE lastName LIKE ‘?I*’
23 WHERE Clause
24 WHERE Clause
25 WHERE Clause
26 ORDER BY Clause Arranged in ascending or descending order –SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC SELECT authorID, firstName, lastName FROM Authors ORDER BY lastName ASC –SELECT columnName1, columnName2, … FROM tableName ORDER BY column DESC SELECT authorID, firstName, lastName FROM Authors ORDER BY lastName DESC
27 ORDER BY Clause
28 ORDER BY Clause
29 ORDER BY Clause SELECT authorID, firstName, lastName FROM Authors ORDER BY lastName, firstName
30 ORDER BY Clause
31 ORDER BY Clause SELECT isbn, title, editionNumber, copyright, price FROM Titles WHERE title LIKE ‘*How to Program’ ORDER BY title ASC
32 Merging Data from Multiple Tables: INNER JOIN Normalize databases –Ensure database does not store data redundantly –SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1, columnName = table2.columnName
<?php // Make a MySQL Connection // Construct our join query $query = "SELECT family.Position, food.Meal ". "FROM family, food ". "WHERE family.Position = food.Position"; $result = mysql_query($query) or die(mysql_error()); // Print out the contents of each row into a table while($row = mysql_fetch_array($result)){ echo $row['Position']. " - ". $row['Meal']; echo " "; }?> Merging Data from Multiple Tables: INNER JOIN family Table food Table Output: Dad - Steak Mom - Salad Dad - Tacos
34 Merging Data from Multiple Tables: INNER JOIN –SELECT firstName, lastName, isbn FROM Authors INNER JOIN AuthorISBN ON Authors.authorID = AuthorISBN.authorID ORDER BY lastName, firstName
35 Merging Data from Multiple Tables: INNER JOIN
36 Fig (1 of 1)
37 Joining Data from Tables Authors, AuthorISBN, Titles and Publishers
38 Joining Data from Tables Authors, AuthorISBN, Titles and Publishers
39 INSERT Statement Inserts new row in table –INSERT INTO tableName ( columnName1, columnName2, …, columnNameN ) VALUES ( value1, value2, …, valueN ) –INSERT INTO Authors ( firstName, lastName ) VALUES ( ‘Sue’, ‘Smith’ )
40 INSERT Statement
41 UPDATE Statement Modifies data in a table –UPDATE tableName SET columnName1 = value1, columnName2 = value2, …, columnNameN = valueN WHERE criteria –UPDATE Authors SET lastName = ‘Jones’ WHERE lastName= ‘Smith’ AND firstName=‘Sue’
42 UPDATE Statement
43 DELETE Statement Removes data from a table –DELETE FROM tableName WHERE criteria –DELETE FROM Authors WHERE lastName=‘Jones’ AND firstName=‘Sue’
44 DELETE Statement
45 MySQL Multi-user and multi-threaded RDBMS server Uses SQL to interact with and manipulate data Supports various programming languages Access tables from different databases Handle large databases The next chapter explains the usage of MySQL with PHP.