Download presentation
Presentation is loading. Please wait.
Published byDavid Morris Modified over 9 years ago
1
CHAPTER 7 Database: SQL, MySQL
2
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
3
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
4
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
5
Relational Database Model 5 Fig. 22.1Relational database structure of an Employee table.
6
6 Relational Database Model departmentlocation 413New Jersey 642Los Angeles 611Orlando Fig. 22.2Table formed by selecting department and location data from the Employee table.
7
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
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.
9
9 Relational Database Overview: Books.mdb Database
10
10 Relational Database Overview: Books.mdb Database
11
11 Relational Database Overview: Books.mdb Database
12
12 Relational Database Overview: Books.mdb Database
13
13 Relational Database Overview: Books.mdb Database
14
14 Relational Database Overview: Books.mdb Database
15
15 Relational Database Overview: Books.mdb Database Fig. 22.11Table relationships in Books.mdb Foreign key- can appears many times in its own table Appears exactly once as primary key of other table
16
16 SQL (Structured Query Language)
17
17 Basic SELECT Query SELECT * FROM tableName –SELECT * FROM Authors –SELECT authorID, lastName FROM Authors
18
18 Basic SELECT Query
19
19 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*’
20
20 WHERE Clause
21
21 WHERE Clause
22
22 WHERE Clause
23
23 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
24
24 ORDER BY Clause
25
25 ORDER BY Clause
26
26 ORDER BY Clause SELECT authorID, firstName, lastName FROM Authors ORDER BY lastName, firstName
27
27 ORDER BY Clause
28
28 ORDER BY Clause SELECT isbn, title, editionNumber, copyright, price FROM Titles WHERE title LIKE ‘*How to Program’ ORDER BY title ASC
29
29 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 –SELECT firstName, lastName, isbn FROM Authors INNER JOIN AuthorISBN ON Authors.authorID = AuthorISBN.authorID ORDER BY lastName, firstName
30
30 Merging Data from Multiple Tables: INNER JOIN
31
31 Fig. 22.22 (1 of 1)
32
32 Joining Data from Tables Authors, AuthorISBN, Titles and Publishers
33
33 Joining Data from Tables Authors, AuthorISBN, Titles and Publishers
34
34 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’ )
35
35 INSERT Statement
36
36 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 lastName=‘Sue’
37
37 UPDATE Statement
38
38 DELETE Statement Removes data from a table –DELETE FROM tableName WHERE criteria –DELETE FROM Authors WHERE lastName=‘Jones’ AND firstName=‘Sue’
39
39 DELETE Statement
40
40 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.