 2012 Pearson Education, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
1 Introduction to Web Application Introduction to Data Base.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Exercise SELECT authorID, lastName FROM authors AuthorID FirstName
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational.
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.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Chapter 1 Overview of Database Concepts Oracle 10g: SQL
1 Chapter 1 Overview of Database Concepts. 2 Chapter Objectives Identify the purpose of a database management system (DBMS) Distinguish a field from a.
Chapter 9 Designing Databases Modern Systems Analysis and Design Sixth Edition Jeffrey A. Hoffer Joey F. George Joseph S. Valacich.
Concepts and Terminology Introduction to Database.
Lecture 2 An Overview of Relational Database IST 318 – DB Admin.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
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.
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
 2006 Pearson Education, Inc. All rights reserved Database, SQL and ADO.NET.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Chapter 1Introduction to Oracle9i: SQL1 Chapter 1 Overview of Database Concepts.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Ch Determine the output displayed from procedures Lab sheet 10.4.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
 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.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
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.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 Finding Your Way Through a Database Exploring Microsoft Office Access.
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.
Fundamentals of DBMS Notes-1.
Database, SQL and ADO.NET
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
Basic select statement
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Accessing Databases with JDBC
LINQ to DATABASE-2.
JDBC.
Databases Intro (from Deitel)
Chapter 4 Relational Databases
Databases and Information Management
The University of Texas – Pan American
Chapter 9 Designing Databases
Structured Query Language
Chapter 8 Working with Databases and MySQL
Databases - בסיסי נתונים
Chapter 22 - SQL, MySQL, DBI and ADO
Databases and Information Management
Introduction To Structured Query Language (SQL)
Manipulating Data Lesson 3.
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

 2012 Pearson Education, Inc. All rights reserved. Chapter 18 Introduction to SQL Part I Based on text [2]  2012 Pearson Education, Inc. All rights reserved. expanded by J. Goetz

It is a capital mistake to theorize before one has data. Arthur Conan Doyle Now go, write it before them in a table, and note it in a book, that it may be for the time to come for ever and ever. Isaiah 30:8 Get your facts first, and then you can distort them as much as you please. Mark Twain I like two kinds of men: domestic and foreign. Mae West

Outline 18.1 Introduction 18.2 Relational Database Model 18.3 Relational Database Overview: Books Database 18.4 Structured Query Language (SQL) 18.4.1 Basic SELECT Query 18.4.2 WHERE Clause 18.4.3 ORDER BY Clause 18.4.4 Merging Data from Multiple Tables: INNER JOIN 18.4.5 Joining Data from Tables Authors, AuthorISBN, Titles and Publishers 18.4.6 INSERT Statement 18.4.7 UPDATE Statement 18.4.8 DELETE Statement

18.1 Introduction Database: Integrated collection of data Database management system (DBMS) Provides mechanisms for storing and organizing data in a way that is consistent with database’s format Allows storage and access to database without knowledge of internal representation Relational Databases most popular Use Structured Query Language SQL to perform queries (search) and manipulate data Programming languages need an interface to interact with relational databases

18.2 Relational Database Model Is logical representation of data that allows relationships among data to be considered without concern for physical structure of data Is composed of tables Rows called records Columns called fields Primary key: field that contains unique data Records normally are unique (by primary key) within a table Each record can be identified by at least one distinct value New sets made from queries called result sets

18.2 Relational Database Model number name department salary location 23603 Jones 413 1100 New Jersey 24568 Kerwin 2100 34589 Larson 642 1800 Los Angeles 35761 Myers 611 1400 Orlando 47132 Neumann 9000 78321 Stephens 8500 record/row field/column primary key Fig. 18.1 Relational-database structure of an Employee table. The records ordered by primary key. department location 413 New Jersey 642 Los Angeles 611 Orlando Fig. 18.2 Result set formed by selecting Department and Location data from the Employee table.

18.2 Relational Database Model Each column represents a different data attribute. Most users of a database use only subsets of the rows and columns. Programs use SQL to define queries that select subsets of the data from a table (Fig. 18.2). Fig. 18.2 | Distinct Department and Location data from the Employees table.

18.3 Relational Database Overview: Books Database Rule of Entity Integrity: every record must have a unique value in its primary-key field Compound Primary key: when a record has a unique key based on a combination of two fields Foreign key: Field for which every entry has a unique value in another table and where the field in the other table is the primary key for that table Rule of Referential Integrity: every foreign-key field value must appear in another table’s primary-key field One to many relationship: A foreign key can appear many times in its own table, but only once as primary key in another table

18.3 Relational Database Overview: Books Database

18.3 Relational Database Overview: Books Database (Cont.) The Titles table (described in Fig. 18.5) consists of four columns. Fig. 18.5 | Titles table of the Books database.

