COP 2700 – Data Structures (SQL)

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Chapter 8 Embedded SQL.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Chapter 4B: More Advanced PL/SQL Programming
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Introduction to PL/SQL
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Stored Procedures, Transactions, and Error-Handling
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Advanced SQL: Cursors & Stored Procedures
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Stored Procedure. Objective At the end of the session you will be able to know :  What are Stored Procedures?  Create a Stored Procedure  Execute a.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Advanced SQL: Triggers & Assertions
Transact SQL The language of Microsoft SQLServer Copyright © 2012 – 2014 by Curt Hill.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Stored Procedure used in PosgreSQL.
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
SQL Triggers, Functions & Stored Procedures Programming Operations.
1 Stored Procedure, Function and Trigger. 2Objectives 1. Database Programming 2. Stored Procedure 3. Function 4. Trigger.
Create Stored Procedures and Functions Database Management Fundamentals LESSON 2.4.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1. Advanced SQL Functions Procedural Constructs Triggers.
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.
COMP 430 Intro. to Database Systems
A Guide to SQL, Seventh Edition
PL/pgSQL
Difference between Oracle PL/SQL and MySQL
SQL in Oracle.
Interacting with the Oracle Server
Introduction to Triggers
Database Management Systems 2
Error Handling Summary of the next few pages: Error Handling Cursors.
PL/SQL Scripting in Oracle:
Stored Procedure, Function and Trigger
Advanced PL/SQL Programing
Stored Procedure used in PosgreSQL
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Information Management
Chapter 8 Advanced SQL Pearson Education © 2009.
Prof. Arfaoui. COM390 Chapter 9
Presentation transcript:

COP 2700 – Data Structures (SQL) Lecture 8 – June 15

Some Comments on Exam and Assignment3 Exists returns only True and False. If the select in the Exists returns ANY rows, then it is true. The variable list on set operations must match. You can join a table with itself AND is not the same as OR A select of one variable can then be used in an IN

Announcements Assignment 4 is due Thursday Night, June 17 by 11:59 PM. Last Day to Drop is today

What is missing from SQL to make it a true Programming Language? Variables @Variable Names, SET Conditional Statements If, Else, Switch/Case Iterative Statements While Traverse a “file” (which in this case is a Query Result Set). Cursor, Open, Fetch, Close Communicate with Outside World Print

These items were added to a program language named Transact/SQL SQL Server specific, although Oracle and MySQL have a similar set of programming commands. These commands are then used to code: Scripts – Programs stored as text files and then processed through either the GUI, or oSQL (DOS Shell Script Processor) Functions – Small programs that accept parameter input and output a result. Stored Procedures – Programs that can be called from other programs, Management Student or oSQL Triggers – Small programs “fired” when certain actions are taken. Usually involved in ensuring data integrity.

Quick Look at oSQL Go to a DOS prompt Enter oSQL -? then oSQL –S localhost\PBSC U=sa

Transact SQL Basics BEGIN DECLARE @Variable1 Type2; Do Some Work SET IF/ELSE While Loop SELECT PRINT Exception Handling END; GO

BEGIN…END Defines Blocks of code Can be stand alone or can distinguish code to be executed for IF/ELSE or While Loops

Some Simple Examples Declare @sName Varchar(60); BEGIN Print the First_Name and Last_Name of Student Z123: Declare @sName Varchar(60); BEGIN SELECT @sname = (Last_Name + ', ' + First_name) FROM Student WHERE Student_ID = 'Z123'; Print @sName; END; GO Note: Each command or declare ends with a semi-colon. The block of commands ends with a GO Print is an internal function that writes stuff to an output area.

