Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.

Slides:



Advertisements
Similar presentations
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Advertisements

Chapter Thirteen Sequences Dr. Chitsaz Objectives: Sequence objects Create and use sequences Application of sequences.
11-1 Copyright © Oracle Corporation, All rights reserved. Different type of keys.
Database Programming Sections 13. Marge Hohly  1. Which statements are True about the following sequence? The sequence was used to generate numbers.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Introduction to Structured Query Language (SQL)
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Copyright س Oracle Corporation, All rights reserved. 13 Other Database Objects.
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
Introduction to Oracle9i: SQL1 Views. Introduction to Oracle9i: SQL2 Chapter Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE.
SQL's Data Definition Language (DDL) – View, Sequence, Index.
Chapter 6 Additional Database Objects
13 Other Database Objects Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Objectives After completing this lesson, you should be able to do the following: Categorize the main database objects Review the table structure List.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
12 Copyright © Oracle Corporation, All rights reserved. Other Database Objects.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
Chapter 5 Sequences.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
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 Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
George Mpopo | Rosebank College ADVANCED DATABASES WITH ORACLE 11g FOR ADDB7311 LEARNING UNIT 2 of 7.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1 (SQL) Other Database Objects Asif Sohail University of the.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Creating Views. 2 home back first prev next last What Will I Learn? List three uses for views from the standpoint of a database administrator Explain,
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
Creating Views Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the.
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
 CONACT UC:  Magnific training   
Other database objects (Sequence and view). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically.
ITEC 313 Database Programming
Creating Views Schedule: Timing Topic 20 minutes Lecture
Creating Other Schema Objects
Other Database Objects
Creating Other Schema Objects
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 5 Sequences.
Chapter 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
Database Programming Sections 11-12–Sequences, Indexes & Synonyms, Controlling User Access, Creating & Revoking Object Privileges 11/25/08.
Database Programming Sections 11 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
CH 4 Indexes.
Using DDL Statements to Create and Manage Tables
Chapter 2 Views.
Contents Preface I Introduction Lesson Objectives I-2
Using DDL Statements to Create and Manage Tables
IST 318 Database Administration
Other Database Objects
Presentation transcript:

Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns

Marge Hohly2 What is a View?  A view is a query stored as a SELECT statement in the data dictionary. A table of logical subsets or combinations of data based on a table or another view.  A ”window” into the database for convenience/per/permission  Presents data from one or more tables in one place  Two Types of Views Simple Complex

Marge Hohly3 Example of a View  CREATE VIEW view_employees AS SELECT first_name, last_name, FROM employees WHERE employee_id BETWEEN 100 and 124;  SELECT * FROM view_employees;

Marge Hohly4 Guidelines for Creating a View  The subquery that defines the view can contain complex SELECT syntax  The subquery that defines the view cannot contain an ORDER BY clause  You can use the OR REPLACE option to change the definition of the view without having to drop or re-grant object privileges previously granted  Aliases can be used for the column names in the subquery

Marge Hohly5 Aliases in a View  Column names in the SELECT statement can have aliases as shown below. Note that aliases can also be listed after the CREATE VIEW statement and before the SELECT subquery  CREATE VIEW view_copy_d_cds AS SELECT cd_number AS “Number”, title AS “Title”, year AS “Year Recorded” FROM d_cds;  CREATE VIEW view_copy_d_cds(Number, Title, Year Recorded) AS SELECT cd_number, title, year FROM d_cds;

Marge Hohly6 The Syntax For Creating a View  CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view_name [(alias [,alias]....)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint_name]] [WITH READ ONLY [CONSTRAINT constraint_name]];  OR REPLACE – recreates the view if it already exists  FORCE – creates the view regardless of whether or not the base tables exist  NO FORCE – creates the view only if the base tables exist (DEFAULT)  view_name – name of the view  alias – specifies a name of each expression selected by the view’s query  subquery – a complete SELECT statement (you can use aliases for the columns in the SELECT list)  WITH CHECK OPTION – specifies that only rows accessible to the view can be inserted or updated  WITH READ ONLY – ensures that NO DML operations can be performed on this view

Marge Hohly7 Simple vs. Complex FeatureSimple ViewComplex View Number of tables used to derive data OneOne or more Can contain functions NoYes Can contain groups of data NoYes Can perform DML operations through a view YesNot always

