Rob Gleasure robgleasure.com

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Chapter Information Systems Database Management.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Chapter 9 Joining Data from Multiple Tables
Sundara Ram Matta Apr 01 st, Sundara Ram Matta Apr 01 st, 2015
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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,
CMPT 258 Database Systems The Relationship Model (Chapter 3)
INFANL01-3 ANALYSE 3 WEEK 3 March 2015 Institute voor Communication, Media en Informatietechnology.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
 CONACT UC:  Magnific training   
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
IS6146 Databases for Management Information Systems Lecture 2: SQL II – Joins, Updates, Insertions, and Deletions Rob Gleasure robgleasure.com.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Fundamental of Database Systems
Rob Gleasure robgleasure.com
Web Systems & Technologies
Fundamental of Database Systems
Rob Gleasure robgleasure.com
Rob Gleasure robgleasure.com
Index An index is a performance-tuning method of allowing faster retrieval of records. An index creates an entry for each value that appears in the indexed.
Lecture 05: SQL Wednesday, January 12, 2005.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data Integrity Cascade Update.
Introduction to Structured Query Language(SQL)
Understand Data Manipulation Language (DML)
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.
Information Systems Database Management
Structured Query Language – The Basics
Understand Data Manipulation Language (DML)
Introduction to Database Systems CSE 444 Lecture 04: SQL
David M. Kroenke and David J
Structured Query Language (SQL) William Klingelsmith
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Cse 344 January 10th –joins.
Chapter Name SQL: Data Manipulation
JOINS (Joinining multiple tables)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Rob Gleasure robgleasure.com
Structured Query Language
Introduction To Structured Query Language (SQL)
Database systems Lecture 3 – SQL + CRUD
Rob Gleasure robgleasure.com
SQL Fundamentals in Three Hours
Rob Gleasure robgleasure.com
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Rob Gleasure robgleasure.com
Rob Gleasure robgleasure.com
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Presentation transcript:

Rob Gleasure R.Gleasure@ucc.ie robgleasure.com IS6146 Databases for Management Information Systems Lecture 2: SQL II – Joins, Updates, Insertions, and Deletions Rob Gleasure R.Gleasure@ucc.ie robgleasure.com

IS6146 Today’s session More SQL Insert Into Update Delete Joins Exercise

More on the SQL DML So far we’ve looked at getting data from specific tables, but there are still two parts of the Data Manipulation Language (DML) we haven’t covered The DML has four main elements Select Insert Into Update Delete Also, what if our data isn’t on one table?…

Insert Into We use INSERT INTO queries to add new tuples (aka records, rows) into a table The basic structure of an INSERT-INTO query is as follows INSERT INTO table_name1 (column_name1, column_name2, …) VALUES value1, value2, …;

Insert Into For example, say we want to insert a new student in a Students table with a Student_ID of 12345678 and a Name of “Jane Smith”, we might have the following INSERT INTO Students (Student_ID, Name) VALUES (12345678, “Jane Smith”) ; Note that we insert the data in the form of a new tuple (aka record, row) and if we do not specify a column, we have to provide data for each column in the new record Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_insert_cols

Update We use UPDATE queries to modify existing tuples (aka records, rows) in a table The basic structure of an UPDATE query is as follows UPDATE table_name1, table_name2, … SET col_name1 = value1, col_name2 = value2, …; WHERE some_column=some_value;

Update For example, say we want to change the previously added student record to be “Janet Smith”, we might have the following UPDATE Students SET (Name = “Janet Smith”) WHERE (Student_ID=12345678) ; Careful with this, if you don’t set a WHERE condition you will change every record in the database Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_update

Delete We use DELETE queries to remove existing tuples (aka records, rows) in a table The basic structure of a DELETE query is as follows DELETE FROM  table_name WHERE  some_column=some_value;

Delete For example, say we want to delete the previously added student record, we might have the following DELETE FROM Students WHERE (Name = “Janet Smith”); Again – be careful, if you don’t set a WHERE condition you delete every record in the database Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_delete

Joins Joins combine tuples (aka rows, records) from multiple tables Joins come in several forms Inner Joins Left Joins Right Joins Full Joins Unions Select Into/Into Select

Inner Joins Inner Joins return the specified columns at the intersection of two or more tables Image from http://www.w3schools.com/

Inner Joins Inner Joins are the most basic (and probably most common) type of join The basic structure of an INNER JOIN query is as follows SELECT  column_name(s) FROM  table1 INNER JOIN  table2 ON  table1.column_name=table2.column_name;

