Dr Gordon Russell, Napier University Unit 1.4 - SQL 3 - V2.0 1 SQL 3 Unit 1.4.

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

© Abdou Illia MIS Spring 2014
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
View (virtual table). View A VIEW is a virtual table A view contains rows and columns, just like a real table. The fields in a view are fields from one.
Virtual training week 4 structured query language (SQL)
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Relational Algebra 2 Chapter 5.2 V3.0 Napier University Dr Gordon Russell.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Introduction to SQL Steve Perry
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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.
ITBIS373 Database Development
Oracle Database Administration Lecture 2 SQL language.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Dr Gordon Russell, Napier University Unit Embedded SQL - V3.0 1 Embedded SQL Unit 5.1.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Dr Gordon Russell, Napier University Unit SQL 2 - V2.0 1 SQL 2 Unit 1.3.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Dr Gordon Russell, Napier University Data Analysis 3 - V2.0 1 Data Analysis 3 Unit 2.3.
Data Analysis 3 Chapter 2.3 V3.0 Napier University Dr Gordon Russell.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Advanced Relational Algebra & SQL (Part1 )
Database UpdatestMyn1 Database Updates SQL is a complete data manipulation language that can be used for modifying the data in the database as well as.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Oracle 11g: SQL Chapter 7 User Creation and Management.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Dr Gordon Russell, Napier University Unit SQL 1 1 SQL 1 Unit 1.2.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
CpSc 3220 The Language of SQL Chapter 17 Modifying Data.
April 2002Information Systems Design John Ogden & John Wordsworth SQL2: 1 Database Design SQL (2) John Wordsworth Department of Computer Science The University.
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.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL Creating and Managing Tables
Using the Set Operators
SQL Creating and Managing Tables
SQL Creating and Managing Tables
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Chapter 2 Views.
Database Management System
IST 318 Database Administration
Using the Set Operators
Manipulating Data Lesson 3.
Unit Relational Algebra 1
Unit Relational Algebra 2
Presentation transcript:

Dr Gordon Russell, Napier University Unit SQL 3 - V2.0 1 SQL 3 Unit 1.4

Dr Gordon Russell, Napier University Unit SQL 3 - V2.02 Subqueries A SELECT query can be used within another SELECT condition and is then known as a subquery A subquery can return only one attribute having zero or more values The use of a view may provide a simpler query format than using techniques such as self-joins

Dr Gordon Russell, Napier University Unit SQL 3 - V2.03 Simple Example Name employees younger than Liza Brunell: SELECT surname,forenames FROM employee WHERE dob > ( SELECT dob FROM employee – subquery WHERE forenames = ’Liza’ AND surname = ’Brunell’); Note - there is no need to use aliases for employee table references since these are unambiguous within the scopes of the main query and the subquery.

Dr Gordon Russell, Napier University Unit SQL 3 - V2.04 Subqueries with ANY, ALL ANY or ALL can be used to qualify tests carried out on the values in the set returned by a subquery. List employees currently earning less than anyone now in programming: SELECT empno FROM jobhistory WHERE salary < ALL (SELECT salary – subquery WHERE position LIKE ’%Programmer%’ AND enddate IS NULL) AND enddate IS NULL;

Dr Gordon Russell, Napier University Unit SQL 3 - V2.05 Subqueries with IN, NOT IN IN and NOT IN can be used to test if a value is or is not present in the set of values returned by a subquery List the names and employee numbers of all those who have never been on atraining course: SELECT empno,forenames,surname FROM employee WHERE empno NOT IN (SELECT DISTINCT empno FROM empcourse);

Dr Gordon Russell, Napier University Unit SQL 3 - V2.06 Subqueries with EXISTS EXISTS tests if a set returned by a subquery is empty List the employee number and job title of all those employees who do a job someone else also does: SELECT empno FROM jobhistory mainjh WHERE enddate IS NULL AND EXISTS ( SELECT empno FROM jobhistory subjh WHERE enddate IS NULL AND mainjh.position = subjh.position AND mainjh.empno = subjh.empno ); Note that aliases are needed to enable references from subquery to main query

