SlideShare a Scribd company logo
1 of 34
Copyright © 2007, Oracle. All rights reserved.
Managing Schema Objects
Copyright © 2007, Oracle. All rights reserved.
8 - 2
Objectives
After completing this lesson, you should be able to:
• Define schema objects and data types
• Create and modify tables
• Define constraints
• View the columns and contents of a table
• Create indexes
• Create views
• Create sequences
• Explain the use of temporary tables
Copyright © 2007, Oracle. All rights reserved.
8 - 3
What Is a Schema?
HR schema
HR user
owns
> Schema
Constraints
Indexes
Views
Sequences
Temp Tables
Data Dict
Copyright © 2007, Oracle. All rights reserved.
8 - 4
Accessing Schema Objects
Copyright © 2007, Oracle. All rights reserved.
8 - 5
Naming Database Objects
• The length of names must be from 1 to 30 bytes, with
these exceptions:
– Names of databases are limited to 8 bytes.
– Names of database links can be as long as 128 bytes.
• Nonquoted names cannot be Oracle-reserved words.
• Nonquoted names must begin with an alphabetic
character from your database character set.
• Quoted names are not recommended.
Copyright © 2007, Oracle. All rights reserved.
8 - 6
Specifying Data Types in Tables
Common data types:
• CHAR(size [BYTE|CHAR]): Fixed-length character
data of size bytes or characters
• VARCHAR2(size [BYTE|CHAR]): Variable-length
character string having a maximum length of size
bytes or characters
• DATE: Valid date ranging from January 1, 4712 (B.C.),
through December 31, 9999 (A.D.)
• NUMBER(p,s): Number with precision p and
scale s
Copyright © 2007, Oracle. All rights reserved.
8 - 7
Creating and Modifying Tables
Specify the table
name and schema.
Specify the column names,
data types, and lengths.
Copyright © 2007, Oracle. All rights reserved.
8 - 8
Creating and Modifying Tables
CREATE TABLE SHOPOWNER.JOBS (
Job_id NUMBER(5),
Job_title VARCHAR2(30),
MIN_SALARY NUMBER(6),
MAX_SALARY NUMBER(6)
)
TABLESPACE USERS;
ALTER TABLE SHOPOWNER.JOBS add bonus NUMBER(6);
Copyright © 2007, Oracle. All rights reserved.
8 - 9
Understanding Data Integrity
JOB_HISTORY
EMPLOYEE_ID
(PK,FK)
START_DATE (PK)
END_DATE
JOB_ID (FK)
DEPARTMENT_ID (FK)
EMPLOYEES
EMPLOYEE_ID (PK)
FIRST_NAME
LAST_NAME
EMAIL
PHONE_NUMBER
HIRE_DATE
JOB_ID (FK)
SALARY
COMMISION_PCT
MANAGER_ID (FK)
DEPARTMENT_ID (FK)
DEPARTMENTS
DEPARTMENT_ID (PK)
DEPARTMENT_NAME
MANAGER_ID
LOCATION_ID (FK)
JOBS
JOB_ID (PK)
JOB_TITLE
MIN_SALARY
MAX_SALARY
REGIONS
REGION_ID (PK)
REGION_NAME
COUNTRIES
COUNTRY_ID (PK)
COUNTRY_NAME
REGION_ID (FK)
LOCATIONS
LOCATION_ID (PK)
STREET_ADDRESS
POSTAL_CODE
CITY
STATE_PROVINCE
COUNTRY_ID (FK)
Schema
> Constraints
Indexes
Views
Sequences
Temp Tables
Data Dict
Copyright © 2007, Oracle. All rights reserved.
8 - 10
Defining Constraints
Copyright © 2007, Oracle. All rights reserved.
8 - 11
Constraint Violations
Examples of how a constraint can be violated:
• Inserting a duplicate primary key value
• Deleting the parent of a child row in a referential
integrity constraint
• Updating a column to a value that is out of the bounds
of a check constraint
101 …
102 …
103 …
101
X … 22
… 49
… 16
… 5
ID AGE
–30
Copyright © 2007, Oracle. All rights reserved.
8 - 12
Constraint States
ENABLE
NOVALIDATE
ENABLE
VALIDATE
Existing data
New data
DISABLE
NOVALIDATE
DISABLE
VALIDATE
No DML
Copyright © 2007, Oracle. All rights reserved.
8 - 13
Constraint Checking
Constraints are checked at the time of:
• Statement execution (for nondeferred constraints)
• COMMIT (for deferred constraints)
Case: DML statement followed by COMMIT
Nondeferred constraints
checked
COMMIT issued
Deferred constraints checked
COMMIT complete
1
3
2
4
Copyright © 2007, Oracle. All rights reserved.
8 - 14
Creating Constraints with SQL: Examples
ALTER TABLE countries
ADD (UNIQUE(country_name) ENABLE NOVALIDATE);
ALTER TABLE shopowner.jobs ADD CONSTRAINT job_pk PRIMARY
KEY (job_id);
CREATE TABLE emp (emp_no NUMBER PRIMARY KEY,Last_name
VARCHAR2(30), first_name VARCHAR2(30), dept_no NUMBER,
Mgr_no NUMBER, hire_date date,salary NUMBER,
CONSTRAINT Mgr_FK FOREIGN KEY (mgr_no) REFERENCES
emp(emp_no),CONSTRAINT ck1 CHECK (salary > 0));
a
c
b
Copyright © 2007, Oracle. All rights reserved.
8 - 15
Viewing the Columns in a Table
Copyright © 2007, Oracle. All rights reserved.
8 - 16
Viewing the Contents of a Table
Copyright © 2007, Oracle. All rights reserved.
8 - 17
Actions with Tables
Copyright © 2007, Oracle. All rights reserved.
8 - 18
Dropping a Table
Dropping a table removes:
• Data
• Table structure
• Database triggers
• Corresponding indexes
• Associated object privileges
Optional clauses for the DROP TABLE statement:
• CASCADE CONSTRAINTS: Dependent referential integrity
constraints
• PURGE: No flashback possible
DROP TABLE hr.employees PURGE;
Copyright © 2007, Oracle. All rights reserved.
8 - 19
Truncating a Table
• Truncating a table removes the data and releases used
space.
• Corresponding indexes are truncated.
TRUNCATE TABLE hr.employees;
Copyright © 2007, Oracle. All rights reserved.
8 - 20
Indexes
22
22
Index Table
Key
Row
pointer
… WHERE key = 22
Schema
Constraints
> Indexes
Views
Sequences
Temp Tables
Data Dict
Copyright © 2007, Oracle. All rights reserved.
8 - 21
Types of Indexes
These are several types of index structures that are
available depending on your needs. Two of the most
common are:
• B-tree index
– Default index type; in the form of a balanced tree
• Bitmap index:
– Has a bitmap for each distinct value indexed
– Each bit position represents a row that may or may not
contain the indexed value.
– Best for low-cardinality columns
Copyright © 2007, Oracle. All rights reserved.
8 - 22
B-Tree Index
Index entry header
Key column length
Key column value
ROWID
Root
Branch
Leaf
Index entry
Copyright © 2007, Oracle. All rights reserved.
8 - 24
Bitmap Indexes
<Blue, 10.0.3, 12.8.3, 1000100100010010100>
<Green, 10.0.3, 12.8.3, 0001010000100100000>
<Red, 10.0.3, 12.8.3, 0100000011000001001>
<Yellow, 10.0.3, 12.8.3, 0010001000001000010>
Key
Start
ROWID
End
ROWID Bitmap
Table
Index
Block 10
Block 11
Block 12
File 3
Copyright © 2007, Oracle. All rights reserved.
8 - 26
Index Options
• Unique index: Ensures that every indexed value is
unique
• Reverse key index: Has its key value bytes stored in
reverse order
• Composite index: Is based on more than one column
• Function-based index: Is based on a function’s return
value
• Compressed index: Has repeated key values removed
• Order: An index can have its key values stored in
ascending or descending order.
Copyright © 2007, Oracle. All rights reserved.
8 - 28
Creating Indexes
CREATE INDEX my_index ON
employees(last_name, first_name);
Copyright © 2007, Oracle. All rights reserved.
8 - 29
Views
CREATE VIEW v AS SELECT location_id, country_name FROM
locations l, countries c
WHERE l.country_id = c.country_id AND c.country_id in
('AU','BR');
COUNTRY table
LOCATION table
View
Schema
Constraints
Indexes
> Views
…
Copyright © 2007, Oracle. All rights reserved.
8 - 30
Creating Views
Copyright © 2007, Oracle. All rights reserved.
8 - 31
Sequences
A sequence is a mechanism for automatically
generating integers that follow a pattern.
• A sequence has a name, which is
how it is referenced when the next
value is requested.
• A sequence is not associated with
any particular table or column.
• The progression can be ascending or
descending.
• The interval between numbers can be of any size.
• A sequence can cycle when a limit is reached.
1
2
3
4
5
Schema
Constraints
Indexes
Views
> Sequences
Temp Tables
Data Dict
Copyright © 2007, Oracle. All rights reserved.
8 - 32
Creating a Sequence
Copyright © 2007, Oracle. All rights reserved.
8 - 34
Using a Sequence
SQL> CREATE TABLE orders
(id NUMBER,
ord_date DATE,
prod_id NUMBER,
prod_desc VARCHAR2(30)
);
Table created.
SQL> INSERT INTO orders VALUES ( abc_seq.NEXTVAL,
sysdate, 1245009, 'Gizmo X');
1 row created.
Copyright © 2007, Oracle. All rights reserved.
8 - 35
Temporary Tables
A temporary table:
• Provides storage of data that is automatically cleaned
up when the session or transaction ends
• Provides private storage of data for each session
• Is available for use to all sessions without affecting the
private data of each session
Schema
Constraints
Indexes
Views
Sequences
> Temp Tables
Data Dict
Copyright © 2007, Oracle. All rights reserved.
8 - 36
Temporary Tables: Considerations
• Use the GLOBAL TEMPORARY clause to create temporary
tables:
• Use the TRUNCATE TABLE command to delete the
contents of the table.
• You can create the following on temporary tables:
– Indexes
– Views
– Triggers
CREATE GLOBAL TEMPORARY TABLE employees_temp
ON COMMIT PRESERVE ROWS
AS SELECT * FROM employees;
Copyright © 2007, Oracle. All rights reserved.
8 - 37
Summary
In this lesson, you should have learned how to:
• Define schema objects and data types
• Create and modify tables
• Define constraints
• View the columns and contents of a table
• Create indexes
• Create views
• Create sequences
• Explain the use of temporary tables
Copyright © 2007, Oracle. All rights reserved.
8 - 38
Practice 8 Overview:
Administering Schema Objects
This practice covers the following topics:
• Creating tables with columns
• Creating constraints:
– PRIMARY KEY
– FOREIGN KEY
– CHECK
• Creating indexes

