Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.

Slides:



Advertisements
Similar presentations
This course has taken from This unique introductory SQL tutorial not only provides easy-to-understand SQL instructions, but it allows.
Advertisements

Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
CSC 2720 Building Web Applications Database and SQL.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Database: SQL and MySQL
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL (DDL & DML Commands)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
DAT602 Database Application Development Lecture 3 Review of SQL Language.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
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.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
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,
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Structured Query Language
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
CHAPTER 10 PHP MySQL Database
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Intro to MySQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
 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.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Restricting and Sorting Data
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
Writing Basic SQL SELECT Statements
 2012 Pearson Education, Inc. All rights reserved.
Open Source Server Side Scripting Permissions & Users
JDBC.
Databases Intro (from Deitel)
CS122 Using Relational Databases and SQL
ISC440: Web Programming 2 Server-side Scripting PHP 3
Introduction To Structured Query Language (SQL)
Restricting and Sorting Data
Introduction To Structured Query Language (SQL)
Restricting and Sorting Data
Presentation transcript:

Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data

Open Source Server Side Scripting 2 ECA 236 sitename  the users table contains the following columns users user_idMEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY first_nameVARCHAR(15) NOT NULL last_nameVARCHAR(30) NOT NULL VARCHAR(40) passwordCHAR(16) NOT NULL registration_dateDATETIME NOT NULL

Open Source Server Side Scripting 3 ECA 236 selecting records  SELECT  the most frequently used of all SQL statements  SELECT returns rows of information based upon a set of selection criteria  the simplest SELECT statement is which returns all the records in the table  the * means ALL SELECT * FROM users;

Open Source Server Side Scripting 4 ECA 236 selecting records cont …  specify which columns to return, each column separated by a comma  advantages of specifying columns  performance  order  functions SELECT user_id, first_name, last_name FROM users;

Open Source Server Side Scripting 5 ECA 236 conditionals  WHERE  used with a variety of operators to retrieve specific records  equality operator  AND operator SELECT FROM users WHERE last_name = ‘Lennon’; SELECT FROM users WHERE last_name = ‘Lennon’ AND first_name = ‘John’;

Open Source Server Side Scripting 6 ECA 236 common operators OperatorExplanation =equals <less than >greater than < =less than or equal to > =greater than or equal to ! =not equal to IS NOT NULLhas a value IS NULLdoes not have a value BETWEENwithin a range NOT BETWEENoutside a range INis included in set of listed values NOT INis not included in set of listed values OR ( also || )where one of two conditionals is TRUE AND ( also && )where both conditionals are TRUE NOT ( also ! )where the condition is not TRUE

Open Source Server Side Scripting 7 ECA 236 WHERE examples  Select the records for every user who was registered during a range of dates  To SELECT a particular day, set the dates greater than midnight on one day, and less than midnight on the following day SELECT * FROM users WHERE ( registration_date > ‘ :00:00’ ) AND ( registration_date < ‘ :00:00’ ); SELECT * FROM users WHERE ( registration_date > ‘ :00:00 ‘) AND ( registration_date < ‘ :00:00’ );

Open Source Server Side Scripting 8 ECA 236 WHERE examples cont …  Since registration_date is a DATETIME data type, we use the time. If it was simply a DATE data type, we could test to see if the value is equal to a specific day SELECT * FROM users WHERE ( registration_date = ‘ ’ );

Open Source Server Side Scripting 9 ECA 236 WHERE examples cont …  Select the first name of all users whose last name is Starr  Select everything from every record in users that does not have an address  NULL means there is no value. If a record has an empty string, it is not NULL SELECT first_name FROM users WHERE last_name = ‘Starr’; SELECT * FROM users WHERE IS NULL; SELECT * FROM users WHERE = ‘ ’;

Open Source Server Side Scripting 10 ECA 236 WHERE examples cont …  Select the record where the password is ‘legsDiamond’ - remember, passwords are case sensitive  Select all the records from users where first_name is in a list of provided values SELECT * FROM users WHERE password = PASSWORD( ‘legsDiamond’ ); SELECT * FROM users WHERE first_name IN ( ‘Leland’, ‘Laura’, ‘Josie’, ‘Bob’ );