Marge Hohly8 Simple View Example  CREATE VIEW view_copy_d_cds(“CD Number”, Title, “Year Recorded”) AS SELECT cd_number, title, year FROM copy_d_cds;

Marge Hohly9 Complex View Example  CREATE VIEW view_dj_on_demand (LAST_NAME, PHONE, EVENT, DATE_HELD) AS SELECT c.last_name, c.phone, e.name, TO_CHAR(e.event_date, ‘Month dd, YYYY’) FROM d_clients c, d_events e WHERE c.client_number = e.client_number;

Marge Hohly10 Modifying a View  To modify a view, use the [OR REPLACE] option  The old view will be replaced by the new version  CREATE OR REPLACE VIEW view_copy_d_cds AS SELECT cd_number, producer, title, year FROM copy_d_cds;

Marge Hohly11 DML Operations on a View  DML operations (INSERT, UPDATE, and DELETE) can be performed on a simple view  Data in the underlying base tables can be changed also  To prevent unintended changes, the DBA can control data access using the WITH CHECK OPTION and WITH READ ONLY constraints

Marge Hohly12 WITH CHECK OPTION CONSTRAINT  CREATE OR REPLACE VIEW view_dept50 AS SELECT department_id, employee_id, first_name, last_name, salary FROM employees WHERE department_id = 50 WITH CHECK OPTION CONSTRAINT view_dept50_check;  UPDATE view_dept50 SET department_id = 190 WHERE employee_id = 141; NOTE: ORA-01402: view WITH CHECK OPTION where-clause violation

Marge Hohly13 WITH READ ONLY CONSTRAINT  CREATE OR REPLACE VIEW view_dept50 AS SELECT department_id, employee_id, first_name, last_name, salary FROM employees WHERE department_id = 50 WITH READ ONLY CONSTRAINT view_dept50_read;  DELETE FROM view_dept50 WHERE employee_id = 144; ORA-01752: cannot delete from view without exactly one key-preserved table

Marge Hohly14 DML Restrictions on a View  You cannot REMOVE a row from an underlying base table if the view contains any of the following: Group functions A GROUP BY clause The pseudocolumn ROWNUM keyword  ROWNUM is just a number value given to each row in the result set. For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.  You can use ROWNUM to limit the number of rows returned by a query, as in this example: SELECT * FROM employees WHERE ROWNUM < 10;

Marge Hohly15 DML Restrictions on a View  You cannot MODIFY data through a view if the view contains: Group functions A GROUP BY clause The pseudocolumn ROWNUM keyword The DISTINCT keyword Columns defined by expressions

Marge Hohly16 DML Restrictions on a VIEW  You cannot ADD data through a view if the view includes group functions includes a GROUP BY clause includes the pseudocolumn ROWNUM keyword includes the DISTINCT keyword includes columns defined by expressions does not include NOT NULL columns in the base tables – the user of the view must know which column in the base table are NOT NULL – these columns must be in the view.

Marge Hohly17 Deleting a View  DROP VIEW viewname; Removing a view does not effect the data in the underlying tables If the view was used to manipulate data in the past, these changes to the base tables remain Only the creator or users with the DROP ANY VIEW privilege can remove a view

Marge Hohly18 What is an Inline View?  Also known as queries in the FROM clause  The view is created “on the fly” instead of saving the view as a separate object  A common use for in-line views in Oracle SQL is the simplify complex queries by removing join operations and condensing several separate queries into a single query.

Marge Hohly19 Inline View Example  SELECT e.name, e.description, p.maxrange, p.code FROM d_events e, (SELECT code, max(high_range) maxrange FROM d_packages GROUP BY code) p WHERE e.package_code = p.code AND e.cost < p.maxrange;  The data returned by the subquery is given an alias, which is then used in conjunction with the main query to return selected columns from both query sources.

Marge Hohly20 Inline View Example  SELECT code, max(high_range) maxrange FROM d_packages GROUP BY code;

Marge Hohly21 Top-N Analysis  A SQL operation used to rank results  Add a pseudocolumn called ROWNUM  ROWNUM is used to select the top “n” (number) of rows SELECT ROWNUM as top, name, cost FROM (SELECT name, cost FROM d_events ORDER BY cost DESC) WHERE ROWNUM <= 2;