More Related Content

Similar to Less08_Schema Advanced Databases and Management.pptx

DDL. data defination language for creating database
DDL. data defination language for creating databaseDDL. data defination language for creating database
DDL. data defination language for creating databaseSHAKIR325211
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tablessiavosh kaviani
 
Dd and atomic ddl pl17 dublin
Dd and atomic ddl pl17 dublinDd and atomic ddl pl17 dublin
Dd and atomic ddl pl17 dublinStåle Deraas
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...rehaniltifat
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objectssiavosh kaviani
 
Oracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the IndicesOracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the IndicesKellyn Pot'Vin-Gorman
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowPavithSingh
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadatarehaniltifat
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuningAnil Pandey
 

Similar to Less08_Schema Advanced Databases and Management.pptx (20)

DDL. data defination language for creating database
DDL. data defination language for creating databaseDDL. data defination language for creating database
DDL. data defination language for creating database
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
Module 3
Module 3Module 3
Module 3
 
chap13.ppt
chap13.pptchap13.ppt
chap13.ppt
 
Database Objects
Database ObjectsDatabase Objects
Database Objects
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Less08 Schema
Less08 SchemaLess08 Schema
Less08 Schema
 
Dd and atomic ddl pl17 dublin
Dd and atomic ddl pl17 dublinDd and atomic ddl pl17 dublin
Dd and atomic ddl pl17 dublin
 
