Lecture 9 – MYSQL and PHP (Part1) SFDV3011 – Advanced Web Development 1.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

A comparison of MySQL And Oracle Jeremy Haubrich.
Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
MySQL-Database Teppo Räisänen Oulu University of Applied Sciences School of Business and Information Management.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
Nichelle K. Norris IS 373: World Wide Web Standards.
Copyright © Curt Hill SQL The Data Definition Language.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Database Lecture # 1 By Ubaid Ullah.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
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.
ASP.NET Programming with C# and SQL Server First Edition
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Concepts of Database Management Seventh Edition
MySQL. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn The main subsystems in MySQL architecture The different storage.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
LIS651 lecture 6 mySQL Thomas Krichel
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
DBMS Implementation Chapter 6.4 V3.0 Napier University Dr Gordon Russell.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
MySQL More… 1. More on SQL In MySQL, the Information Schema is the “Catalog” in the SQL standard SQL has three components: Data definition Data manipulation.
What’s a database? Data stored in a structured format that lends itself to easy manipulation and recall.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
CMPT 258 Database Systems The Relationship Model (Chapter 3)
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
CHAPTER 10 PHP MySQL Database
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
Relational Databases and MySQL Charles Severance
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.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
SQL Basics Review Reviewing what we’ve learned so far…….
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Introduction to Database Programming with Python Gary Stewart
Lecture 1.21 SQL Introduction Steven Jones, Genome Sciences Centre.
3 A Guide to MySQL.
CS 3630 Database Design and Implementation
CS320 Web and Internet Programming SQL and MySQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Data Definition and Data Types
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
ISC440: Web Programming 2 Server-side Scripting PHP 3
DATABASE MANAGEMENT SYSTEM
CS3220 Web and Internet Programming SQL and MySQL
Web Programming– UFCFB Lecture
MySQL Database System Installation Overview SQL summary
CS3220 Web and Internet Programming SQL and MySQL
Presentation transcript:

Lecture 9 – MYSQL and PHP (Part1) SFDV3011 – Advanced Web Development 1

2 SQL  SQL (Structured Query Language) is the language most commonly used to define, manipulate, and query RDBMSs  It’s origins trace back to SEQUEL designed for IBM’s “System R” in 1970s  Currently there is an ANSI (American National Standards Institute) standard for SQL  The standard does not adhere in many respects to the relational model described by Codd (e.g. it explicitly breaks several important relational concepts - particularly by allowing duplicate rows, and columns with the same names)

3 RDBMSs based on SQL  There are a number of choices for an RDBMS based on SQL: Oracle - developed by Oracle Corporation (whose origins date back to 70s, when Ellis spotted potential in concept of relational theory and IBM’s SEQUEL) Microsoft SQL Server MySQL - extremely popular, open source RDBMS PostgreSQL, SQLite,...and many others  Each of these RDBMSs uses it’s own implementation of SQL. In this course we will cover MySQL.

4 MYSQL  Open source RDBMS  Popular, because it’s free, lightweight (not all the features of Oracle) and relatively fast  Named after the daughter (My) of Michael Widenius, who was the main author of the first version  Used by Wikipedia, Facebook and many other websites  Managed and developed by MySQL AB until 2008 when it was acquired by SUN Microsystems, which in turn was bought by Oracle Corporation in 2010.

5 Example of database OrganisationIDNameAddressNotes 1ACME IncNew YorkVery friendly crew 2Abcde.comBathgateNo comment VisitIDVisitorIDOrganisationIDDate 11221/03/ /07/ /02/06 VisitorIDName Affiliation 1John University 2Jim University 3John University Visitors Organisation Visits

6 Numeric data types BIT[(n)]1 to 2 n TINYINT-128 to 127 SMALLINT to INT to FLOAT-3.4E+38 to -1.18e-38, 0, and 1.18E-38 to 3.4E+38 BOOL, BOOLEAN – 0 = FALSE – other non null values = TRUE

7 String data types CHAR(n), 0< n <255 – [BINARY | ASCII | UNICODE] TINYBLOBup to 255 bytes TINYTEXTup to 255 characters BLOBup to bytes TEXTup to characters ENUM('value1', 'value2',...) up to values

8 Date and Time data types DATE' ' to ' ' TIME'-838:59:59' to '838:59:59' DATETIME' :00:00' to ' :59:59' TIMESTAMP used to record update or insert times YEAR [(2 | 4)]1901 to 2155,

9 Log in mysql Type command: mysql -h hostname -u username -p Where: hostname = localhost default username = root Once logged in, the following mysql prompt appears to type mysql commands: mysql>

10 Create a database  Type the command: create database dbname;  View tables created (at first there won't be any, because you haven't made any yet!) : show tables;

11 Create a table mysql> create table tablename( column1 type, column2 type,... columnn type); mysql> create table visitors( visitorID int unsigned not null auto_increment primary key, name char(50) not null, char(50), Affiliation char(200)); mysql> create table organisations( organisationID int unsigned not null auto_increment primary key, name char(50) not null, address char(200), notes text); mysql> create table visits( visitID int unsigned not null auto_increment primary key, visitorID int unsigned not null, organisationID int unsigned not null, date date);