18.3 Relational Database Overview: Books Database (Cont.) Figure 18.6 contains the data from the Titles table. Fig. 18.6 | Data from the Titles table of the Books database.

18.3 Relational Database Overview: Books Database (Cont.) The AuthorISBN table (described in Fig. 18.7) matches the AuthorID and ISBN columns. These foreign keys form a composite primary key. Fig. 18.7 | AuthorISBN table of the Books database.

18.3 Relational Database Overview: Books Database (Cont.) Figure 18.8 contains the data from the Author­ISBN table of the Books database. Fig. 18.8 | Data from the AuthorISBN table of Books.

18.3 Relational Database Overview: Books Database Rule of Entity Integrity: every record must have a unique value in its primary-key field Compound Primary key: when a record has a unique key based on a combination of two fields Foreign key: Field for which every entry has a unique value in another table and where the field in the other table is the primary key for that table Rule of Referential Integrity: every foreign-key field value must appear in another table’s primary-key field One to many relationship: A foreign key can appear many times in its own table, but only once as primary key in another table Foreign keys also allow related data in multiple tables to be joined.

18.3 Relational Database Overview: Books Database (Cont.) Common Programming Error 18.1 Not providing a value for every column in a primary key breaks the Rule of Entity Integrity and causes the DBMS to report an error. Common Programming Error 18.2 Providing the same value for the primary key in multiple rows breaks the Rule of Entity Integrity and causes the DBMS to report an error. Common Programming Error 18.3 Providing a foreign-key value that does not appear as a primary-key value in another table breaks the Rule of Referential Integrity and causes the DBMS to report an error.

18.3 Relational Database Overview: Books Database (Cont.) Figure 18.9 is an entity-relationship (ER) diagram for the Books database. Note that primary keys are italic. On the Authors end of the line, there is a 1, and on the AuthorISBN end, an infinity symbol (∞), indicating a one-to-many relationship. Fig. 18.9 | Entity-relationship diagram for the Books database.

18.3 Books Database Fig. 18.11 Table relationships in Books. Lines represent the link between the foreign key in one table and the primary in another table The fields whose names appears in italic and with a 1 contains that tables’s primary key. The AuthorISBN table contains a compound primary key. publisherID in the Titles table is a foreign key

18.4 Structured Query Language (SQL) SQL - used to request data (perform queries) and manipulate data

Extracts information from one or more tables in a database Format: 18.4.1Basic Select Query Extracts information from one or more tables in a database Format: Basic: SELECT * FROM tableName e.g.: SELECT * FROM Authors * extracts all columns from the tableName table should be retrieved. To get specific fields use a comma separated list instead of *

18.4 Structured Query Language (SQL) To retrieve only specific columns, use a list of the column names: SELECT AuthorID, LastName FROM Authors This query returns the data listed in Fig. 18.11. Fig. 18.11 | AuthorID and LastName data from the Authors table.

Used to specify certain criteria in a query 18.4.2 Where Clause Used to specify certain criteria in a query The basic form of a query with selection criteria is SELECT columnName1, columnName2, FROM tableName WHERE criteria SELECT * FROM tableName WHERE criteria Example: SELECT * FROM Titles WHERE copyright > 1999 Can use LIKE clause Used for pattern matching Uses wildcards *: zero or more characters take its place in Access, Linux ?: exactly one character takes its place in Acceess, Linux

18.4 Structured Query Language (SQL) To select books for which the Copyright is more recent than 2007: SELECT BookTitle, EditionNumber, Copyright. FROM Titles WHERE Copyright > '2007' Figure 18.12 shows the result of the preceding query. Fig. 18.12 | Books with copyright dates after 2007 from table Titles.

18.4.2 WHERE Clause SELECT authorID, firstName, lastName FROM Authors The following query locates authors whose last names start with the letter D: SELECT authorID, firstName, lastName FROM Authors WHERE lastName LIKE ‘D%’ The % indicates that any number of chrs can appear after the letter D SELECT authorID, firstName, lastName FROM Authors WHERE lastName LIKE ‘_i%’

18.4 Structured Query Language (SQL) An underscore (_) indicates a single wildcard character at that position. The following query locates authors whose last names have the letter y as the second letter. SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE '_y%' Fig. 18.14 | The only author from the Authors table whose last name contains y as the second letter.

18.4 Structured Query Language (SQL) 18.4.3 ORDER BY Clause Rows in the result can be sorted into ascending or descending order by using the optional ORDER BY clause. SELECT columnName1, columnName2, FROM tableName ORDER BY column ASC ORDER BY column DESC ASC specifies ascending order, DESC specifies descending order column specifies the column on which the sort is based.