Open Source Server Side Scripting 11 ECA 236 strings  using numbers, dates, and NULL in a conditional is fairly straightforward  to check for string equality use the = operator  to check for a close, but not equal match, other operators and symbols are available  EG, to select all last names beginning with “Smith” we need a more flexible operator

Open Source Server Side Scripting 12 ECA 236 LIKE NOT LIKE  used primarily with strings  use two wildcard characters  underscore _  matches any single character  can be used in combination with itself >LIKE ‘_ _ ‘ will match any two letter combination  percent sign  matches zero or more characters  case insensitive

Open Source Server Side Scripting 13 ECA 236 LIKE NOT LIKE cont …  match any user whose last name begins with ‘Smith’  will match ‘Smith’, ‘Smithberger’, ‘Smithsonian’, ‘smithy’, but not ‘Klingensmith’  on the other hand  will match ‘Klingensmith’ SELECT * FROM users WHERE last_name LIKE ‘Smith%’; SELECT * FROM users WHERE last_name LIKE ‘%Smith’;

Open Source Server Side Scripting 14 ECA 236 LIKE NOT LIKE cont …  Select the first and last name of every user whose address is not from aol.com  To use a literal underscore or percent sign in a query, escape them. SELECT first_name, last_name FROM users WHERE NOT LIKE SELECT first_name FROM users WHERE LIKE ‘sam\_%’;

Open Source Server Side Scripting 15 ECA 236 ORDER BY  used to sort query results  if you do not specify ORDER BY, query will most likely be returned by the primary key in ascending order  you can use ORDER BY to sort by any column SELECT last_name FROM users ORDER BY registration_date;

Open Source Server Side Scripting 16 ECA 236 ORDER BY cont …  default order when using ORDER BY is ascending ( designated as ASC )  number increase from small to large  dates go from older to most recent  string go from A to Z  to reverse the order specify DESC SELECT first_name, last_name FROM users ORDER BY last_name DESC;

Open Source Server Side Scripting 17 ECA 236 ORDER BY cont …  you can ORDER BY multiple columns  return the first and last names ordered by registration date, then last name  if a sorted column contains NULL values, these will appear before columns containing values SELECT first_name, last_name FROM users ORDER BY registration_date, last_name;

Open Source Server Side Scripting 18 ECA 236 ORDER BY cont …  you can use ORDER BY along with WHERE to retrieve and order specific records  Return the entire record where any registration date is after November 10, but ORDER BY the user’s last name SELECT * FROM users WHERE registration_date > ‘ :00:00’ ORDER BY last_name;

Open Source Server Side Scripting 19 ECA 236 LIMIT  limits the number of records returned  not part of SQL standard  not available on all databases  LIMIT the number of records returned to 3  return 5 records beginning with the 11 th  begins at index 0 SELECT * FROM users ORDER BY last_name LIMIT 3; SELECT * FROM users ORDER BY last_name LIMIT 10, 5;

Open Source Server Side Scripting 20 ECA 236 LIMIT cont …  Select the third person who was entered into the table  the LIMIT n1, n2 clause is great for returning multiple pages of query results where you show the first 10 results, then the next ten, etc. SELECT first_name, last_name FROM users ORDER BY registration_date LIMIT 2, 1;

Open Source Server Side Scripting 21 ECA 236 DELETE  DELETE eliminates a record – it does not delete a table or database  once you use DELETE to delete a record, there is no way to retrieve  back up your database before running DELETE  use a WHERE statement with DELETE  otherwise you will delete all data in the table

Open Source Server Side Scripting 22 ECA 236 DELETE cont …  will delete everything in the table while retaining its structure  will delete only the specified record  to avoid major errors, log into the mysql monitor with the – – i–am–a–dummy parameter  MySQL will then not allow you to run UPDATE or DELETE without a WHERE clause DELETE FROM users; DELETE FROM users WHERE user_id = 33;

Open Source Server Side Scripting 23 ECA 236 DROP  to drop a table  to drop a database DROP TABLE table_name; DROP DATABASE database_name;