Database Basics CS380 1. Why use a database?  powerful: can search it, filter data, combine data from multiple sources  fast: can search/filter a database.

Slides:



Advertisements
Similar presentations
Chapter 6 Basic forms 1. Forms and query string 2 form : a group of user input (UI) controls that accepts information from the user and sends the information.
Advertisements

PHP and MySQL PHP for the Web, page PHP and MySQL MySQL Resource PHP – MySQL Resource
Objectives Connect to MySQL from PHP
MySQL and PHP By Trevor Adams.
Intermediate PHP & MySQL
Tutorial #8 – Creating Data Tables. Tutorial #6 Review – Layouts Two Column Fixed Width, Three Column Fixed Width Class VS. ID Container Universal Selector.
CIS 1310 – HTML & CSS 8 Tables. CIS 1310 – HTML & CSS Learning Outcomes  Create a Table  Apply Attributes to Format Tables  Increase the Accessibility.
Lecture 16: SQL and HTML tables
Appendix A Database Design CS Database design principles  database design: the act of deciding the schema for a database  database schema: a description.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
INTRODUCTORY Tutorial 7 Creating Tables. XP New Perspectives on Blended HTML, XHTML, and CSS2 Objectives Discern the difference between data tables and.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Principles of Web Design 6 th Edition Chapter 10 – Data Tables.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
PHP meets MySQL.
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
Example simpsons database
JavaScript Professor Robin Burke. 2 Outline Quiz Tables JavaScript.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.
CSS Table Properties.
Creating PHPs to Insert, Update, and Delete Data CS 320.
ADO.NET Data Access. Page  2 SQL  When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
Accessing mySQL relational database. MySQL database.  Today, we will attempt and open a connection to the MySQL server.  We need to specify the database.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
CSE 154 LECTURE 22:RELATIONAL DATABASES AND SQL. Relational databases relational database: A method of structuring data as tables associated to each other.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Introduction to Database Programming with Python Gary Stewart
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Web Systems & Technologies
CSE 154 Lecture 17: HTML tables.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
CS320 Web and Internet Programming SQL and MySQL
JDBC.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Lecture 25: SQL and HTML tables
Lecture 22: Relational Databases and SQL
Lecture 23: Multi-table SQL Queries (Joins)
Tutorial 6 PHP & MySQL Li Xu
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
Lecture 20: Relational Databases and SQL
Lecture 21: Multi-table SQL Queries (Joins)
Presentation transcript:

Database Basics CS380 1

Why use a database?  powerful: can search it, filter data, combine data from multiple sources  fast: can search/filter a database very quickly compared to a file  big: scale well up to very large data sizes  safe: built-in mechanisms for failure recovery (e.g. transactions)  multi-user: concurrency features let many users view/edit data at same time  abstract: provides layer of abstraction between stored data and app(s) 2

Database software  Oracle  Microsoft SQL Server (powerful) and Microsoft Access (simple)  PostgreSQL (powerful/complex free open-source database system)  SQLite (transportable, lightweight free open-source database system)  MySQL (simple free open-source database system)  many servers run "LAMP" (Linux, Apache, MySQL, and PHP)  Wikipedia is run on PHP and MySQL CS380 3

Relational databases  relational database: A method of structuring data as tables associated to each other by shared attributes.  a table row corresponds to a unit of data called a record; a column corresponds to an attribute of that record  relational databases typically use Structured Query Language (SQL) to define, manage, and search data CS380 4

Example simpsons database CS380 5

Example world database CS380 6

Example imdb database 7  other tables:  directors (id, first_name, last_name)  movies_directors (director_id, movie_id)  movies_genres (movie_id, genre)

SQL 8 CS380

SQL basics  Structured Query Language (SQL): a language for searching and updating a database  a standard syntax that is used by all database software (with minor incompatiblities)  generally case-insensitive  a declarative language: describes what data you are seeking, not exactly how to find it CS380 9 SELECT name FROM cities WHERE id = 17; SQL INSERT INTO countries VALUES ('SLD', 'ENG', 'T', 100.0); SQL

Issuing SQL commands directly in MySQL  Use xampp to import databases:  world.sql  simpsons.sql CS SHOW DATABASES; USE database; SHOW TABLES; SQL

The SQL SELECT statement  the SELECT statement searches a database and returns a set of results  the column name(s) written after SELECT filter which parts of the rows are returned  table and column names are case-sensitive  SELECT * FROM table ; keeps all columns CS SELECT column(s) FROM table; SQL SELECT name, code FROM countries; SQL

The DISTINCT modifier  eliminates duplicates from the result set CS SELECT DISTINCT column(s) FROM table; SQL SELECT language FROM languages; SQL SELECT DISTINCT language FROM languages; SQL

The WHERE clause  WHERE clause filters out rows based on their columns' data values  in large databases, it's critical to use a WHERE clause to reduce the result set size  suggestion: when trying to write a query, think of the FROM part first, then the WHERE part, and lastly the SELECT part CS SELECT column(s) FROM table WHERE condition(s); SQL SELECT name, population FROM cities WHERE country_code = "FSM"; SQL

More about the WHERE clause  the WHERE portion of a SELECT statement can use the following operators:  =, >, >=, <, <=  <> : not equal  BETWEEN min AND max  LIKE pattern  IN (value, value,..., value) CS WHERE column operator value(s) SQL SELECT name, gnp FROM countries WHERE gnp > ; SQL

Multiple WHERE clauses: AND, OR  multiple WHERE conditions can be combined using AND and OR CS SELECT * FROM cities WHERE code = 'USA' AND population >= ; SQL

