Database, Mysql, SQL
Database, Mysql, SQL
Database, Mysql, SQL
Database, MySQL,
SQL
RECAP: HTTP Server (Python 3.x)
• Examples of databases:
– Telephone book white pages
– T.V. Guide
– Airline reservation system
– Motor vehicle registration records
– Papers in your filing cabinet
– Files on your computer hard drive
Relational Databases
department location
413 New Jersey
611 Orlando
642 Los Angeles
Fig. 22.2 Table formed by selecting department and location data from the Employee table.
Relational Database Overview: Books
Database – Titles Table Structure
Field Description
publisherID The publisher’s ID number in the database. This
auto-incremented Integer is the table’s primary
key.
publisherName The name of the publisher (a String).
Fig. 22.5 Publishers table from Books.mdb.
Relational Database Overview: Books.mdb
Database – Publishers Table Data
Relational Database Overview: Books
Database – Authors Table Structure
Field Description
authorID Author’s ID number in the database. In the Books.mdb
database, this Integer column is defined as auto-
increment. For each new row inserted in this table, the
database increments the authorID value, ensuring that
each row has a unique authorID. This column represents
the table’s primary key.
firstName Author’s first name (a String).
lastName Author’s last name (a String).
Fig. 22.3 Authors table from Books.mdb.
Relational Database Overview: Books
Database – Authors Table Data
Field Description
authorID The author’s ID number, which allows the database
to associate each book with a specific author. The
integer ID number in this column must also appear
in the Authors table.
isbn The ISBN number for a book (a String).
Fig. 22.7 AuthorISBN table from Books.mdb.
Relational Database Overview: Books
Database – AuthorsISBN Table Data
DBMS
• Creating Database
– SQL syntax
• create database <database-name>;
– Example
• create database Books;
• Selecting Database
– SQL syntax
• use <database-name>;
– Example
• use Books;
CREATE TABLE: Column Types
• Example: AuthorsISBN
– INSERT AuthorsISBN SET isbn =
'0130923613', authorId=1;
– INSERT AuthorsISBN SET isbn =
'0130923214', authorId=1;
Basic SELECT SQL Query
• Examples
– E.g SELECT * FROM Authors
• Example:
– SELECT Authors.firstName, Authors.lastName,
Titles.isbn
FROM Authors, Titles, AuthorsISBN
Where AuthorsISBN.authorId = Authors.authorId
and AuthorsISBN.isbn = Titles.isbn;
Merging Data from Multiple Tables: INNER
JOIN
firstName lastName isbn firstName lastName isbn
Harvey Deitel 0130895601 Harvey Deitel 0130856118
Harvey Deitel 0130284181 Harvey Deitel 0130161438
Harvey Deitel 0130284173 Harvey Deitel 013028419x
Harvey Deitel 0130829293 Harvey Deitel 0139163050
Harvey Deitel 0134569555 Harvey Deitel 0135289106
Harvey Deitel 0130829277 Harvey Deitel 0130895717
Harvey Deitel 0130852473 Harvey Deitel 0132261197
Harvey Deitel 0138993947 Harvey Deitel 0130895725
Harvey Deitel 0130125075 Paul Deitel 0130895601
Paul Deitel 0130284181 Paul Deitel 0135289106
Paul Deitel 0130284173 Paul Deitel 0130895717
Paul Deitel 0130829293 Paul Deitel 0132261197
Paul Deitel 0134569555 Paul Deitel 0130895725
Paul Deitel 0130829277 Tem Nieto 0130284181
Paul Deitel 0130852473 Tem Nieto 0130284173
Paul Deitel 0138993947 Tem Nieto 0130829293
Paul Deitel 0130125075 Tem Nieto 0134569555
Paul Deitel 0130856118 Tem Nieto 0130856118
Paul Deitel 0130161438 Tem Nieto 0130161438
Paul Deitel 013028419x Tem Nieto 013028419x
Paul Deitel 0139163050 Sean Santry 0130895601
Fig. 22.21 Authors from table Authors and ISBN numbers of the authors’
books, sorted in ascending order by lastName and firstName.
UPDATE Statement
• Example
– UPDATE Authors SET firstName='Timothy'
WHERE lastName='Neito';
UPDATE Statement
• E.g
– DELETE FROM Authors WHERE firstName = 'Sue';
DELETE Statement
import pymysql
conn = pymysql.connect(host='127.0.0.1’,
user='root', passwd='root', db=’Books')
cur = conn.cursor()
cur.execute("SELECT firstName,lastName FROM
Authors")
for r in cur:
print(r)
cur.close()
conn.close()