Temporary SQL Tables Los Angeles Pierce College Computer Science 560.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
System Administration Accounts privileges, users and roles
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Introduction to Structured Query Language (SQL)
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Chapter 1 Introduction Outstanding Features About This Book 1. A novel writing style is adopted to try to attract students’ or beginning programmers’ interesting.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Database Technical Session By: Prof. Adarsh Patel.
Introduction to SQL Steve Perry
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Solving Lab 3 Los Angeles Pierce College Computer Science 560.
RDBMSSection Relational DBMS DATABASE DEVELOPMENT And Franchise Colleges By MANSHA NAWAZ.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Component 4: Introduction to Information and Computer Science Unit 6a Databases and SQL.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
DAT602 Database Application Development Lecture 3 Review of SQL Language.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Controlling User Access. 2 home back first prev next last What Will I Learn? Compare the difference between object privileges and system privileges Construct.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
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.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Normalizing Database Files Professor Ralph Westfall May, 2011.
IT Faculty Software Engineering Seniors UML for a simple DataBase Management System Prepared by: أنس الأسود بشير الفروان زهير الزعبي ياسر المحمد.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Basics of JDBC Session 14.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
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.
Query Processing – Implementing Set Operations and Joins Chap. 19.
CSCI 6962: Server-side Design and Programming Shopping Carts and Databases.
Chapter 3: Relational Databases
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
A Guide to SQL, Sixth Edition 1 Chapter 5 Updating Data.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
 CONACT UC:  Magnific training   
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.
3 A Guide to MySQL.
The Basics of Data Manipulation
SQL Creating and Managing Tables
ISC440: Web Programming 2 Server-side Scripting PHP 3
SQL Creating and Managing Tables
SQL Creating and Managing Tables
The Basics of Data Manipulation
Database SQL.
Presentation transcript:

Temporary SQL Tables Los Angeles Pierce College Computer Science 560

How Do We Get One Column From Two Tables? NCAA Table: –NCAA_SEQ_KEY NOT NULL NUMBER(5) –NCAA_YEAR NUMBER(4) –NCAA_PLACE NUMBER(1) –NCAA_SCHOOL CHAR(30) NIT Table: –NIT_SEQ_KEY NOT NULL NUMBER(5) –NIT_YEAR NUMBER(4) –NIT_PLACE NUMBER(1) –NIT_SCHOOL CHAR(30) –NIT_TIE CHAR(1)

What’s The Desired Output? Three columns: –School Name –Tournament won –Year Something like: School Tournament Year UCLA NCAA 1972 UCLA NIT 1985 UCLA NCAA 1995

The Problem How do we get two different columns from two different tables into one result column? (School, year) 1 How do we insert the table name into a result column? Answer: cheat. 1 We’ll pretend we can’t do this with UNION

Temporary SQL Tables We really want a table which looks like: CREATE TABLE BB_WINNERS ( BB_SEQUENCE NUMBER(5) CONSTRAINT bb_seq_pk PRIMARY KEY, SCHOOL CHAR(30), YEAR NUMBER(4), TOURNAMENT CHAR(4), PLACE NUMBER(1), TIE CHAR(1));

Ideal SQL Selection Statement SELECT SCHOOL,TOURNAMENT, YEAR FROM BB_WINNERS WHERE PLACE = 1 ORDER BY SCHOOL, YEAR;

Drawbacks To The Ideal Case A special table for one query is probably wasteful Additional table space needed for something which is rarely used Data is duplicated – not an issue in our sample case, but potentially a problem in a real world situation

Solution: Temporary SQL Tables Acts just like a normal table BUT – only exists for the duration of a session or a transaction Disk space for table only needed when it is actually in use Note: assume we have a sequence called BB_SEQ already defined.

Creating A Temporary Table CREATE GLOBAL TEMPORARY TABLE BB_WINNERS ( BB_SEQUENCE NUMBER(5) CONSTRAINT bb_seq_pk PRIMARY KEY, SCHOOL CHAR(30), YEAR NUMBER(4), TOURNAMENT CHAR(4), PLACE NUMBER(1), TIE CHAR(1)) ON COMMIT PRESERVE ROWS; Above shows the differences from our ideal table.

Transaction versus Session Key concept: When to delete the rows in the temporary table? Transaction level: delete rows in the temporary table when a COMMIT is issued Session level: delete rows when the user’s session ends (PRESERVE option) –EXIT in SQL*Plus We can use either one, depending upon how long we need the temporary data

Loading The Temporary Table CREATE GLOBAL TEMPORARY TABLE BB_WINNERS ( BB_SEQUENCE NUMBER(5) CONSTRAINT bb_seq_pk PRIMARY KEY, SCHOOL CHAR(30), YEAR NUMBER(4), TOURNAMENT CHAR(4), PLACE NUMBER(1), TIE CHAR(1)) ON COMMIT PRESERVE ROWS; INSERT INTO BB_WINNERS (SELECT BB_SEQ.NEXTVAL, NCAA_SCHOOL, NCAA_YEAR, ‘NCAA’, NCAA_PLACE, NULL FROM NCAA);

Adding In Our Other Table How to add the data from the NIT table: INSERT INTO BB_WINNERS (SELECT BB_SEQ.NEXTVAL, NIT_SCHOOL, NIT_YEAR, ‘NIT’, NIT_PLACE, NIT_TIE FROM NIT);

When To Use Temporary Tables? Performance may or may not suffer: –“The prudent use of temporary tables can dramatically improve Oracle SQL performance.” – Don Burleson –“Temporary tables slow performance dramatically.” – Justin Gunther (talking about Microsoft SQL Server) So…often a useful technique Use must be evaluated carefully in high- activity systems

Questions? For further information, refer to the CREATE statement in the SQL reference for the level of Oracle you are using.