12 Looking at the database View all tables: – show tables; Describe a table: – describe tablename; List table content: – Query the database …

13 Inserting data mysql> insert into tablename [(col1, col2,...)] values (val1, val2,...); mysql> insert into visitors (name, ) values ('John Doe', ('Jim Doe', ('Jon Doe', mysql> insert into organisations (name, address, notes) values ('ACME', 'New York', NULL), ('abcde.com', ‘Bathgate’, 'No comment!'); mysql> insert into visits values (NULL, 1, 2, ' '), (NULL, 2, 2, ' '), (NULL, 1, 2, ' ');

14 Retrieving data SELECT [options] items [INTO file_details] FROM tables [WHERE conditions] [GROUP BY group_type] [HAVING where_definitions] [ORDER BY order_type] [LIMIT limit_criteria] [PROCEDURE procedure_name(args)] [lock_options];

15 Retrieving data Select whole table: – select * from table_name; Select one column: – select col_name from table_name; Select two columns: – select col1, col2 from table_name; Selection with criterion: – select * from table_name where criterion; – Note: criterion is a boolean.

16 Selection from multiple tables (1) Selection condition(s) may involve more than one table Example: “Show the dates of all visits made by Jim Doe.” select visits.date from visits, visitors where visitors.name = 'Jim Doe' and visitors.visitorID = visits.visitorID;

17 Selection from multiple tables (2) Selection from more than two tables: – Follow relations between tables Example: “Show the dates of all visits made by Jim Doe to Acme Inc.” You do it, using SELECT, FROM, WHERE, AND Answer: select visits.date from visits, visitors, organisations where visitors.name = 'Jim Doe' and visitors.visitorID = visits.visitorID and organisation.name = 'Acme Inc.' and organisation.organisationID = visits.organisationID;

18 Updating records UPDATE [options] tablename SET col1=expr1, col2=expr2 … [WHERE conditions] [ORDER BY order_type] [LIMIT number] Example - Update ACME’s address: update organisations set address = 'Boston' where organisationID = 1;

19 Deleting records DELETE [options] FROM tablename [WHERE conditions] [ORDER BY order_type] [LIMIT number] Example - Delete ACME from organisation table: delete from organisations where organisationID = 1;

20 Deleting tables DROP [options] TABLE tablename; Example – Delete organisations: drop table organisations;

21 Tips  SQL commands can be stored in a text file.  Commands stored in the text file can be executed using source Example: mysql> source filename.sql  In command windows (DOS/UNIX), use the up arrow to navigate to previously typed commands.

22 MYSQL storage engines  MySQL supports multiple storage engines  Each table can be configured to use a different storage engine – you can mix storage engines in one database  When you create a new table, you can specify which storage engine to use as follows: create table visits( visitID int unsigned not null auto_increment primary key, visitorID int unsigned not null, organisationID int unsigned not null, date date) ENGINE=’MyISAM ’;  It is possible to migrate a table to a different storage engine (this is not always a trivial operation, since some features might not be supported in both engines) ALTER TABLE visits ENGINE=’InnoDB ’;  To find out which storage engine a table uses: SHOW CREATE TABLE visits \G ;

23 MYSQL storage engines  InnoDB (default for MySQL 5.5 and later) - transaction safe (ACID compliant), data stored in clustered indexes, foreign-key support  MyISAM (default prior to MySQL 5.5) - no-transaction, full-text search index  Archive - supports only INSERT and SELECT queries; un-indexed tables for storing data that is not accessed too often  Memory - all data stored in RAM (does not survive reboot), very fast, good for intermediate results...and more

24 Schema optimization  Choosing data types  The smaller the data type (in terms of bytes it uses to represent information) the faster its manipulation  Simpler data types are easier to handle (e.g. storing date and time as integers, not strings)  Define fields as NOT NULL whenever you can - possibility of variable being null makes queries and indexing more complicated  Using VARCHAR instead of CHAR trades storage space for speed  It is best to use INTEGER type for primary keys

25 Indexing  An index is a map of your data that orders it and allows for quick search (like index at the end of a book with references to pages where a particular term can be found)  Indexes help a database retrieve data efficiently  For small databases indexing can be neglected, but as the size of the database grows, indexing becomes critical  Indexes slow down inserts and updates but speed up WHERE clauses and ORDER BY.  Failure to properly index data is the leading cause of SQL performance problems

26 Query performance  Poorly designed queries may unnecessarily retrieve more data than needed (not only too many rows but also too many columns) that is eventually thrown away before returning the final result  Do you need so many rows? SELECT * FROM organisation, visits WHERE organisation.OrganisationID = visits.OrganisationID LIMIT 1;  Do you need all the columns?  The EXPLAIN statement can be used as a way to obtain information about how MySQL executes a SELECT statement EXPLAIN SELECT * FROM organisation, visits WHERE organisation.OrganisationID = visits.OrganisationID;