Used to arrange results of a query 18.4.3 ORDER BY Clause Used to arrange results of a query Can be ascending or descending order Uses ASC and DESC respectively Example: SELECT authorID FROM Authors ORDER BY authorID ASC Can be used to sort by multiple fields

18.4 Structured Query Language (SQL) To obtain the list of authors in ascending order by last name (Fig. 18.15), use the query SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName ASC Fig. 18.15 | Authors from table Authors in ascending order by LastName.

18.4 Structured Query Language (SQL) To obtain the same list of authors in descending order by last name (Fig. 18.16), use SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName DESC Fig. 18.16 | Authors from table Authors in descending order by LastName.

18.4 Structured Query Language (SQL) Multiple columns can be used for sorting with an ORDER BY clause: ORDER BY column1 sortingOrder, column2 sortingOrder,… Note that the sortingOrder does not have to be identical for each column. SELECT BookTitle, EditionNumber,Copyright FROM Titles ORDER BY Copyright DESC, BookTitle ASC This query returns books sorted first in descending order by copyright date, then in ascending order by title (Fig. 18.17).

18.4 Structured Query Language (SQL) Fig. 18.17 | Data from Titles in descending order by Copyright and ascending order by BookTitle. SELECT BookTitle, EditionNumber,Copyright FROM Titles ORDER BY Copyright DESC, BookTitle ASC This query returns books sorted first in descending order by copyright date, then in ascending order by title (Fig. 18.17).

18.4 Structured Query Language (SQL) The WHERE and ORDER BY clauses can be combined. ORDER BY must be the last clause in the query. SELECT ISBN, BookTitle, EditionNumber, Copyright FROM Titles WHERE BookTitle LIKE '%How to Program' ORDER BY BookTitle ASC

18.4 Structured Query Language (SQL) 18.4.4 Retrieving Data from Multiple Tables: INNER JOIN Database designers typically normalize databases - i.e., split related data into separate tables to ensure that a database does not store redundant data. For example, we use a table to store “links” between authors and titles. If we did not separate this information into individual tables, we would need to include author information with each entry in the Titles table.

18.4.4 Merging Data from Multiple Tables: INNER JOIN Merges records from multiple tables into a single record Tests for matching values in a common field General Form: SELECT * FROM table1 INNER JOIN table2 ON table1.fieldName=table2.fieldName Example: SELECT firstName, lastName, isbn FROM Authors INNER JOIN AuthorISBN ON Authors.authorID= AuthorISBN.authorID Fully-qualified names use the table name and dot operator followed by the field name

18.4.4 Merging Data from Multiple Tables: INNER JOIN The ON clause specifies the columns from each table that are compared to determine which rows are merged. SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN ON Authors.AuthorID = AuthorISBN.AuthorID ORDER BY LastName, FirstName Fig. 18.19 | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 1 of 2.)

18.4 Structured Query Language (SQL) Fig. 18.19 | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 2 of 2)

18.4.6 Insert Statement Inserts a new record into a table Form: INSERT INTO tableName(fieldName1, fieldName2,…,fieldNameN) VALUES (value1, value2, …,valueN) Ex: INSERT INTO Authors (firstName, lastName) VALUES (‘Sue’, ‘Smith’) Values must match field names in order and type

18.4 Structured Query Language (SQL) INSERT INTO Authors (firstName, lastName) VALUES (‘Sue’, ‘Smith’) AuthorID is an identity column, so it is assigned the next value in an autoincremented sequence. Fig. 18.20 | Table Authors after an INSERT operation.

18.4 Structured Query Language (SQL) Common Programming Error 18.5 It is an error to specify a value for an identity column in an INSERT statement. Common Programming Error 18.6 To specify a string containing a single quote in a SQL statement, there must be two single quotes in the position where the single-quote character appears in the string (e.g., 'O''Malley').

Modifies data in a table Form: UPDATE tableName 18.4.7 UPDATE Statement Modifies data in a table Form: UPDATE tableName SET fieldName1 = value1, fieldName2 = value2, … WHERE criteria Ex: UPDATE Authors SET LastName = 'Jones‘ WHERE LastName = 'Smith' AND FirstName = 'Sue'

Fig. 18.21 | Table Authors after an UPDATE operation. 18.4.7 UPDATE Statement Fig. 18.21 | Table Authors after an UPDATE operation. UPDATE Authors SET LastName = 'Jones‘ WHERE LastName = 'Smith' AND FirstName = 'Sue' Keyword AND is a logical operator that returns true if and only if both of its operands are true. SQL also provides other logical operators, such as OR and NOT.

Fig. 18.22 | Table Authors after a DELETE operation. 18.4.8 DELETE Statement Removes data from a table Form: DELETE FROM tableName WHERE criteria DELETE FROM Authors WHERE LastName = 'Jones' AND FirstName = 'Sue' Fig. 18.22 | Table Authors after a DELETE operation.