SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming.

Slides:



Advertisements
Similar presentations
Relational Database Systems Higher Information Systems Advanced Implementation in MySQL/PHP.
Advertisements

Organisation Of Data (1) Database Theory
Introduction to Structured Query Language (SQL)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Introduction to Structured Query Language (SQL)
Intro to ADO.NET1 CTEC2902 Advanced Programming Introduction to ActiveX Data Objects.NET (.NET Class Library for Database Processing)
Relational Databases What is a relational database? What would we use one for? What do they look like? How can we describe them? How can you create one?
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Databases. Objectives Define what a database is. Understand the difference between a flat and relational database Design and create a relational database.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Chapter 10 - VB.Net by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL.
2.3 Organising Data for Effective Retrieval
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.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
School library systems 3.2 Education. Libraries often contain many thousands of books, magazines, CD- ROMs, etc. In fact, some of the largest libraries.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
RDBMSSection Relational DBMS DATABASE DEVELOPMENT And Franchise Colleges By MANSHA NAWAZ.
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.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
ADO.NET Data Access. Page  2 SQL  When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL by Example By convention SQL keywords are written in uppercase. SELECT * FROM Books –This query returns all rows in the Books table. –SQL statements.
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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Nikolay Kostov Telerik Corporation
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
Advanced SQL: Triggers & Assertions
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
Structured Query Language
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
 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.
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Sample Table Standard Notation Entity name in uppercase
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
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.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
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.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
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.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Year 12 > 13 Applied GCE ICT Unit 7 Using Database Software.
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.
Chapter 5 Introduction to SQL.
 2012 Pearson Education, Inc. All rights reserved.
ATS Application Programming: Java Programming
SQL Tutorial.
Chapter 22 - SQL, MySQL, DBI and ADO
Introduction To Structured Query Language (SQL)
Introduction To Structured Query Language (SQL)
Advanced Database Concepts: Reports & Views
Section 4 - Sorting/Functions
The University of Akron College of Applied Science & Technology Dept
Restricting and Sorting Data
Presentation transcript:

SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming

SQL Exercises2 The story so far... You know How to use existing classes (via their API) You are now ready to tackle ADO.NET, but first... Let’s remember RDB and SQL CTEC2902 Advanced Programming

SQL Exercises3 Use this database to answer the following questions Table definitions Books (CopyID, Title, Author, ISBN, PublisherID, Cost) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) e.g. A Simple Library database Books Publishers Borrowers Loans RDB consists of 1 or more tables A table consist of 0 or more records A record consist of 1 or more fields Tables are linked using primary and foreign keys Table name is in bold Primary keys are underlined

SQL Exercises4 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Extract the name, address, and phone number of all the publishers in the database SELECT Name, Address, Phone FROM Publishers Remember: SELECT always returns a (temporary) table How many columns does the above table have? How many rows?

SQL Exercises5 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Extract names and addresses of all borrowers on BSc Computing course SELECT Name, Address FROM Borrowers WHERE Course = ‘BSc Computing’ Only those records that satisfy the condition are selected How many columns (or fields) does the above table have? How many rows (or records)?

SQL Exercises6 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Names of all BIS students with books out SELECT Name FROM Borrowers, Loans WHERE Course = ‘BIS’ AND Loans.BorrowerID = Borrowers.BorrowerID Yes, this SQL will produce duplicate records if a student has borrowed several books Is there a problem with the table that this SQL returns?

SQL Exercises7 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Names of all BIS students with books out SELECT DISTINCT Name FROM Borrowers, Loans WHERE Course = ‘BIS’ AND Loans.BorrowerID = Borrowers.BorrowerID The keyword DISTINCT will prevent duplicate records occurring in the resulting data table

SQL Exercises8 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All details of all borrowers registered in the library SELECT * FROM Borrowers The * means “all the fields”

SQL Exercises9 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All names of all authors, whose names begin with letter K SELECT DISTINCT Author FROM books WHERE Author LIKE ‘K%’ % is called “wildcard”; it means “any string”

SQL Exercises10 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All books with.NET anywhere in their title SELECT * FROM books WHERE Title LIKE ‘%.NET%’ (The % is a.NET requirement; in other SQL, the & may be required)

SQL Exercises11 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. Names and addresses of all borrowers with books due in on 16/12/2011 SELECT Name, Address FROM Borrowers, Loans WHERE Loans.BorrowerID = Borrowers.BorrowerID AND DueDate = #16/12/2011# Note the # delimiters; they enclose literal date values

SQL Exercises12 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g. All details of all borrowers registered in the library, in ascending order of name SELECT * FROM borrowers ORDER BY Name Default is Ascending or ASC e.g., All books in descending order of title SELECT * FROM books ORDER BY Title DESC

SQL Exercises13 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g., Fetch the titles of those books that are currently on loan SELECT Title FROM books, loans WHERE books.CopyID = loans.CopyID

SQL Exercises14 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g., Fetch the names of all Computing students with books out SELECT Name FROM borrowers, loans WHERE Course = ‘Computing’ AND loans.BorrowerID = borrowers.BorrowerID

SQL Exercises15 Simple Library Books (CopyID, Title, Author, ISBN, PublisherID) Publishers (PublisherID, Name, Address, Phone) Borrowers (BorrowerID, Name, Address, Course) Loans (CopyID, BorrowerID, DueDate) E.g., Get the ISBN of the book, entitled VB.NET Secrets, by Jo Bloggs SELECT ISBN FROM Books WHERE Author = ‘Jo Bloggs’ AND Title = ‘VB.NET Secrets’

SQL Exercises16 Questions 1. I want to select two books, one with ID 11 and the other with ID 25. Will this command do the job? SELECT * FROM books WHERE CopyID = 11 AND CopyID = Which records will be selected by the following command? SELECT * FROM books WHERE CopyID <> 11 AND CopyID <> 25

SQL Exercises17 Extended WHERE Specifying a range of numeric values SELECT * FROM employee WHERE Salary BETWEEN AND Q: can similar ranges be specified for strings? Dates? Using sets of values; e.g., SELECT * FROM books WHERE PublisherID IN (3, 4, 5, 6) SELECT * FROM books WHERE PublisherID NOT IN (1, 2)

SQL Exercises18 Other SQL commands you will use INSERT INTO UPDATE DELETE Each command has its own, specific syntax Find out from www (or books & notes) Look out for: stored procedures