Announcements PS 3 is out (see the usual place on the course web) Be sure to read my notes carefully Also read SQL tutorial: http://www.w3schools.com/sql/default.asp Take a break around 10:15am 1
Databases 2
Multi-tiered Web app architecture Front-end / Middle Tier Data / Resource Client Tier Web Tier Application Tier Tier HTTP... Web Browser (Chrome) HTTP Server (Apache) Web Application (PHP, Java) Database Server (MySQL, Oracle) 3
Example: Databases today 4
What is a database? A database: A very large, integrated collection of persistent data (records) A database contains one or more tables Each table contains records (or rows) Each row contains fields (or columns) that contain data itself Typically models a real-world enterprise Entities (e.g., students, courses) Relationships (e.g., Amy is taking CS 135) A web app dealing with dynamic content needs a database on the backend! Example: Google employee database 5
What is a DBMS? A Database Management System (DBMS) is a software system designed to store, manage, and facilitate access to databases Reliable storage and recovery of 100s of GB Querying/Updating interface and API for applications and Web pages Support for many concurrent users Typically this term DBMS is used narrowly: Relational DBMS with transactions Examples Oracle, DB2, SQL Server, MySQL, SQLite, etc. 6
Describing data: data models A data model is a collection of concepts for describing data Mathematical representation of data Examples: relational model = tables semi-structured model = trees/graphs A schema is a description of a particular collection of data, using a given model Relational model is the most widely used today Main concept: relation, a table with rows and columns Every relation has a schema, which describes the columns (fields). This is really the meta data part of a database. 7
Example: college database Schema: Students(sid text, name text, gpa real) Courses(cid text, cname text, credits integer) Enrolled(sid text, cid text, grade text) 8
Conceptual design: ER diagram fid PROFESSOR name Teaches STUDENT Takes COURSE sid name cid name semester 9
Designing a schema (set of relations) Convert the ER diagram to tables STUDENT Takes COURSE sid name sid cid cid name sem 1 Amy 1 10-03 10-03 DB S15 2 Bob 1 12-10 12-10 PL F14 3 Mary 3 13-03 13-03 OS F13 PROFESSOR fid name 1 Jim 2 Art 8 Mike Teaches fid cid 1 10-03 2 12-10 8 13-03 10
Applications use queries in SQL Structured Query Language (SQL) <html> <body> <!-- someembeddedsql: SELECT name, major, gpa FROM STUDENT, Takes, COURSE WHERE STUDENT.sid = Takes.sid AND Takes.cid = COURSE.cid --> </body> </html> 11
MySQL 12
MySQL Probably the most popular DBMS for web servers (well over 10 million installations) Powerful Fast Not a resource hog Scalable For the latest benchmarks, see http://mysql.com/why-mysql/benchmarks Free 13
Accessing MySQL via the command line Three main ways you can interact with MySQL 1. Using a command line 2. Via a web interface such as phpmyadmin 3. Through a programming language like PHP, Java, etc. We will see first two today 14
Command line interface (Windows) Assuming that you have installed XAMPP... You will be able to access the MySQL executable from the directory: C:\xampp\mysql\bin By default, the initial MySQL user is root and will not have a password set. Using it as a development server that only you should be able to access, we won t worry about creating one. From a command window prompt (Start -> Run; CMD) enter: C:\xampp\mysql\bin\mysql u root You have just logged into MySQL as user root, without a password Now, you are ready to type in MySQL commands, e.g., SHOW databases; 15
Command line interface (OSX) Assuming that XAMPP is installed; the web server is running; and the MySQL server has started... You will be able to access the MySQL executable from the MySQL installation directory: /Applications/XAMPP/bin By default, the initial MySQL user is root and it will have a password of root too. So, to start a MySQL client, try the following on a Terminal /Applications/XAMPP/bin/mysql u root You have just logged into MySQL as user root, and not to request your password (Consider using an alias if you don t want to type in this long command every time) Now, you are ready to type in MySQL commands, e.g., SHOW databases; 16
Running a MySQL script file Once a MySQL client has started, from the command prompt try the following command with a script file named mydb.sql mysql> source mydb.sql See mydb.sql 17
Common MySQL commands 18
Accessing MySQL via phpmyadmin Although to use MySQL you have to learn these main commands and how they work, once you understand them, it can be much quicker and simpler to use a program such as phpmyadmin to manage your database and tables To do this Go to http://localhost/xampp Click the phpmyadmin link toward the bottom of the lefthand menu to open up the program In the lefthand pane of the main phpmyadmin screen, you can select a database you wish to work with From here you can perform all the main operations such as creating a new database, adding tables, creating indexes, etc. Visit https://docs.phpmyadmin.net to see the supporting documentation for phpmyadmin 19
Next... More database stuff SQL in PHP 20