SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
Virtual training week 4 structured query language (SQL)
Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Introduction to Structured Query Language (SQL)
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
1 Agenda Summary of last class Loops Simple Loops WHILE Loops FOR Loops Records Cursors.
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.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Working with Tables: Data Management and Retrieval Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 6 Additional Database Objects
Chapter 6 Additional Database Objects Oracle 10g: SQL.
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
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Quick review of SQL And conversion to Oracle SQL.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
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.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Database Technology Jing Shen.
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
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,
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Variables and control statements in PL\SQL Chapter 10.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Chapter 3 Table Creation and Management Oracle 10g: SQL.
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.
 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.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL Introduction SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English.
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
SQL Creating and Managing Tables
Interacting with the Oracle Server
Introduction To Database Systems
Introduction to Oracle9i: SQL
SQL 101.
Structured Query Language (SQL) William Klingelsmith
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Chapter 4 Indexes.
CH 4 Indexes.
Database systems Lecture 3 – SQL + CRUD
CH 4 Indexes.
Manipulating Data.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Presentation transcript:

SQL Within PL / SQL Chapter 4

2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control

3 SQL Statements Using SQL in PL / SQL The only SQL statements allowed in PL/SQL are: DML (data manipulation language) transaction control DDL (data definition language) statements are not permitted

4 SQL Statements DML in PL / SQL The DML statements permitted are: Select Insert Update Delete

5 DML in PL / SQL Select SELECT statement in PL/SQL retrieves data from a database table into: PL/SQL record set of PL/SQL variables Using SELECT in PL/SQL should only return one row When you need to retrieve more than one row from a table use a cursor

6 DML in PL / SQL Select Syntax SELECT { * | select_list_item } INTO { PL/SQL_record | variables} FROM table_reference WHERE where_clause each variable must be compatible with its associated select list item count of variables must be same as number of items in list record should contain fields that correspond to the select list in type and count

7 select.sql DECLARE v_StudentRecord students%ROWTYPE; v_Department classes.department%TYPE; v_Course classes.course%TYPE; BEGIN SELECT * INTO v_StudentRecord FROM students WHERE id = 10000; SELECT department, course INTO v_Department, v_Course FROM classes WHERE room_id = 99997; END;

8 DML in PL / SQL Insert The INSERT statement inserts data into a database table There are two variations of the INSERT command add one row to a table using the specified VALUES list add one or several rows when the insert command uses a SELECT statement

9 DML in PL / SQL Insert Syntax INSERT INTO table_reference [(column_names)] {VALUES (expression) | select_statement} use column names when the values are listed in a different order than as defined during table creation Only a portion of the columns of the table are used during insert table definition remains unchanged after the new row is inserted

10 DML in PL / SQL Insert The word VALUES must precede the list of data to be inserted Regarding the values in the list: a character string must be in single quotes numbers can stand by themselves dates must be in single quotes In the default Oracle date format Converted using TO_DATE function (this is the suggested method)

11 Insert Using Built-in Functions  You can modify the contents of the values before they are entered into a column of a table  by a VALUES list  from a SELECT statement  Use any of the built-in functions supported by PL/SQL  character  date functions  numeric  conversion

12 insert.sql DECLARE v_StudentID students.id%TYPE; BEGIN SELECT student_sequence.NEXTVAL INTO v_StudentID FROM dual; INSERT INTO students (id, first_name, last_name) VALUES (v_StudentID, 'Timothy', 'Taller'); INSERT INTO students (id, first_name, last_name) VALUES (student_sequence.NEXTVAL, 'Patrick', 'Poll'); END;

13 DML in PL / SQL Insert with Select It is also possible to insert rows into a table using the results of a SELECT statement The results of a SELECT statement can return one or several rows based on the WHERE clause can be a mix of columns from one or more tables

14 DML in PL / SQL Update Requires setting specific values for each column you wish to change Specifying which row or rows to modify using a WHERE clause You can use built-in functions in setting a value for the update

15 Update in PL / SQL Embedded SELECT It is possible to set values in an UPDATE by embedding a SELECT statement right in the middle of it SELECT has its own WHERE clause UPDATE has its own WHERE clause to affect the rows You must be certain that the SELECT will return no more than one row

16 Embedded SELECT BEGIN UPDATE comfort set Midnight = (SELECT temperature FROM weather WHERE city = ‘MANCHESTER’) WHERE city = ‘WALPOLE’ AND SampleDate = TO_DATE (’22-DEC-1999’, ‘DD-MON-YYYY’); END;

17 DML in PL / SQL Delete Removing a row or rows from a table WHERE clause is necessary to removing only the rows you intend DELETE without the where clause will delete all of the rows of a table

18 delete.sql DECLARE v_StudentCutoff NUMBER; BEGIN v_StudentCutoff := 10; DELETE FROM classes WHERE current_students < v_StudentCutoff; DELETE FROM students WHERE current_credits = 0 AND major = 'Economics'; END;

19 DML in PL / SQL Truncate Another command for deleting records from a table is the TRUNCATE command TRUNCATE TABLE students; Does not operate the same as DELETE deletes all rows from a table cannot be rolled back records are unrecoverable does not run any DELETE triggers does not record any information in a snapshot log

20 DML in PL / SQL WHERE Clause The SELECT, UPDATE, and DELETE statements all include the WHERE clause Defines the active set (set of rows): returned by a SELECT query Acted upon by an UPDATE or DELETE Consists of conditions, joined together by the boolean operators AND, OR, and NOT Conditions usually take the form of comparisons using the relational operators (such as: =, <>, >, >=, <, <=)

21 DML in PL / SQL WHERE Clause with Cursor The UPDATE, and DELETE statements both include a WHERE clause with a special syntax the WHERE CURRENT OF is used with a cursor definition often processing done in a fetch loop modifies the rows that have been retrieved by a cursor

22 DML in PL / SQL WHERE Clause with Cursor This method consists of two parts: the FOR UPDATE clause in the cursor declaration the WHERE CURRENT OF clause in an UPDATE or DELETE statement

23 forupdat.sql DECLARE v_NumCredits classes.num_credits%TYPE; CURSOR c_RegisteredStudents IS SELECT * FROM students WHERE id IN (SELECT student_id FROM registered_students WHERE department= 'HIS' AND course = 101) FOR UPDATE OF current_credits;

24 forupdat.sql (cont.) BEGIN FOR v_StudentInfo IN c_RegisteredStudents LOOP SELECT num_credits INTO v_NumCredits FROM classes WHERE department = 'HIS' AND course = 101; UPDATE students SET current_credits = current_credits + v_NumCredits WHERE CURRENT OF c_RegisteredStudents; END LOOP; COMMIT; END;

25 DML in PL / SQL Synonyms It is possible to create a synonym for a: table view sequence stored procedure function package

26 DML in PL / SQL Synonyms The syntax for creating a synonym is: CREATE SYNONYM synonym_name FOR reference; Where: synonym_name – name of your synonym reference – schema object being referenced

27 Pseudocolums Currval and Nextval CURRVAL and NEXTVAL are used with sequences A sequence is an Oracle object used to generate unique numbers Once created, you can access it with its name by: sequence.CURRVAL sequence.NEXTVAL

28 Pseudocolums Currval and Nextval Sequence values can be used in: SELECT list of a query VALUES clause of an INSERT SET clause of an UPDATE Sequence values cannot be used in: WHERE clause PL/SQL statement