Structured Query Language

Slides:



Advertisements
Similar presentations
Chapter 7 Working with Databases and MySQL
Advertisements

What is a Database By: Cristian Dubon.
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
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.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
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 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Database revision.
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.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 16 Using Relational Databases.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: Relational Databases
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  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.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Fundamentals of DBMS Notes-1.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Database Access with SQL
Databases.
Chapter 5 Introduction to SQL.
 2012 Pearson Education, Inc. All rights reserved.
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
JDBC.
Chapter 4 Relational Databases
Database Management  .
Building and Using Queries
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
Chapter 22 - SQL, MySQL, DBI and ADO
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Chapter 7 Introduction to Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
DATABASE Purpose of database
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Structured Query Language SELECT WHERE INSERT DELETE Structured Query Language MySQL~PHP Anita Philipp – Spring 2010

Objectives Study the basics of databases and MySQL Work with MySQL databases Define database tables Modify user privileges Work with database records Work with phpMyAdmin

Introduction to Databases Ordered collection of information Can quickly access information Each row in a database table is called a record Record Single complete set of related information - Row Fields Columns in a database table Individual categories of information stored in a record

Introduction to Databases Flat-File Database Stores information in a single table Relational Database Stores information across multiple related tables

Understanding Relational Databases Primary Table main table in a relationship that is referenced by another table Related Table (or “child table”) references a primary table in a relational database Primary Key field that contains a unique identifier for each record in a primary table Foreign Key field in a related table that refers to primary key in primary table link records across multiple tables

One-to-One Relationships Related table - one record for each record in the primary table Breaks information into multiple, logical sets Could be placed within a single table Make one table confidential and accessible only by certain individuals

One-to-Many Relationship one record in a primary table has many related records in a related table Normalization multiple related tables to reduce redundant and duplicate information

One-to-Many Relationship Figure 7-3 Table with Redundant Information Figure 7-4 One-to-Many Relationship

Many-to-Many Relationship Junction Table Sometimes called “cross-reference” table Creates a one-to-many relationship for each of the two tables in a many-to-many relationship Compound key: employee_id and language_id Number of years each programmer has worked with each language Figure 7-5 Many-to-Many Relationship

Working with Database Management Systems DataBase Management System (DBMS) Application or collection of applications used to access and manage a database Schema Structure of a database including its tables, fields, and relationships Flat-File Database Management System Stores data in a flat-file format (text file, one record per line) Sometimes refers to a one table database Relational DataBase Management System (RDBMS) Stores data in a relational format

Working with DBMS Query Structured set of instructions and criteria for retrieving, adding, modifying, and deleting Structured Query Language (SQL) Data manipulation language Open DataBase Connectivity (ODBC) Allows ODBC-compliant applications to access any data source for which there is an ODBC driver

SQL-Structured Query Language Most widely used language for defining and manipulating relational databases 1. Create new databases 2. Add/modify tables 3. Search for Data 4. Build new tables from existing ones 5. Remove tables and data

SQL Language Data Definition Language (DDL) …defining databases, tables, indexes Data Manipulation Language (DML) …searching, retrieving, modifying, inserting, deleting, updating table data