Lab
LabLab
Lab
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Sql oracle
Sql oracleSql oracle
Sql oracle
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
Les11.ppt
Les11.pptLes11.ppt
Les11.ppt
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
 
Les10
Les10Les10
Les10
 
Oracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the IndicesOracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the Indices
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuning
 

Recently uploaded

Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

Less08_Schema Advanced Databases and Management.pptx

  • 1. Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects
  • 2. Copyright © 2007, Oracle. All rights reserved. 8 - 2 Objectives After completing this lesson, you should be able to: • Define schema objects and data types • Create and modify tables • Define constraints • View the columns and contents of a table • Create indexes • Create views • Create sequences • Explain the use of temporary tables
  • 3. Copyright © 2007, Oracle. All rights reserved. 8 - 3 What Is a Schema? HR schema HR user owns > Schema Constraints Indexes Views Sequences Temp Tables Data Dict
  • 4. Copyright © 2007, Oracle. All rights reserved. 8 - 4 Accessing Schema Objects
  • 5. Copyright © 2007, Oracle. All rights reserved. 8 - 5 Naming Database Objects • The length of names must be from 1 to 30 bytes, with these exceptions: – Names of databases are limited to 8 bytes. – Names of database links can be as long as 128 bytes. • Nonquoted names cannot be Oracle-reserved words. • Nonquoted names must begin with an alphabetic character from your database character set. • Quoted names are not recommended.
  • 6. Copyright © 2007, Oracle. All rights reserved. 8 - 6 Specifying Data Types in Tables Common data types: • CHAR(size [BYTE|CHAR]): Fixed-length character data of size bytes or characters • VARCHAR2(size [BYTE|CHAR]): Variable-length character string having a maximum length of size bytes or characters • DATE: Valid date ranging from January 1, 4712 (B.C.), through December 31, 9999 (A.D.) • NUMBER(p,s): Number with precision p and scale s
  • 7. Copyright © 2007, Oracle. All rights reserved. 8 - 7 Creating and Modifying Tables Specify the table name and schema. Specify the column names, data types, and lengths.
  • 8. Copyright © 2007, Oracle. All rights reserved. 8 - 8 Creating and Modifying Tables CREATE TABLE SHOPOWNER.JOBS ( Job_id NUMBER(5), Job_title VARCHAR2(30), MIN_SALARY NUMBER(6), MAX_SALARY NUMBER(6) ) TABLESPACE USERS; ALTER TABLE SHOPOWNER.JOBS add bonus NUMBER(6);
  • 9. Copyright © 2007, Oracle. All rights reserved. 8 - 9 Understanding Data Integrity JOB_HISTORY EMPLOYEE_ID (PK,FK) START_DATE (PK) END_DATE JOB_ID (FK) DEPARTMENT_ID (FK) EMPLOYEES EMPLOYEE_ID (PK) FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID (FK) SALARY COMMISION_PCT MANAGER_ID (FK) DEPARTMENT_ID (FK) DEPARTMENTS DEPARTMENT_ID (PK) DEPARTMENT_NAME MANAGER_ID LOCATION_ID (FK) JOBS JOB_ID (PK) JOB_TITLE MIN_SALARY MAX_SALARY REGIONS REGION_ID (PK) REGION_NAME COUNTRIES COUNTRY_ID (PK) COUNTRY_NAME REGION_ID (FK) LOCATIONS LOCATION_ID (PK) STREET_ADDRESS POSTAL_CODE CITY STATE_PROVINCE COUNTRY_ID (FK) Schema > Constraints Indexes Views Sequences Temp Tables Data Dict
  • 10. Copyright © 2007, Oracle. All rights reserved. 8 - 10 Defining Constraints
  • 11. Copyright © 2007, Oracle. All rights reserved. 8 - 11 Constraint Violations Examples of how a constraint can be violated: • Inserting a duplicate primary key value • Deleting the parent of a child row in a referential integrity constraint • Updating a column to a value that is out of the bounds of a check constraint 101 … 102 … 103 … 101 X … 22 … 49 … 16 … 5 ID AGE –30
  • 12. Copyright © 2007, Oracle. All rights reserved. 8 - 12 Constraint States ENABLE NOVALIDATE ENABLE VALIDATE Existing data New data DISABLE NOVALIDATE DISABLE VALIDATE No DML
  • 13. Copyright © 2007, Oracle. All rights reserved. 8 - 13 Constraint Checking Constraints are checked at the time of: • Statement execution (for nondeferred constraints) • COMMIT (for deferred constraints) Case: DML statement followed by COMMIT Nondeferred constraints checked COMMIT issued Deferred constraints checked COMMIT complete 1 3 2 4
  • 14. Copyright © 2007, Oracle. All rights reserved. 8 - 14 Creating Constraints with SQL: Examples ALTER TABLE countries ADD (UNIQUE(country_name) ENABLE NOVALIDATE); ALTER TABLE shopowner.jobs ADD CONSTRAINT job_pk PRIMARY KEY (job_id); CREATE TABLE emp (emp_no NUMBER PRIMARY KEY,Last_name VARCHAR2(30), first_name VARCHAR2(30), dept_no NUMBER, Mgr_no NUMBER, hire_date date,salary NUMBER, CONSTRAINT Mgr_FK FOREIGN KEY (mgr_no) REFERENCES emp(emp_no),CONSTRAINT ck1 CHECK (salary > 0)); a c b
  • 15. Copyright © 2007, Oracle. All rights reserved. 8 - 15 Viewing the Columns in a Table
  • 16. Copyright © 2007, Oracle. All rights reserved. 8 - 16 Viewing the Contents of a Table
  • 17. Copyright © 2007, Oracle. All rights reserved. 8 - 17 Actions with Tables
  • 18. Copyright © 2007, Oracle. All rights reserved. 8 - 18 Dropping a Table Dropping a table removes: • Data • Table structure • Database triggers • Corresponding indexes • Associated object privileges Optional clauses for the DROP TABLE statement: • CASCADE CONSTRAINTS: Dependent referential integrity constraints • PURGE: No flashback possible DROP TABLE hr.employees PURGE;
  • 19. Copyright © 2007, Oracle. All rights reserved. 8 - 19 Truncating a Table • Truncating a table removes the data and releases used space. • Corresponding indexes are truncated. TRUNCATE TABLE hr.employees;
  • 20. Copyright © 2007, Oracle. All rights reserved. 8 - 20 Indexes 22 22 Index Table Key Row pointer … WHERE key = 22 Schema Constraints > Indexes Views Sequences Temp Tables Data Dict
  • 21. Copyright © 2007, Oracle. All rights reserved. 8 - 21 Types of Indexes These are several types of index structures that are available depending on your needs. Two of the most common are: • B-tree index – Default index type; in the form of a balanced tree • Bitmap index: – Has a bitmap for each distinct value indexed – Each bit position represents a row that may or may not contain the indexed value. – Best for low-cardinality columns
  • 22. Copyright © 2007, Oracle. All rights reserved. 8 - 22 B-Tree Index Index entry header Key column length Key column value ROWID Root Branch Leaf Index entry
  • 23. Copyright © 2007, Oracle. All rights reserved. 8 - 24 Bitmap Indexes <Blue, 10.0.3, 12.8.3, 1000100100010010100> <Green, 10.0.3, 12.8.3, 0001010000100100000> <Red, 10.0.3, 12.8.3, 0100000011000001001> <Yellow, 10.0.3, 12.8.3, 0010001000001000010> Key Start ROWID End ROWID Bitmap Table Index Block 10 Block 11 Block 12 File 3
  • 24. Copyright © 2007, Oracle. All rights reserved. 8 - 26 Index Options • Unique index: Ensures that every indexed value is unique • Reverse key index: Has its key value bytes stored in reverse order • Composite index: Is based on more than one column • Function-based index: Is based on a function’s return value • Compressed index: Has repeated key values removed • Order: An index can have its key values stored in ascending or descending order.
  • 25. Copyright © 2007, Oracle. All rights reserved. 8 - 28 Creating Indexes CREATE INDEX my_index ON employees(last_name, first_name);
  • 26. Copyright © 2007, Oracle. All rights reserved. 8 - 29 Views CREATE VIEW v AS SELECT location_id, country_name FROM locations l, countries c WHERE l.country_id = c.country_id AND c.country_id in ('AU','BR'); COUNTRY table LOCATION table View Schema Constraints Indexes > Views …
  • 27. Copyright © 2007, Oracle. All rights reserved. 8 - 30 Creating Views
  • 28. Copyright © 2007, Oracle. All rights reserved. 8 - 31 Sequences A sequence is a mechanism for automatically generating integers that follow a pattern. • A sequence has a name, which is how it is referenced when the next value is requested. • A sequence is not associated with any particular table or column. • The progression can be ascending or descending. • The interval between numbers can be of any size. • A sequence can cycle when a limit is reached. 1 2 3 4 5 Schema Constraints Indexes Views > Sequences Temp Tables Data Dict
  • 29. Copyright © 2007, Oracle. All rights reserved. 8 - 32 Creating a Sequence
  • 30. Copyright © 2007, Oracle. All rights reserved. 8 - 34 Using a Sequence SQL> CREATE TABLE orders (id NUMBER, ord_date DATE, prod_id NUMBER, prod_desc VARCHAR2(30) ); Table created. SQL> INSERT INTO orders VALUES ( abc_seq.NEXTVAL, sysdate, 1245009, 'Gizmo X'); 1 row created.
  • 31. Copyright © 2007, Oracle. All rights reserved. 8 - 35 Temporary Tables A temporary table: • Provides storage of data that is automatically cleaned up when the session or transaction ends • Provides private storage of data for each session • Is available for use to all sessions without affecting the private data of each session Schema Constraints Indexes Views Sequences > Temp Tables Data Dict
  • 32. Copyright © 2007, Oracle. All rights reserved. 8 - 36 Temporary Tables: Considerations • Use the GLOBAL TEMPORARY clause to create temporary tables: • Use the TRUNCATE TABLE command to delete the contents of the table. • You can create the following on temporary tables: – Indexes – Views – Triggers CREATE GLOBAL TEMPORARY TABLE employees_temp ON COMMIT PRESERVE ROWS AS SELECT * FROM employees;
  • 33. Copyright © 2007, Oracle. All rights reserved. 8 - 37 Summary In this lesson, you should have learned how to: • Define schema objects and data types • Create and modify tables • Define constraints • View the columns and contents of a table • Create indexes • Create views • Create sequences • Explain the use of temporary tables
  • 34. Copyright © 2007, Oracle. All rights reserved. 8 - 38 Practice 8 Overview: Administering Schema Objects This practice covers the following topics: • Creating tables with columns • Creating constraints: – PRIMARY KEY – FOREIGN KEY – CHECK • Creating indexes