Inner Joins For example, say in addition to the previously added student record, a separate REFERENCES table stores student details. We could retrieve Student_IDs included in both tables as follows: SELECT Students.Student_ID, Student.Name, References.Ref_Details FROM Students INNER JOIN References ON Students.Student_ID = References.Student_ID; Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_join_inner

Left Joins Left Joins (sometimes called left outer joins) return all of the specified columns from the first table and their intersection (where it exists) with two or more tables Image from http://www.w3schools.com/

Left Joins The big difference here is that columns from our first table that have no corresponding entry in the latter tables are still returned (with null signifying the missing entry) The basic structure of an LEFT JOIN query is as follows SELECT  column_name(s) FROM  table1 LEFT JOIN  table2 ON  table1.column_name=table2.column_name;

Left Joins For example, what if some students do not have references and we still want to see their Student_ID and Name? We could retrieve these records as follows: SELECT Students.Student_ID, Student.Name, References.Ref_Details FROM Students LEFTJOIN References ON Students.Student_ID = References.Student_ID; Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_join_left

Right Joins Right Joins (sometimes called right outer joins) return all of the specified columns from the latter tables and their intersection (where it exists) with the first table Image from http://www.w3schools.com/

Right Joins Here, columns from our latter tables that have no corresponding entry in the first tables are still returned (with null signifying the missing entry in the first table) The basic structure of an RIGHT JOIN query is as follows SELECT  column_name(s) FROM  table1 RIGHT JOIN  table2 ON  table1.column_name=table2.column_name;

Right Joins For example, what if some references have been received before the corresponding student records have been created and we still want to see them? We could retrieve these records as follows: SELECT Students.Student_ID, Student.Name, References.Ref_Details FROM Students RIGHT JOIN References ON Students.Student_ID = References.Student_ID; Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_join_right&ss=-1

Full Outer Joins Full Outer Joins return all of the specified columns from the first and latter tables, including entries in either table with no corresponding table in the other(s) Image from http://www.w3schools.com/

Full Outer Joins Here, columns from our any table that have no corresponding entry in other tables are still returned (with null signifying the missing entry) The basic structure of an FULL OUTER JOIN query is as follows SELECT  column_name(s) FROM  table1 FULL OUTER JOIN  table2 ON  table1.column_name=table2.column_name;

Full Outer Joins For example, what if we want to see the full set of records to determine which student records we have not yet created and which references are still outstanding? We could retrieve these records as follows: SELECT Students.Student_ID, Student.Name, References.Ref_Details FROM Students FULL OUTER JOIN References ON Students.Student_ID = References.Student_ID;

Unions Unions are a bit different, they tend to be used for retrieving comprehensive sets of similar records Unions combine two or more SELECT queries, provided the following conditions are met Each SELECT query must have the same number of columns Each merged column must share data types Columns in each SELECT query must be in the same order

Unions The basic structure of a UNION query is as follows SELECT column_name(s) FROM table1 UNION table2; Note that this will automatically return only distinct records, though we can used UNION ALL if we want to include duplicates

Unions For example, what if our students table only stores enrolled students, whereas another Provisional_Students table stores students in provisional places awaiting confirmation? We could retrieve these records as follows: SELECT Name FROM Students UNION SELECT Name FROM Provisional_Students ORDER BY Name; Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_union_all2

Select Into Sometimes we want to copy an entire schema into a new table. We can do this using SELECT INTO as follows SELECT  column_name(s) INTO  newtable FROM  table1; This often ends up as a way of backing things up, e.g. SELECT * INTO Students_Backup FROM Students

Insert Into Select Alternatively, sometimes we want to add data from one table to an existing table. We can do this using INSERT INTO SELECT as follows INSERT INTO  table2 (column_name(s)) SELECT  column_name(s) FROM  table1; Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_insert_into_select

Exercise Consider the following problems related to the Customers database, what queries best solve them? We want to retrieve all customers with addresses in Mexico? We want to add a new Customer called 'Juan Garcia Ramos', with contact name 'Juan Ramos', address of 'Tribulete 4356', in the city 'México D.F', with a post code of '05029', in the country of 'Mexico‘? We want to update that customer’s contact name to ‘Anna Ramos’? We want to delete the same customer?

Exercise We want to select ProductName and CategoryID from the Products table, along with the SupplierName from the Suppliers table using a left join. Hint: use the SupplierID column in the Products table as a foreign key We want to retrieve all cities mentioned in customer records and supplier records using a union We want create e new Customer_Cities_Backup table storing all cities listed in the Customers table We want to add all cities listed in the Suppliers table into the Customers table