CHAPTER 7 DATABASE ACCESS THROUGH WEB

Slides:



Advertisements
Similar presentations
Virtual training week 4 structured query language (SQL)
Advertisements

Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Microsoft Access 2010 Chapter 7 Using SQL.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
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
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems.
Sundara Ram Matta Apr 01 st, Sundara Ram Matta Apr 01 st, 2015
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL (DDL & DML Commands)
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
DAT602 Database Application Development Lecture 3 Review of SQL Language.
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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
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.
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,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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.
SQL Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL. Structured Query Language ( SQL is a language of database, it includes database creation, deletion, fetching rows and modifying rows etc. ) SQL is.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Web Systems & Technologies
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
Writing Basic SQL SELECT Statements
 2012 Pearson Education, Inc. All rights reserved.
Introduction to Structured Query Language(SQL)
ATS Application Programming: Java Programming
JDBC.
STRUCTURED QUERY LANGUAGE
SQL Tutorial.
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Structured Query Language
SQL Queries Chapter No 3.
Introduction To Structured Query Language (SQL)
Structured Query Language
Manipulating Data Lesson 3.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

CHAPTER 7 DATABASE ACCESS THROUGH WEB PART 2 OF 2 [Structured Query Language] Madam Hazwani binti Rahmat http://fsktm2.uthm.edu.my/hazwani/v2/

WHAT IS SQL? SQL stands for Structured Query Language. It is pronounced as “SQL” or “sequel. SQL is a query language used for accessing and modifying information in the database. Although SQL is an ANSI (American National Standards Institute) standard, there are many different versions of the SQL language. However, to be compliant with the ANSI standard, all SQL versions support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

SQL STATEMENTS SQL is not case sensitive. It is a simple language with a limited number of commands and those commands are very readable and are almost structured like English sentences. Semicolon is the standard way to separate each SQL command in database systems that allow more than one SQL statement to be executed in the same call to the server. Some database systems require a semicolon at the end of each SQL statement. MS Access and SQL Server 2000 and do not require a semicolon after each SQL statement, but some database programs force it.

SQL DML AND DDL SQL can be divided into TWO parts: Data Manipulation Language (DML) DML refers to the query and update commands. It is used to insert, select, update, and delete records in the database. Basic DML commands are: SELECT - Retrieves data from the database INSERT - Inserts new data into the database UPDATE - Updates existing data in the database DELETE - Deletes existing data from the database

SQL DML AND DDL Data Definition Language (DDL) The DDL part of SQL permits database tables to be created or deleted. The most important DDL statements in SQL are: CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index

SQL SELECT STATEMENT The SELECT statement is used to select data from a database. The SELECT statement returned ALL the results from the queried database table. A simple SQL SELECT Statement, requires : Column(s) Name The columns that will be returned in the results. Table Name. The table that will be queried to retrieve the desired results.

SQL SELECT STATEMENT Syntax (To select one / more columns) : SELECT column_name FROM table_name Example : SELECT firstName, age FROM employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 firstName age Alex 28 Billy 40 Craig 35 Original Data : Table employee Query Result

SQL SELECT STATEMENT Syntax (To select ALL columns) : SELECT * FROM table_name Example : SELECT * FROM employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : Table employee Query Result

SQL WHERE CLAUSE The WHERE clause not only used in SELECT statement, but it is also used in UPDATE, DELETE statement etc. The WHERE clause is used to FILTER records that satisfy a given condition. The WHERE clause returned MATCHING results from the queried database table restricted to a specific condition. “condition” is the filter to be applied on the results. The filter could be a range, single value or sub query.

SQL WHERE CLAUSE Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE column_name = ‘criteria’ SQL uses single quotes around text values (most database systems will also accept double quotes). However, numeric values should not be enclosed in quotes.

SQL WHERE CLAUSE Example (for text values): SELECT firstName,lastName FROM employee WHERE lastName = ‘James’ empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 firstName lastName Alex James Billy Original Data : Table employee Query Result

SQL WHERE CLAUSE Example (for numeric values): SELECT firstName,lastName FROM employee WHERE age= 28 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 firstName lastName Alex James Original Data : Table employee Query Result

SQL AND , OR & NOT The WHERE clause can filter records based on more than one condition using operators. With the WHERE clause, the following operators can be used: Operator Description = Equal <> or != Not equal > Greater than < Less than >= Greater than or equal LIKE Search for a pattern

SQL AND , OR & NOT The AND operator can be used to join two or more conditions in the WHERE clause. Both sides of the AND condition must be true in order for the condition to be met and for those rows to be displayed. Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE condition1 AND condition_N both conditions must be TRUE

SQL AND , OR & NOT Example (applicable for one / more columns) : SELECT empID, firstName, age FROM employee WHERE lastName = ‘James’ AND age > 20 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 1 Alex 28 2 Billy 40 Original Data : Table employee Query Result

SQL AND , OR & NOT The OR operator can be used to join TWO or more conditions in the WHERE clause also. However, either or both side of the OR operator can be true and the condition will be met - hence, the rows will be displayed. Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE condition1 OR condition_N either or both conditions can be TRUE