Data Manipulation Language SQL Data Manipulation Language Parameter Declarations …optional parameters that can be passed to the query Manipulative Statement …Indicates the type of action to be taken (INSERT, DELETE, SELECT, or UPDATE) Options …Conditions for selecting record, grouping, ordering, totaling of records (FROM, WHERE, GROUP BY, HAVING, ORDER BY Aggregate Functions …process multiple record maybe (subset of entire table) (AVG, COUNT, SUM, MAX, MIN)

Two Types of Statements SQL Two Types of Statements Selection Query Retrieves one or more rows Action Query Actions such as update, delete Does not retrieve any rows

Selection Queries

Selection Query - Format

Retrieving Data – SELECT Statement SELECT Field FROM TableName Display the price SELECT fldPrice FROM tblCars; Display year and model SELECT fldYear, fldModel FROM tblCars; Display all car information SELECT * FROM tblCars; *Fields are retrieved in the order they are in the SELECT Statement tblCars fldMake fldModel fldYear fldColor fldPrice

Sorting Data – ODER BY Statement SELECT Field FROM TableName ORDER BY Field [ASC | DESC] Display the Model and Price … least expensive to most expensive SELECT fldModel, fldPrice FROM tblCars ORDER BY fldPrice; Display the Price and Model* … most expensive to least expensive SELECT fldPrice, fldModel FROM tblCars ORDER BY fldPrice DESC; Display all information sorting by Make in ascending order and Price in descending order SELECT * FROM tblCars ORDER BY fldMake, fldPrice DESC; *Fields are retrieved in the order they are in the SELECT Statement tblCars fldMake fldModel fldYear fldColor fldPrice

Restricting Data – WHERE Statement SELECT Field FROM TableName WHERE Criteria < <= > >= = <> IN BETWEEN LIKE NOT WHERE fldPrice > 2000 WHERE fldPrice BETWEEN 5000 AND 10000 (Includes 5000 and 10000) WHERE fldYear = 2010 (If type is Year ~ no quotes needed) WHERE fldDates> ‘2010-04-15’ (If type is Date ~ enclose in single quotes) WHERE fldModel LIKE ‘E%’ (String enclosed in single quotes LIKE is used for pattern matching)

Restricting Data – WHERE Statement LIKE - Pattern-matching Operator % Matches multiple characters _ Matches a single character WHERE fldModel LIKE ‘H%’; …Honda, Hyundai WHERE fldMake LIKE ‘H_ _ _ _’ ; …Honda WHERE fldMake = ‘O\’Hara’; If apostrophe is in the name (O’Hara), a backslash is required

Restricting Data – WHERE Statement SELECT Field FROM TableName WHERE Criteria Display the Model and Price … cars over 10000 SELECT fldModel, fldPrice FROM tblCars WHERE fldPrice > 10000; Display the Year and Model… cars made in 2010 SELECT fldYear, fldModel FROM tblCars WHERE fldYear = 2010; Display all information for cars whose Model begins with S SELECT * FROM tblCars WHERE fldModel LIKE ‘S%’; tblCars fldMake fldModel fldYear fldColor fldPrice

Restricting Data - DISTINCT Shows only unique fields, avoids redundancy SELECT DISTINCT Field FROM TableName List the makes of cars, do not include name more than once SELECT DISTINCT fldMake FROM tblCars; RESULTS: With DISTINCT: Honda, Hyundai Without DISTINCT: Honda, Hyundai, Honda tblCars fldMake fldModel fldYear fldColor fldPrice

Restricting Data - LIMIT Limits the number of records returned SELECT Field FROM TableName LIMIT StartValue, NumRows; SELECT fldMake FROM tblCars LIMIT 0, 2; RESULTS: Start with the first record and display 2 records tblCars fldMake fldModel fldYear fldColor fldPrice

Action Queries

Action Queries Insert, Change, Delete Records Does not retrieve a row Performs operations on data Returns a value indicating that operation was successfully completed

Action Query -INSERT Insert 2007, Red, and 10000 INSERT INTO Table(Fld1, Fld2, Fld3) VALUES (Val1, Val2, Val3) Insert 2007, Red, and 10000 INSERT INTO tblCars (fldYear, fldColor, fldPrice) VALUES (2007, ‘Red’, 10000) *If the field was defined as date type as opposed to year type, the format would be ‘2007-04-15’. tblCars fldYear fldColor fldPrice

Action Query - DELETE DELETE FROM Table WHERE Criteria; DELETE * FROM Table WHERE Criteria; Delete all cars made prior to 2010 DELETE FROM tblCars WHERE fldYear < 2010 Delete all black cars DELETE FROM tblCars WHERE fldColor = ‘Black’; tblCars fldYear fldColor fldPrice

Action Query- UPDATE UPDATE Table SET fld1= NewValue, fld2=NewValue, etc. WHERE Criteria; Increase the cost of all cars by 10% UPDATE tblCars SET fldPrice = fldPrice * 1.10; Change the color of all red cars to maroon UPDATE tblCars SET fldColor = ‘maroon’ WHERE fldcolor = ‘red’ ; tblCars fldMake fldModel fldYear fldColor fldPrice

Variables in Queries $carMake = $_POST[“car_make"]; Display Make $SQLQuery = “SELECT fldMake FROM tblCars WHERE fldMake= ‘$carMake’ ”; FROM tblCars WHERE fldMake= ‘$_POST[“car_make"]’ ”; Insert a new record into the table $SQLQuery = “INSERT INTO tblCars (fldMake, fldModel, fldYear, fldColor, fldPrice) VALUES (‘$carMake’, ‘$carModel’, ‘$carYear’, ‘$carColor’, ‘$carPrice’)”; tblCars fldMake fldModel fldYear fldColor fldPrice

Primary Keys: Auto Increment If the Primary key is an auto-incremented field, use NULL for the insert value $SQLQuery = “INSERT INTO tblCars (fldID, fldMake, fldModel, fldYear, fldColor, fldPrice) VALUES (NULL,‘$carMake’, ‘$carModel’, ‘$carYear’, ‘$carColor’, ‘$carPrice’)”; tblCars fldID fldMake fldModel fldYear fldColor fldPrice

The End