Marge Hohly22 The Syntax for Creating a Sequence  CREATE SEQUENCE sequence_name [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}];  Sequence_name – the name of sequence generator (object)  INCREMENT BY n – interval between sequence numbers where n is an integer (if omitted n is 1)  START WITH n – specifies the first sequence number to be generated (if omitted start with 1)  MAXVALUE n – specifies the maximum value the sequence can generate  NOMAXVALUE – specifies a maximum value of 10^27 for ascending and -1 for descending  MINVALUE n – specifies the minimum value the sequence can generate  NOMINVALUE – specifies a minimum value of 1 for ascending and -10^27 for descending  CYCLE – whether the sequence continues to generate values after reaching its max or min value  NOCYCLE – the default if CYCLE is not specified  CACHE n – specifies how many values the Oracle Server preallocates and keeps in memory (default is 20) if the sequence values are cached, they will be lost if there is a system failure  NOCACHE – does not cache any values

Marge Hohly23 Example of a Sequence  CREATE SEQUENCE emp_emp_id_seq INCREMENT BY 10 START WITH 300 MAXVALUE 9999 NOCACHE NOCYCLE;  ask for NEXTVAL = 300 – it becomes CURRVAL the number just generated in HTMLDB once you return the NEXTVAL from the sequence you no longer have the “session” and the database no longer knows what’s the CURVAL

Marge Hohly24 Using a Sequence to INSERT  INSERT INTO employees VALUES (emp_emp_id_seq.NEXTVAL, ‘Kramer’, ‘Wilson’, ‘KWILSON’, ‘ ’, ’11-FEB-87’, ‘AD_ASST’, 5000, NULL, 101, 10);

Marge Hohly25 NEXTVAL and CURRVAL  NEXTVAL is a pseudocolumn used to return the next available sequence value  CURRVAL is a pseudocolumn used to obtain the last-used sequence value  NEXTVAL must be issued before CURRVAL contains a value  NEXTVAL and CURRVAL must be qualified with a sequence name: emp_emp_id_seq.nextval

Marge Hohly26 Modifying & Deleting a Sequence  ALTER SEQUENCE emp_emp_id_seq INCREMENT BY 5 MAXVALUE 9999 NOCACHE NOCYCLE:  DROP SEQUENCE emp_emp_id_seq;

Marge Hohly27 Sequence Gaps  Gaps (nonsequential numbers) can be generated by: rolling back a statement containing a sequence, the number is lost a system crash. If the sequence caches values into the memory and the system crashes, these values are lost. the same sequence being used for multiple tables. If you do so, each table can contain gaps in the sequential numbers

Marge Hohly28 What is an Index?  A schema object that can speed up the retrieval of rows by using a POINTER (isles in a grocery store)  If you do not have an index on the column you’re selecting, then a full table scan occurs  Unique Index – Automatically created when you define a column in a table to have a PRIMARY KEY or a UNIQUE KEY constraint.  Non-Unique Index – An index that a user can create to speed up access to the rows For example, to optimize joins, you can create an index on the FOREIGN KEY column, which speeds up the search to match rows to the PRIMARY KEY column.

Marge Hohly29 Example of an INDEX  WHEN TO CREATE AN INDEX The column contains a wide range of values A column contains a large number of null values One or more columns are frequently used together in a WHERE clause or a join condition The table is large and most queries are expected to retrieve less than 2-4% of the rows.  WHEN NOT TO CREATE AN INDEX The table is small The columns are not often used as a condition in the query Most queries are expected to retrieve more than 2-4% of the rows in the table The table is updated frequently – DML required index updates The indexed columns are referenced as part of an expression

Marge Hohly30 Example of an INDEX  CREATE INDEX d_cds_name_ _idx ON d_clients(last_name, );  DROP INDEX d_cds_name_ _idx;

Marge Hohly31 Example of a SYNONYM  CREATE [PUBLIC] SYNONYM synonym_name FOR object;  CREATE SYNONYM emp FOR ussc_bhs_sql01_s02.employees; PUBLIC: creates a synonym accessible to all users (we don’t have the privilege to use PUBLIC in HTML_DB) synonym_name: is the name of the synonym to be created object: identifies the object for which the synonym is created -A private synonym name must be distinct from all other objects owned by the same user.