SQL AND , OR & NOT Example (applicable for one / more columns) : SELECT empID, firstName, age FROM employee WHERE firstName = ‘Alex’ OR lastName = ‘Dave’ empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 1 Alex 28 3 Craig 35 Original Data : Table employee Query Result

SQL AND , OR & NOT You can also combine AND and OR (use parenthesis to form complex expressions). Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE condition1 AND (condition_X OR condition_Y )

SQL AND , OR & NOT Example (applicable for one / more columns) : SELECT empID, firstName, age FROM employee WHERE age < 40 AND (firstName = ‘Alex’ OR lastName = ‘James’ ) empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 1 Alex 28 Original Data : Table employee Query Result

SQL AND , OR & NOT To find rows that do not satisfy a condition, the logical operator, NOT can be used. If a condition is satisfied, then the row is not returned. Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE NOT condition

SQL AND , OR & NOT Example (applicable for one / more columns) : SELECT empID, firstName, age FROM employee WHERE NOT lastName = ‘James’ empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 3 Craig 35 Original Data : Table employee Query Result

SQL LIKE The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. The LIKE pattern matching operator can also be used in the conditional selection of the WHERE clause. LIKE is a very powerful operator that allows to select only rows that are "like" what specified. The percent sign "%" can be used as a wildcard to match any possible character that might appear before or after the characters specified.

SQL LIKE Syntax(applicable for one / more columns) : SELECT column_name FROM table_name WHERE column_name LIKE pattern Example(applicable for one / more columns) : SELECT empID, lastName, age FROM employee WHERE lastName LIKE ‘%a%’ empID lastName age 1 James 28 2 40 3 Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Query Result Original Data : Table employee

SQL DISTINCT In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes there is a need to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values. The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate records and fetching only unique records.

SQL DISTINCT Syntax: Example: Query Result SELECT DISTINCT column_name FROM table_name Example: SELECT DISTINCT lastName FROM employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 lastName James Query Result Original Data : Table employee

SQL JOIN The JOIN keyword is used in an SQL statement to query data from TWO or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys.

SQL JOIN Syntax(applicable for one / more columns) : SELECT column_name FROM table_name1 INNER JOIN table_name2 ON table_name1.PK = table_name2.FK The INNER JOIN keyword returns rows when there is at least one match in both tables. If there are rows in table1 that do not have matches in table 2, those rows will NOT be listed.

SQL JOIN Example (applicable for one / more columns) : Query Result SELECT firstname, department.division FROM employee INNER JOIN department ON employee.empID = department.empID firstName division empID Alex Sales 1 Billy Marketing 2 Craig HR 3 Query Result empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 deptID division empID 1 HR 3 2 Sales Marketing Original Data : Table1 : employee Original Data : Table2 : department

SQL ORDER BY The ORDER BY keyword is used to sort the query result by a specified column. Syntax(sort by one column) : SELECT * FROM table_name ORDER BY column_name Example (sort by one column) : SELECT * FROM employee ORDER BY firstName empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Query Result

SQL ORDER BY Syntax(sort by multiple column) : SELECT * FROM table_name ORDER BY column_name1, column_name_n Example (sort by multiple column) : SELECT * FROM employee ORDER BY firstName, lastName empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Query Result

SQL ORDER BY DESC The ORDER BY keyword sorts the records in ascending order by default. Syntax(sort by one column) : SELECT * FROM table_name ORDER BY column_name1 DESC Example (sort by one column) : SELECT * FROM employee ORDER BY age empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee empID firstName lastName age 2 Billy James 40 3 Craig Dave 35 1 Alex 28 Query Result

SQL INSERT The INSERT INTO statement is used to insert a new record in a table. Syntax(all column) : INSERT INTO table_name VALUES (value1, value2, value_n) Syntax(certain column) : INSERT INTO table_name (column1, column2, column_n) When adding a new row, ensure the data type of the value and the column matches and follow the integrity constraints, if any, defined for the table.

SQL INSERT Example(all column) : Original Data : employee Query Result INSERT INTO employee VALUES (4, ‘Matt’, ‘Kent’, 30) empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 Original Data : employee Query Result

SQL INSERT Example(all column) : Original Data : employee Query Result INSERT INTO employee (empID, firstName, lastName) VALUES (5, ‘Henry’, ‘Owen’) empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 5 Henry Owen Original Data : employee Query Result

SQL UPDATE The UPDATE statement is used to update existing records in a table. Syntax(all column) : UPDATE table_name SET column1=value1, column1=value1, column_n=value_n WHERE columnX=valueX The WHERE clause specifies which record or records that should be updated. If it is omitted, all records will be updated.

SQL UPDATE Example(all column) : Query Result Original Data : employee UPDATE employee SET age=29 WHERE empID=5 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 5 Henry Owen empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 5 Henry Owen 29 Query Result Original Data : employee

SQL DELETE The DELETE statement is used to delete records in a table. Syntax(one record) : DELETE FROM table_name WHERE columnX=valueX It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact: Syntax(all records) : DELETE FROM table_name or DELETE * FROM table_name

SQL DELETE Example(all column) : Original Data : employee Query Result DELETE FROM employee WHERE empID=4 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee Query Result