SQL's Data Definition Language (DDL) – View, Sequence, Index.

Slides:



Advertisements
Similar presentations
SQL2-ch2 管理綱要物件.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
System Administration Accounts privileges, users and roles
View Sen Zhang. Views are very common in business systems users view of data is simplified a form of security - user sees only the data he/she needs to.
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.
CSC 2720 Building Web Applications Database and SQL.
INTEGRITY Enforcing integrity in Oracle. Oracle Tables mrobbert owner granted access.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
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.
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.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
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.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
DataBase Management System What is DBMS Purpose of DBMS Data Abstraction Data Definition Language Data Manipulation Language Data Models Data Keys Relationships.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
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.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
© 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.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Session 1 Module 1: Introduction to Data Integrity
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.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
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.
Chapter 3: Relational Databases
11-1 © Prentice Hall, 2004 Chapter 11: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
SQL Basics Review Reviewing what we’ve learned so far…….
Creating Indexes Database Systems Objectives Distinguish between the indexes that are created automatically and those that are created manually.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Writing Correlated Subqueries
Creating Other Schema Objects
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 5 Sequences.
Chapter 4 Indexes.
CH 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
Database Management System
Presentation transcript:

SQL's Data Definition Language (DDL) – View, Sequence, Index

DDL: View, Index, Sequence, Synonym n Objects are defined using the CREATE statement n Some objects can be modified using the ALTER statement n Some objects cannot be modified - they must be dropped and then re-created n Objects are removed using the DROP statement

View n A view is a virtual table – no data is actually maintained for a view n A view is defined based on existing tables by using a SELECT statement: CREATE VIEW viewname AS SELECT … n When a view is referenced the view's defining query is executed and data is retrieved from tables referenced by the defining query n The defining query for a view is stored in the data dictionary

View Example CREATE VIEW management AS SELECT Emp_no, ename FROM employees WHERE emp_no IN (SELECT mgr FROM employees WHERE mgr IS NOT NULL); n Using a view in a query causes the SELECT statement that defines the view to be executed and data values retrieved eg SELECT ename FROM management;

View Example 2 n Views are often used to simplify writing of complex queries which involve multiple tables or group functions Eg: CREATE VIEW deptsummary AS SELECT d.deptno, dept_name, sum(sal) Salaries FROM departments d, employees e WHERE d.dept_no = e.dept_no(+) GROUP BY d.dept_no, dept_name; SELECT * FROM deptsummary WHERE Salaries > 5000;

View Example 3 n Views are also used to permit access to only certain rows and/or columns in a table to some user(s) and not permit access to the remaining columns and/or rows Eg: CREATE VIEW emplist AS SELECT emp_no, ename, fname, job FROM employees WHERE LOWER(job)!='president' ORDER BY ename, fname; SELECT * FROM emplist;

Simple and Complex Views n A simple view is based upon a query that involves only a single table and uses no grouping; any other view is referred to a complex view n Data can be modified using INSERT, UPDATE and DELETE statements applied to a simple view but not to a complex view eg: UPDATE emplist SET job = 'Assistant' WHERE job = 'Clerk';

Modifying Data using a View n Data can be modified using INSERT, UPDATE and DELETE statements on a simple view but this is not normally done n A simple view defined using a WITH READ ONLY clause will not permit data to be modified eg CREATE VIEW emplist2 AS SELECT emp_no, ename, fname, job FROM employees WHERE LOWER(job)!='president' ORDER BY ename, fname WITH READ ONLY;

Change View n Change definition of a view by using the OR REPLACE clause in the CREATE VIEW statement eg CREATE OR REPLACE VIEW emplist AS SELECT emp_no, ename, fname, job FROM employees ORDER BY ename, fname;

Indexes n Indexes store the value of a column(s) being indexed as well as pointer(s) to the physical location of the row(s) having this value n Most are implemented using a B*Tree n Indexes are used for 2 purposes: improving performance of queries and enforcing uniqueness of column values n CREATE INDEX indexname ON tablename(col1name,col2name,…)

Indexing and Performance n Indexes are often created to improve performance of queries n However indexes slow performance of INSERT, UPDATE and DELETE operations because the index must be updated also n Be aware of trade-off between overhead involved and usefulness of index n DBMS decides when an index should be used to retrieve data – programmer cannot reference an index in a query

Indexes n Index can be used to enforce uniqueness on column(s) in a table n PRIMARY KEY and UNIQUE constraints are actually implemented by Oracle DBMS defining a UNIQUE index n CREATE UNIQUE INDEX indexname ON tablename(col1name,…) eg CREATE UNIQUE INDEX SINIndex ON STUDENT(SIN)

Composite Indexes n If defining an index on a composite field, the order of the columns is significant : list the most commonly queried column first so that the index can be used for this single column or for the combined columns eg CREATE INDEX SectionIndex ON SECTION (subjcode,sectcode) n SectionIndex can be used to search on subjcode alone or subjcode and sectcode combined – but is not used to search on sectcode alone

Common Reasons to Define an Index on Large Tables n Column(s) used frequently in joins – therefore index defined on most foreign key fields n Column(s) used frequently in clauses (WHERE, GROUP BY, ORDER BY) n Columns with a wide distribution of values so index entries correspond to a limited number of rows n Column with large occurrence of NULL values since NULL values are not indexed

Sequence n A sequence is an object which is used to generate sequential values which are often assigned to primary keys n A sequence is not physically linked with a table or column n It is the responsibility of the developer to only use a sequence with the primary key or column that it was defined for n CREATE SEQUENCE seqname INCREMENT BY n START WITH n

Sequence(ctd) Eg CREATE SEQUENCE emp_no_seq START AT 1000 INCREMENT BY 1 n A sequence has two pseudo-columns: - NEXTVAL : gets the next sequential value of the sequence - CURRVAL : gets the current value of the sequence Eg INSERT INTO employees VALUES(emp_no_seq.nextval, …) Eg SELECT emp_no_seq.currval FROM dual

Synonym n A synonym allows you to associate an alternate name with an object such as a table or view Eg CREATE SYNONYM emp FOR employees; SELECT * FROM emp;

Remove Object from Data Dictionary n Remove object from data dictionary DROP VIEW viewname DROP SEQUENCE sequencename DROP INDEX indexname DROP SYNONYM synonymname