New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

SQL View Sequence

by: Ismail Yousuf

SQL View Sequence CSC 271

Ismail Yousuf
Comsats Institute of Information Technology
GPA 3.7

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

How to create View, Sequence and Index in Oracle Database? See notes to learn
Database Management
Amjad Usman
Class Notes
Database, Oracle, SQL, Computer Science
25 ?




Popular in Database Management

Popular in ComputerScienence

This 43 page Class Notes was uploaded by Ismail Yousuf on Saturday January 9, 2016. The Class Notes belongs to CSC 271 at Comsats Institute of Information Technology taught by Amjad Usman in Winter 2016. Since its upload, it has received 28 views. For similar materials see Database Management in ComputerScienence at Comsats Institute of Information Technology.

Similar to CSC 271 at Comsats Institute of Information Technology

Popular in ComputerScienence


Reviews for SQL View Sequence


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 01/09/16
CSC271 Database Systems Lecture 25 Views, Sequence and Index Instructor : Amjad Usman Tuesday, December 15, 2015 Database Objects Object Description Table Basic unit of storage; composed of rows and columns View Logically represents subsets of data from one or more tables Sequence Generates primary key values Index Improves the performance of some queries Synonym Alternative name for an object 3 What is a View? EMPLOYEES Table: 4 Why use Views?  To restrict data access  To make complex queries easy  To provide data independence  To present different views of the same data 5 Simple and Complex Views Feature SimpleViews ComplexViews Number of tables One One or more Contain functions No Yes Contain groups of data No Yes DML operations through a view Yes Not always 6 Creating a View  You embed a subquery within the CREATE VIEW statement. CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]];  The subquery can contain complex SELECT syntax. 7 Creating a View  Create a view,EMPVU80,that contains details of employees in department 80. CREATE VIEW empvu80 AS SELECT employee_id, last_name, salary FROM employees WHERE department_id = 80; View created.  Describe the structure of the view by using the DESCRIBE command. DESCRIBE empvu80 8 Creating a View  Create a view by using column aliases in the subquery. CREATE VIEW salvu50 AS SELECT employee_id ID_NUMBER, last_name NAME, salary*12 ANN_SALARY FROM employees WHERE department_id = 50; View created.  Select the columns from this view by the given alias names. 9 Retrieving data from view SELECT * FROM salvu50; 10 Querying a View Oracle Server SQL Developer USER_VIEWS SELECT * EMPVU80 FROM empvu80; SELECT employee_id, last_name, salary FROM employees WHERE department_id=80; EMPLOYEES 11 Modifying a View  Modify the EMPVU80 view by using CREATE OR REPLACE VIEW clause.Add an alias for each column name. CREATE OR REPLACE VIEW empvu80 (id_number, name, sal, department_id) AS SELECT employee_id, first_name || ' ' || last_name, salary, department_id WHERE department_id = 80; View created.  Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery. 12 Creating a Complex View Create a complex view that contains group functions to display values from two tables. CREATE VIEW dept_sum_vu (name, minsal, maxsal, avgsal) AS SELECT d.department_name, MIN(e.salary), MAX(e.salary),AVG(e.salary) FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name; View created. 13 Rules for Performing DML Operations on a View  You can perform DML operations on simple views.  You cannot remove a row if the view contains the following:  Group functions  A GROUP BY clause  The DISTINCT keyword  The pseudocolumn ROWNUM keyword 14 Rules for Performing DML Operations on a View You cannot add data through a view if the view includes:  Group functions  A GROUP BY clause  The DISTINCT keyword  The pseudocolumn ROWNUM keyword  Columns defined by expressions  NOT NULL columns in the base tables that are not selected by the view 15 Using the WITH CHECK OPTION Clause  You can ensure that DML operations performed on the view stay within the domain of the view by using the WITH CHECK OPTION clause. CREATE OR REPLACE VIEW empvu20 AS SELECT * FROM employees WHERE department_id = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck ; View created.  Any attempt to change the department number for any row in the view fails because it violates the WITH CHECK OPTION constraint. 16 Denying DML Operations  You can ensure that no DML operations occur by adding the WITH READ ONLY option to your view definition.  Any attempt to perform a DML on any row in the view results in an Oracle server error. 17 Denying DML Operations CREATE OR REPLACE VIEW empvu10 (employee_number, employee_name, job_title) AS SELECT employee_id, last_name, job_id FROM employees WHERE department_id = 10 WITH READ ONLY; View created. 18 Removing a View  Y ou can remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW view; DROP VIEW empvu80; View dropped. 19 Top-N Analysis  Top-N queries ask for the n largest or smallest values of a column.For example  What are the ten best selling products?  What are the ten worst selling products?  Both largest and smallest values sets are consiop-NdT queries. 20 Performing Top-N Analysis The high-level structure of aT op-N analysis query is: SELECT [column_list], ROWNUM FROM (SELECT [column_list] FROM table ORDER BY Top-N_column) WHERE ROWNUM <= N; 21 Example of Top-N Analysis  To display the top three earner names and salaries from the EMPLOYEES table: 1 2 3 SELECT ROWNUM as RANK, last_name, salary FROM (SELECT last_name,salary FROM employees ORDER BY salary DESC) WHERE ROWNUM <= 3; 1 2 3 22 Database Objects Object Description Table Basic unit of storage; composed of rows and columns Logically represents subsets of data from View one or more tables Sequence Generates primary key values Index Improves the performance of some queries Synonym Alternative name for an object 23 What Is a Sequence? A sequence:  Automatically generates unique numbers  Is a sharable object  Is typically used to create a primary key value  Replaces application code  Speeds up the efficiency of accessing sequence values when cached in memory 24 The CREATE SEQUENCE Statement Syntax Define a sequence to generate sequential numbers automatically: CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}]; 25 Creating a Sequence  Create a sequence named DEPT_DEPTID_SEQ to be used for the primary key of the DEPARTMENTS table.  Do not use the CYCLE option. CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE; Sequence created. 26 Confirming Sequences  Verify your sequence values in the USER_SEQUENCES data dictionary table. SELECT sequence_name, min_value, max_value, increment_by, last_number FROM user_sequences;  The LAST_NUMBER column displays the next available sequence number if NOCACHE is specified. 27 NEXTVAL and CURRVAL Pseudocolumns  NEXTVAL returns the next available sequence value.It returns a unique value every time it is referenced,even for different users.  CURRVAL obtains the current sequence value.  NEXTVAL must be issued for that sequence before CURRVAL contains a value. 28 Using a Sequence  Insert a new department named“Support” in location ID 2500. INSERT INTO departments(department_id, department_name, location_id) VALUES (dept_deptid_seq.NEXTVAL, 'Support', 2500); 1 row created.  View the current value for the DEPT_DEPTID_SEQ sequence. SELECT dept_deptid_seq.CURRVAL FROM dual; 29 Using a Sequence  Caching sequence values in memory gives faster access to those values.  Gaps in sequence values can occur when:  A rollback occurs  The system crashes  A sequence is used in another table  If the sequence was created with NOCACHE,view the next available value,by querying the USER_SEQUENCES table. 30 Modifying a Sequence Change the increment value,maximum value,minimum value,cycle option,or cache option. ALTER SEQUENCE dept_deptid_seq INCREMENT BY 20 MAXVALUE 999999 NOCACHE NOCYCLE; Sequence altered. 31 Guidelines for Modifying a Sequence  You must be the owner or have the ALTER privilege for the sequence.  Only future sequence numbers are affected.  The sequence must be dropped and re-created to restart the sequence at a different number.  Some validation is performed. 32 Removing a Sequence  Remove a sequence from the data dictionary by using the DROP SEQUENCE statement.  Once removed,the sequence can no longer be referenced. DROP SEQUENCE dept_deptid_seq; Sequence dropped. 33 What is an Index? An index:  Is a schema object  Is used by the Oracle server to speed up the retrieval of rows by using a pointer  Can reduce disk I/O by using a rapid path access method to locate data quickly  Is independent of the table it indexes  Is used and maintained automatically by the Oracle server 34 How Are Indexes Created?  Automatically:A unique index is created automatically when you define a PRIMARY KEY or UNIQUE constraint in a table definition.  Manually:Users can create nonunique indexes on columns to speed up access to the rows. 35 Creating an Index  Create an index on one or more columns. CREATE INDEX index ON table (column[, column]...);  Improve the speed of query access to the LAST_NAME column in the EMPLOYEES table. CREATE INDEX emp_last_name_idx ON employees(last_name); Index created. 36 When to Create an Index You should create an index if:  A 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 to 4 percent of the rows 37 When Not to Create an Index It is usually not worth creating an index if:  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 to 4 percent of the rows in the table  The table is updated frequently  The indexed columns are referenced as part of an expression 38 Confirming Indexes  The USER_INDEXES data dictionary view contains the name of the index and its uniqueness.  The USER_IND_COLUMNS view contains the index name,the table name,and the column name. SELECT ic.index_name, ic.column_name, ic.column_position col_pos,ix.uniqueness FROM user_indexes ix, user_ind_columns ic WHERE ic.index_name = ix.index_name AND ic.table_name = 'EMPLOYEES'; 39 Function-Based Indexes  A function-based index is an index based on expressions.  The index expression is built from table columns, constants,SQL functions,and user-defined functions. CREATE INDEX upper_dept_name_idx ON departments(UPPER(department_name)); Index created. SELECT * FROM departments WHERE UPPER(department_name) = 'SALES'; 40 Removing an Index  Remove an index from the data dictionary by using the DROP INDEX command. DROP INDEX index;  Remove the UPPER_LAST_NAME_IDX index from the data dictionary. DROP INDEX upper_last_name_idx; Index dropped.  To drop an index,you must be the owner of the index or have the DROP ANY INDEX privilege. 42 Creating and Removing Synonyms  Create a shortened name for the DEPT_SUM_VU view. CREATE SYNONYM d_sum FOR dept_sum_vu; Synonym Created.  Drop a synonym. DROP SYNONYM d_sum; Synonym dropped. 43 THANK YOU SO MUCH 44


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Allison Fischer University of Alabama

"I signed up to be an Elite Notetaker with 2 of my sorority sisters this semester. We just posted our notes weekly and were each making over $600 per month. I LOVE StudySoup!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.