Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
CIS183 Oracle SQL Programming
-
Upload
studyserious -
Category
Documents
-
view
62 -
download
0
Transcript of CIS183 Oracle SQL Programming
![Page 1: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/1.jpg)
CIS183: Oracle SQL Programming
Instructor: Rebecca GottliebLecture 1: 2/1/11
![Page 2: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/2.jpg)
Copyright 2011 Rebecca Gottlieb
Agenda
• Quiz #0 and Introductions• Syllabus• Class Website – Oracle Website• Lab Development Environment Handout• Introduction to Databases!• Data Modeling Review• In-class Assignment• Homework
![Page 3: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/3.jpg)
Copyright 2011 Rebecca Gottlieb
Job Descriptions
• Business Analyst• Programmer/Developer• Database Administrator• Project Manager• IT Manager
![Page 4: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/4.jpg)
Copyright 2011 Rebecca Gottlieb
Database Components
• What is a database? Data? Information?
• Tables with rows and columns• Record• Organization: Relational, Object Relational
• Primary Key• Foreign Key
![Page 5: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/5.jpg)
Copyright 2011 Rebecca Gottlieb
Types of Databases: Implementation, Size,
Content• Single-user database (packaged
software)• Multi-user database• E-Commerce• OLTP, OLAP, ETL,• MRP, CRM, ERP• Reporting, Data Warehouse, Data
Mining databases
![Page 6: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/6.jpg)
Copyright 2011 Rebecca Gottlieb
Application Life Cycle
• Requirements gathering• Database design (Logical/Physical/App
design)• DBMS Creation• Implementation• Data loading• Testing• Operational Maintenance
![Page 7: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/7.jpg)
Copyright 2011 Rebecca Gottlieb
DBMS: Components
• Architecture• Users -> application -> DBMS -> db
• Application Functions• Display, process queries, reports, apply
business rules
• DBMS Functions• information, DBA activities, utilities,
concurrency
• Database – physical, hardware, RI constraints
![Page 8: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/8.jpg)
Copyright 2011 Rebecca Gottlieb
Application Types
• Client/Server• eCommerce – web/app server• Reporting applications
![Page 9: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/9.jpg)
Copyright 2011 Rebecca Gottlieb
Database
• Metadata• Data w/Relationships• Indexes• Code• SQL, PL/SQL• Schemas, Users• Hardware (the physical
representation)
![Page 10: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/10.jpg)
Copyright 2011 Rebecca Gottlieb
Database Design
• From existing data • From existing database/warehouse/mart• New system development• Database Design – definition?
• Analyze the data and business requirements• Create data model using normalization• Create physical database
• Redesign• Migration to new data model• Database consolidation
![Page 11: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/11.jpg)
Copyright 2011 Rebecca Gottlieb
BAD Database Design
• Forces developers to write complicated code
• Stores duplicate data• Over-denormalization• Poorly written SQL – slow response time• Sizing issues• Data model is unable to accommodate
changes to new requirements
![Page 12: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/12.jpg)
Relational Database Concept
• Dr. E. F. Codd proposed the relational model for database systems in 1970.
• It is the basis for the relational database management system (RDBMS).
• The relational model consists of the following:• Collection of objects or relations• Set of operators to act on the relations• Data integrity for accuracy and consistency
Copyright 2011 Rebecca Gottlieb
![Page 13: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/13.jpg)
RDBMS Definition
A relational database is a collection of relations ortwo-dimensional tables.
Copyright 2011 Rebecca Gottlieb
Oracleserver
Table name: EMPLOYEES Table name: DEPARTMENTS
… …
![Page 14: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/14.jpg)
Data Models
Copyright 2011 Rebecca Gottlieb
Model ofsystemin client’smind
Entity model ofclient’s model
Tables on disk
Oracleserver
Table modelof entity model
![Page 15: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/15.jpg)
Identifying Entities
• Review the business requirements and evaluate each noun to see if it could be an entity
• Identify what information about each entity is to be stored. Give each piece of information (attribute) a name (column).
• Determine if each instance of an entity is uniquely identifiable and mark those attributes(s)
• Write a short description of each entity• Draw a diagram of each entity and its attributes
Copyright 2009 Rebecca Gottlieb
![Page 16: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/16.jpg)
Copyright 2011 Rebecca Gottlieb
• Entity:• Singular, unique
name• Uppercase• Soft box• Synonym in
parenthesesEMPLOYEE#* number* nameo job title
DEPARTMENT#* number* nameo location
assigned to
composed of
Attribute:• Singular name• Lowercase• Mandatory marked with
“*”• Optional marked with
“o”
Unique Identifier (UID)Primary marked with “#”Secondary marked with “(#)”
![Page 17: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/17.jpg)
Identify Entities: Faber College
• Faber college is divided into several schools: a school of business, a school of arts and sciences, a school of education, and a school of applied sciences. Each school is located at a specific area of campus and is run by a dean.
• A school can not share a dean with another school. Each dean has a name, office number, and phone number.
• Each school offers classes to its students. Each class may be taught in its own school, or at a location in another school on campus. More than one school can offer the same class.
• A dean may teach a class.Copyright 2009 Rebecca
Gottlieb
![Page 18: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/18.jpg)
Relationships
• A relationship is a significant association between one (recursive) or more entities.
• Recursive relationships follow the same guidelines as non-recursive
• They are the “verbs” of a diagram• Relationships are bi-directional. If there is a relationship
between entity 1 and 2 there must be a relationship between entity 2 and 1
• Relationships are noted with a diamond
Copyright 2009 Rebecca Gottlieb
EMPLOYEE
PAYCHECK
receives
supervises
![Page 19: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/19.jpg)
Cardinality
• Maximum: largest amount of instances that can participate in a relationship
• Minimum: least amount of instances that can participate in a relationship
• Sets the upper and lower bounds• A class has at least 18 students and a maximum of 40
• 1:1 (one to one) relationship• 1:M (one to many) relationship
• The position of the 1 and the M are important. They refer to the entities nearest to them in the diagram
• 1: parent entity, M: child entity• M is the upper bound (max cardinality) for the relationship
• M:M or M:N (many to many) relationship• Relationships are optional if the lower bound is 0• Relationships are mandatory if the lower bound is 1
Copyright 2009 Rebecca Gottlieb
![Page 20: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/20.jpg)
Relating Multiple Tables
Copyright 2011 Rebecca Gottlieb
• Each row of data in a table is uniquely identified by a primary key.
• You can logically relate data from multiple tables using foreign keys.
Table name: EMPLOYEES
Primary key Primary keyForeign key…
Table name: DEPARTMENTS
![Page 21: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/21.jpg)
Terminology
• Row• Column• Primary Key• Row w/o Key designation• Foreign Key• Field with and without data (NULL)
Copyright 2011 Rebecca Gottlieb
![Page 22: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/22.jpg)
Attributes
• Attributes provide a piece of information about an entity• Attributes should be assigned to only one entity• Aggregate attributes should be broken down into simple
attributes (e.g. a name should be First Name and Last Name)• Each attribute should have a single value for each instance
of an entity• Attributes whose value is derived or calculated from the
values of other attributes should not be included in the ER Model
• Attributes do not have attributes of their own
What is wrong with a key like this: VHSUSA12345Where VHS, USA, and the number are different codes?
Copyright 2009 Rebecca Gottlieb
![Page 23: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/23.jpg)
Attributes cont.
• Attributes are determined from the business information requirements
• The attribute names should be easily understood• Names should be singular• Names should be specific: hire_date not h_date• Name should not be an entity name with the exception
of a PK or FK reference• Attribute names are listed in the box of their entity in
mixed upper case and lower case characters• Determine if each attribute is unique, not null or has a
default value• #=unique, *=not null
Copyright 2009 Rebecca Gottlieb
![Page 24: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/24.jpg)
HR Schema Details
• HR Schema ERDhttp://download.oracle.com/docs/cd/E11882_01/server.112/e16508/tablecls.htm#CBBJICEB
• HR Schema scripts:$ORACLE_HOME/demo/schema folder
Copyright 2011 Rebecca Gottlieb
![Page 25: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/25.jpg)
Database User HR• This is the schema that is used in this course. In the Human Resource (HR)
records, each employee has an identification number, email address, job identification code, salary, and manager. Some employees earn commissions in addition to their salary.
• The company also tracks information about jobs within the organization. Each job has an identification code, job title, and a minimum and maximum salary range for the job. Some employees have been with the company for a long time and have held different positions within the company. When an employee resigns, the duration the employee was working for, the job identification number, and the department are recorded.
• The sample company is regionally diverse, so it tracks the locations of its warehouses and departments. Each employee is assigned to a department, and each department is identified either by a unique department number or a short name. Each department is associated with one location, and each location has a full address that includes the street name, postal code, city, state or province, and the country code.
• In places where the departments and warehouses are located, the company records details such as the country name, currency symbol, currency name, and the region where the country is located geographically.
Copyright 2009 Rebecca Gottlieb
![Page 26: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/26.jpg)
In Class Assignment
ABC company sells a variety of products. For each sale, an invoice is written up by a sales representative. Each sales representative may write many invoices, and is solely responsible for each invoice that he/she writes. The information stored on each invoice includes the date, invoice amount, customer name, and shipping address. An invoice may only be written for a single customer, and each customer may have many invoices. The invoice may contain many detail lines that describe each product purchased by the customer. The information stored on each detail line includes product description, quantity, price per item, and the total price of each line. ABC company maintains its inventory information for each product that it carries. It also maintains information on each vendor for those products. The information stored on each product includes product description, product price, product code and quantity on hand.
Copyright 2009 Rebecca Gottlieb
![Page 27: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/27.jpg)
Using SQL to Query Your Database• Structured query language (SQL) is:
• The ANSI standard language for operating relational databases
• Efficient, easy to learn, and use• Functionally complete (With SQL, you can define,
retrieve, and manipulate data in the tables.)
SELECT department_name FROM departments;
Oracleserver
![Page 28: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/28.jpg)
Copyright 2011 Rebecca Gottlieb
• • SELECT • INSERT• UPDATE• DELETE• MERGE
• CREATE• ALTER• DROP• RENAME• TRUNCATE• COMMENT
• GRANT• REVOKE
• COMMIT• ROLLBACK• SAVEPOINT
Data manipulation language (DML)
Data definition language (DDL)
Transaction control
Data control language (DCL)
![Page 29: CIS183 Oracle SQL Programming](https://reader033.fdocuments.in/reader033/viewer/2022061615/5536a6164a79595b138b4ae5/html5/thumbnails/29.jpg)
See Oracle Appendix D
Copyright 2011 Rebecca Gottlieb