Another Example Print the instructor’s name for Instructor_ID “Bradley” and then print the Semesters, Courses and Sections that he taught: DECLARE @sName Varchar(60); DECLARE @Semester varchar(10), @Section Numeric, @Course_ID varchar(10); BEGIN SELECT @sName = (Last_Name + ', ' + First_name) FROM Instructor WHERE Instructor_ID = 'Bradley'; Print @Sname; declare Courses cursor for (SELECT Semester, Course_ID, Section FROM Schedule WHERE Instructor_ID = 'Bradley'); OPEN Courses; FETCH Courses into @Semester, @Course_ID, @Section; WHILE @@FETCH_STATUS = 0 Print(@Semester + ' ' + @Course_ID + ' ' + cast(@Section as varchar(5))); END; CLOSE Courses; DEALLOCATE Courses;

Stuff Not Database Related Simple Counting Loop (SQLServer does NOT have a FOR loop!!) DECLARE @Ctr Integer; BEGIN SET @Ctr = 1 WHILE @Ctr < 10 PRINT @Ctr; SET @CTR = @CTR + 1 END;

Selects in Procedures Scalar Selects Cursors Direct Select Statements Returns One Row and can then directly have the values of the columns assigned to variables Cursors Returns a result set that one can then transverse through Direct Select Statements Like Regular Old Selects. Write the output out to the console or to a grid. Let’s add a select to our previous script.

Function Calls Must Return a value Basic Function Creation CREATE (ALTER) FUNCTION Example (@Number Numeric) RETURNS Numeric AS BEGIN DECLARE @DoubleNumber Numeric; SET @DoubleNumber = 2 * @Number; Return @DoubleNumber; END;

Get Age Function CREATE FUNCTION GETAGE(@Birthdate Date) RETURNS NUMERIC AS BEGIN Declare @Age integer, @Year integer, @Day integer, @TodayYear integer, @TodayDay integer; Declare @Month integer, @TodayMonth integer; Set @Year = Year(@BirthDate); Set @TodayYear = Year(GETDATE()); Set @AGE = @TodayYear - @Year; Set @Month = Month(@BirthDate); SET @TodayMonth = Month(GETDATE()); IF @Month > @TodayMonth Begin Set @AGE = @AGE - 1; Return @Age; END; IF @Month < @TodayMonth Set @Day = Day(@birthdate); Set @TodayDay = Day(GETDATE()); IF @Day > @TodayDay GO select dbo.GETAGE(birth_date), * from student

Function with Database Call CREATE FUNCTION StudentName (@Student_ID as Varchar(10)) RETURNS Varchar(50) AS BEGIN DECLARE @Name Varchar(60) = 'Not Found'; SELECT @Name = (Last_Name + ', ' + First_Name) FROM Student WHERE Student_ID = @Student_ID; RETURN @Name; END;

Stored Procedure Like a Function, but is not required to return anything. There can be “OUTPUT” parameters, but normally it communicates back to the calling procedure with Error Codes and “Throws”. Can be called directly from Transact SQL or Management Studio

Simple Stored Procedure CREATE PROCEDURE InsertInstructor @InstrId varchar,@LastName varchar, @FirstName varchar, @Office varchar, @City varchar, @Message varchar(100) output as DECLARE @n integer; BEGIN SELECT @n=COUNT(*) FROM Instructor WHERE Instructor_ID = @InstrId; IF (@n > 0) SET @Message ='Instructor ' + @InstrId +' already in table, insert aborted'; RETURN; END; INSERT INTO Instructor (Instructor_ID, Last_Name, First_Name, Office, City) VALUES (@InstrID, @LastName, @FirstName, @Office, @City); SET @Message = 'Insert Complete for Instructor ' + @InstrId;

Calling a Stored Procedure use registration go declare @Message varchar(100); InsertInstructor 'Jones','Jones','Tom','EE903','Miami',@message output; print @message;

For Wednesday Triggers Small Transact SQL programs that are “Triggered” when the RDMS processes a change to a table Used to maintain constraints, database consistency Used to automatically create log type entries into other tables for security.

For Tonight We Need to write a function that will return the GPA for a student. Let’s design the function What is the input? What is the output? What database involvement do we have? What is the processing?