Approximate matches: LIKE  LIKE 'text%' searches for text that starts with a given prefix  LIKE '%text' searches for text that ends with a given suffix  LIKE '%text%' searches for text that contains a given substring CS WHERE column LIKE pattern SQL SELECT code, name, population FROM countries WHERE name LIKE 'United%'; SQL

Sorting by a column: ORDER BY  can write ASC or DESC to sort in ascending (default) or descending order: CS ORDER BY column(s) SQL SELECT code, name, population FROM countries WHERE name LIKE 'United%' ORDER BY population; SQL SELECT * FROM countries ORDER BY population DESC; SQL  can specify multiple orderings in decreasing order of significance: SELECT * FROM countries ORDER BY population DESC, gnp; SQL

Limiting rows: LIMIT  also useful as a sanity check to make sure your query doesn't return 10 7 rows CS LIMIT number SQL SELECT name FROM cities WHERE name LIKE 'K%' LIMIT 5; SQL

Databases and PHP 19 CS380

PHP MySQL functions CS380 20

Complete PHP MySQL example CS # connect to world database on local computer $db = mysql_connect("localhost", "traveler", "packmybags"); mysql_select_db("world"); # execute a SQL query on the database $results = mysql_query("SELECT * FROM countries WHERE population > ;"); # loop through each country while ($row = mysql_fetch_array($results)) { ?>, ruled by <?php } ?> PHP

Connecting to MySQL: mysql_connect  mysql_connect opens connection to database on its server  any/all of the 3 parameters can be omitted (default: localhost, anonymous)  mysql_select_db sets which database to examine CS mysql_connect("host", "username", "password"); mysql_select_db("database name"); PHP # connect to world database on local computer mysql_connect("localhost", "traveler", "packmybags"); mysql_select_db("world"); PHP

Performing queries: mysql_query  mysql_query sends a SQL query to the database  returns a special result-set object that you don't interact with directly, but instead pass to later functions  SQL queries are in " ", end with ;, and nested quotes can be ' or \" CS mysql_connect("host", "username", "password"); mysql_select_db("database name"); $results = mysql_query("SQL query");... PHP $results = mysql_query("SELECT * FROM cities WHERE code = 'USA' AND population >= ;"); PHP

Result rows: mysql_fetch_array  mysql_fetch_array returns one result row as an associative array  the column names are its keys, and each column's values are its values  example: $row["population"] gives the population from that row of the results CS mysql_connect("host", "username", "password"); mysql_select_db("database name"); $results = mysql_query("SQL query"); while ($row = mysql_fetch_array($results)) { do something with $row; } PHP

Result rows: mysql_fetch_array  mysql_fetch_array returns one result row as an associative array  the column names are its keys, and each column's values are its values  example: $row["population"] gives the population from that row of the results CS if (!mysql_connect("localhost", "traveler", "packmybags")) { die("SQL error occurred on connect: ". mysql_error()); } if (!mysql_select_db("world")) { die("SQL error occurred selecting DB: ". mysql_error()); } $query = "SELECT * FROM countries WHERE population > ;"; $results = mysql_query($query); if (!$results) { die("SQL query failed:\n$query\n". mysql_error()); } PHP

Complete example w/ error checking  mysql_fetch_array returns one result row as an associative array  the column names are its keys, and each column's values are its values  example: $row["population"] gives the population from that row of the results CS # connect to world database on local computer check(mysql_connect("localhost", "traveler", "packmybags"), "connect"); check(mysql_select_db("world"), "selecting db"); # execute a SQL query on the database $query = "SELECT * FROM countries WHERE population > ;"; $results = mysql_query($query); check($results, "query of $query"); # loop through each country while ($row = mysql_fetch_array($results)) { ?>, ruled by <?php } # makes sure result is not false/null; else prints error function check($result, $message) { if (!$result) { die("SQL error during $message: ". mysql_error()); } }?> PHP

Other MySQL PHP functions CS380 27

HTML tables:,,  table defines the overall table, tr each row, and td each cell's data  tables are useful for displaying large row/column data sets  NOTE: tables are sometimes used by novices for web page layout, but this is not proper semantic HTML and should be avoided CS ,1 1,2 okay 2,1 real wide 2,2 HTML

Table headers, captions:,  th cells in a row are considered headers; by default, they appear bold  a caption at the start of the table labels its meaning CS My important data Column 1 Column 2 1,1 1,2 okay 2,1 real wide 2,2 HTML

Styling tables  all standard CSS styles can be applied to a table, row, or cell  table specific CSS properties:  border-collapse, border-spacing, caption-side, empty-cells, table-layout CS <table { border: 2px solid black; caption-side: bottom; } tr { font-style: italic; } td { background-color: yellow; text-align: center; width: 30%; } CSS

The border-collapse property  by default, the overall table has a separate border from each cell inside  the border-collapse property merges these borders into one CS table, td, th { border: 2px solid black; } table { border-collapse: collapse; } CSS

Table headers, captions:,  colspan makes a cell occupy multiple columns; rowspan multiple rows  text-align and vertical-align control where the text appears within a cell CS Column 1 Column 2 Column 3 1,1-1,2 1,3-3,3 2,1 2,2 3,1 3,2 HTML

Table headers, captions:,  col tag can be used to define styles that apply to an entire column (self-closing)  colgroup tag applies a style to a group of columns (NOT self-closing) CS Column 1 Column 2 Column 3 1,1 1,2 1,3 2,1 2,2 2,3 HTML

Don't use tables for layout!  (borderless) tables appear to be an easy way to achieve grid-like page layouts  many "newbie" web pages do this  but, a table has semantics; it should be used only to represent an actual table of data  instead of tables, use divs, widths/margins, floats, etc. to perform layout CS380 34