Dr Gordon Russell, Napier University Unit SQL 3 - V2.07 UNION of Subqueries A query included two or more subqueries connected by a set operation such as UNION (MINUS or INTERSECT). UNION returns all the distinct rows returned by two subqueries List the number of each employee in departments 2 or 4, plus employees who know about administration: (SELECT empno FROM employee WHERE depno IN (2,4)) UNION (SELECT empno FROM course,empcourse WHERE course.courseno = empcourse.courseno AND cname LIKE ’%Administration%’);

Dr Gordon Russell, Napier University Unit SQL 3 - V2.08 Views A view is a way of collecting information from parts of one or more tables to enable users to more easily access the database. CREATE VIEW view_name [(column_list)] AS query; Attributes can be renamed in column_list if required.

Dr Gordon Russell, Napier University Unit SQL 3 - V2.09 Views cont... Suppose a user needs to regularly manipulate details about employee, name, and current position. It might be simpler to create a view limited to this information only, rather than always extracting it from two tables: CREATE VIEW empjob AS SELECT employee.empno,surname,forenames,position FROM employee,jobhistory WHERE employee.empno = jobhistory.empno AND enddate IS NULL;

Dr Gordon Russell, Napier University Unit SQL 3 - V2.010 Views cont... A view can be accessed like any other table List those currently in Programming type jobs: SELECT empno,surname,forenames FROM empjob WHERE position LIKE ’%Program%’; A view can (should) be dropped when no longer required: DROP VIEW view_name

Dr Gordon Russell, Napier University Unit SQL 3 - V2.011 Views cont... The use of a view may provide a simpler query format than using techniques such as self-joins or subqueries Name employees younger than Liza Brunell: CREATE VIEW liza AS SELECT dob FROM employee WHERE forenames = ’Liza’ AND surname = ’Brunell’; SELECT surname,forenames FROM employee,liza WHERE employee.dob > liza.dob; DROP VIEW liza;

Dr Gordon Russell, Napier University Unit SQL 3 - V2.012 View Manipulation When is a view ‘materialised’ or populated with rows of data? When it is defined or when it is accessed If it is the former then subsequent inserts, deletes and updates would not be visible. If the latter then changes will be seen. Some systems allow you to chose when views are materialised, most do not and views are materialised whenever they are accessed thus all changes can be seen.

Dr Gordon Russell, Napier University Unit SQL 3 - V2.013 VIEW update, insert and delete Can we change views? Yes, provided the primary key of all the base tables which make up the vieware present in the view.

Dr Gordon Russell, Napier University Unit SQL 3 - V2.014 VIEW cont... This view cannot be changed because we have no means of knowing whichrow of B to modify

Dr Gordon Russell, Napier University Unit SQL 3 - V2.015 Other SQL Statements So far we have just looked at SELECT but we need to be able to do other operations as follows: – –INSERT - which writes new rows into a database – –DELETE - which deletes rows from a database – –UPDATE - which changes values in existing rows We also need to be able to control access to out tables by other users (see the later SECURITY lecture). We may need to provide special views of tables to make queries easier to write. These views can also be made available to other users so that they can easily see our data but not change it in any way.

Dr Gordon Russell, Napier University Unit SQL 3 - V2.016 INSERT INSERT INTO table_name [(column_list)] VALUES (value_list) The column_list can be omitted if every column is to be assigned a value, otherwise it must list columns to be assigned values. The value_list is a set of literal values giving the value of each column in the same order as the column_list, if specified, or as the columns are defined in the original CREATE TABLE. insert into course values (11,’Advanced Accounting’,10-jan-2000); insert into course (courseno,cname) values(13,’Advanced Administration’);

Dr Gordon Russell, Napier University Unit SQL 3 - V2.017 DELETE DELETE FROM table_name [WHERE condition]; the rows of table_name which satify the condition are deleted. Delete Examples: DELETE FROM jobhistory -- remote current posts from jobhistoryWHERE enddate IS NULL; DELETE FROM jobhistory -- Remove all posts from jobhistory, ; -- leaving an empty table DROP table jobhistory; -- Remove jobhistory table completely

Dr Gordon Russell, Napier University Unit SQL 3 - V2.018 UPDATE UPDATE table_name SET column_name = expression,{column_name=expression} [WHERE condition] The expression can be NULL a literal value an expression based upon the current column value Give a salary rise of 10% to all accountants: UPDATE jobhistory SET salary = salary * 1.10 WHERE position LIKE ’%Accountant%’ AND enddate IS NULL;