Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Design: Basic to Advanced Techniques Fall 2010 Mondays 7-9pm 200 Sutardja-Dai Hall Databases & SQL Lecture Code:

Similar presentations


Presentation on theme: "Web Design: Basic to Advanced Techniques Fall 2010 Mondays 7-9pm 200 Sutardja-Dai Hall Databases & SQL Lecture Code:"— Presentation transcript:

1 Web Design: Basic to Advanced Techniques Fall 2010 Mondays 7-9pm 200 Sutardja-Dai Hall Databases & SQL Lecture Code:

2 Today’s Agenda Quiz Lecture Lab

3 Announcements Final Project Specs up this week

4 What is MySQL? Client Side Web Browser HTTP Request (visit website) Interpret and render received files JavaScript Execution Server Side Web Server Serve Website Send HTML, CSS, and JavaScript files Send images Interprets and executes PHP code PHP code calls/talks to Database

5 What is MySQL? MySQL is a brand of database software Relational database management system (RDMS) Named after original developer’s daughter: My Closely used with PHP, and partial reason for success Used by Google, Wikipedia, Facebook, YouTube…

6 Other Systems MySQL PostgreSQL SQLite Microsoft SQL Server Notice “SQL”. Pronounced “sequel”.

7 Where do Databases Fit In?

8 Client Server.

9 Web Server Communication 1. Open URL http://jonathanmui.aw- industries.com/mylife.phphttp://jonathanmui.aw- industries.com/mylife.php 2. Web Server receives request and determines it is a php file, so it will send processing to the PHP interpreter. 3. Interpreter looks up File System 4. File system returns the corresponding file 5. Interpreter looks up database 6. Database returns result set 7. PHP Interpreter does work on the data obtained, then translates all that into HTML 8. Web Server serves you the HTML <?php print("Family & Friends"); $album_files = scandir("Pics"); for($index = 2; $album_files[$index]; $index++){?> > <?php } ?> 1 2 3 4 5 6 7 8 9 10 11

10 Use Cases Directory Services User authentication Banking Reservations Browser cache Many, many things! What about our Cal student information? Our grades?

11 SQL Structured Query Language Databases are not just a repository of information Can ask the database questions about the data How many students are enrolled in the DeCal? What is their average attendance rate? How many have turned in all assignments? Which student is doing the best in the course? Does a user with this password exist? Need to maintain the data Create, Read, Update, Delete

12 CRUD Create "INSERT INTO `products` (`name`,`price`) VALUES (‘Tonka truck’, ‘13.00’)" Read "SELECT `index`, `name` FROM `products`" Update “UPDATE `products` SET `price` = 20.00 WHERE `name` = ‘Tonka truck’” Delete “DELETE FROM `products` WHERE `name` = ‘Tonka truck’”

13 How is Data Stored in a Database? P_IDNAMEPRICE 1Kerbie & Ban Townhouse 10.00 2Tonka Truck20.00 3Brain Yo-Yo13.00 VarcharDecimalInteger A unique “Primary Key” Products

14 How is Data Stored in a Database? A: In Relational Tables Each table has a name Columns have labels Each column stores a different type of data An entry in a database appears as a ROW Each column in the row has a value All rows have a key – a unique identifier Typically an integer

15 Data Types Integer Double Float Varchar Longtext Boolean …

16 Relationships Let’s model…relationships: Girlfriend and Boyfriend G_IDNAME 1Jessica 2Portia 3Jane B_IDNAMEG_ID 1Bob3 2Kensington2 3Billy1 GirlfriendsBoyfriends Foreign Key

17 Some Terminology Primary Key A primary key is used to uniquely identify each row in a table. It can either be part of the actual record itself, or it can be an artificial field (one that has nothing to do with the actual record). A primary key can consist of one or more fields on a table. When multiple fields are used as a primary key, they are called a composite key Foreign Key A field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data References: http://www.1keydata.com/sql/http://www.1keydata.com/sql/

18 Boyfriend(s) Portia seduces Billy… G_IDNAME 1Jessica 2Portia 3Jane B_IDNAMEG_ID 1Bob3 2Kensington2 3Billy1 2 GirlfriendsBoyfriends

19 Girlfriend(s) Bob picks up the slack… G_IDNAME 1Jessica 2Porschea 3Jane B_IDNAMEG_ID 1Bob ? 2Kensington2 3Billy2 GirlfriendsBoyfriends

20 Better Relationship G_IDNAME 1Jessica 2Portia 3Jane B_IDNAME 1Bob 2Kensington 3Billy GirlfriendsBoyfriends G_IDB_ID 22 23 31 11 Relationships Portia and Kensington Portia and Billy Jane and Bob Jessica and Bob Modeling

21 Relationship Types “One to Many” “Many to Many” GirlfriendsBoyfriends GirlfriendsBoyfriends Relationships

22 phpMyAdmin

23 CRUD Create INSERT INTO ( ) VALUES ( ) Read SELECT FROM WHERE Update UPDATE SET = WHERE Delete DELETE FROM WHERE

24 SQL Examples S_IDNameGrade 555567JonathanA 555568AmberC 555572AlexF 555573TimA 555574MikeB Students How do we find the name of all students with an A? How do we delete Alex’s row? How do we edit Amber’s grade from an F to an A? How do we add Alex with an A? SELECT Name FROM Students WHERE Grade = A DELETE FROM Students WHERE S_ID = 555572 UPDATE Students SET Grade = A WHERE S_ID = 555568 INSERT INTO Students (Name, Grade) VALUES (‘Alex’, ‘A’)

25 Index These allow the SQL query to search the tables faster Syntax CREATE INDEX “ ” ON “ ” (column_name) CREATE INDEX “StudentNames” ON “Students” (name)

26 Join Allows us to create relationships on the fly. Simply selecting multiple tables SELECT * FROM WHERE SELECT * FROM Girlfriends, Boyfriends WHERE Girlfriends.G_ID = Boyfriends.G_ID AND Girlfriends.G_ID = 1

27 PHP and MySQL Print titles from all the entries in our blogs table

28 PHP and MySQL Get contents of a blog with a requested title View.php?title=Octopi and Unicorns

29 PHP and MySQL Insert new entry into blogs table

30 PHP and MySQL


Download ppt "Web Design: Basic to Advanced Techniques Fall 2010 Mondays 7-9pm 200 Sutardja-Dai Hall Databases & SQL Lecture Code:"

Similar presentations


Ads by Google