Less06 Schema Tb
Transcript of Less06 Schema Tb
-
8/14/2019 Less06 Schema Tb
1/46
6Copyright 2005, Oracle. All rights reserved.
Managing Schema Objects
-
8/14/2019 Less06 Schema Tb
2/46
6-2 Copyright 2005, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to dothe following: 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 Delete the data in a table Explain the use of temporary tables
-
8/14/2019 Less06 Schema Tb
3/46
6-3 Copyright 2005, Oracle. All rights reserved.
What Is a Schema?
HR schemaHR user
owns
-
8/14/2019 Less06 Schema Tb
4/46
6-4 Copyright 2005, Oracle. All rights reserved.
Schemas
Schemas that created as part of the database creationprocess are: SYS
SYSTEM Sample schemas, optionally
-
8/14/2019 Less06 Schema Tb
5/46
6-5 Copyright 2005, Oracle. All rights reserved.
SchemasFull Notes Page
-
8/14/2019 Less06 Schema Tb
6/46
6-6 Copyright 2005, Oracle. All rights reserved.
Accessing Schema Objects
-
8/14/2019 Less06 Schema Tb
7/466-7 Copyright 2005, Oracle. All rights reserved.
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 128bytes. 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.
-
8/14/2019 Less06 Schema Tb
8/466-8 Copyright 2005, Oracle. All rights reserved.
Naming Database Objects
Nonquoted names can contain only: Alphanumeric characters from your database
character set The underscore (_) Dollar sign ($) Pound sign (#)
No two objects can have the same name within thesame namespace.
-
8/14/2019 Less06 Schema Tb
9/466-9 Copyright 2005, Oracle. All rights reserved.
Schema Object Namespaces
Each of the following have their own namespace:
Indexes
ConstraintsClusters
Database triggers
Private database links
Dimensions
The following are in the samenamespace:TablesViewsSequencesPrivate synonymsStand-alone proceduresStand-alone stored functions
PackagesMaterialized viewsUser-defined types
-
8/14/2019 Less06 Schema Tb
10/466-10 Copyright 2005, Oracle. All rights reserved.
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-lengthcharacter string having a maximum length of size bytes or characters
DATE: Valid date range from January 1, 4712 B.C.through A.D. December 31, 9999
NUMBER( p , s ): Number with precision p and scales
-
8/14/2019 Less06 Schema Tb
11/466-11 Copyright 2005, Oracle. All rights reserved.
Other Data Types
BINARY_FLOAT BINARY_DOUBLE FLOAT INTEGER NCHAR NVARCHAR2 LONG LONG RAW
RAW ROWID UROWID BLOB CLOB NCLOB BFILE TIMESTAMP
-
8/14/2019 Less06 Schema Tb
12/466-12 Copyright 2005, Oracle. All rights reserved.
Other Data TypesFull Notes Page
-
8/14/2019 Less06 Schema Tb
13/466-13 Copyright 2005, Oracle. All rights reserved.
Creating and Modifying Tables
Specify the tablename and schema.
Specify the column names,data types, and lengths.
-
8/14/2019 Less06 Schema Tb
14/466-14 Copyright 2005, Oracle. All rights reserved.
Creating and Modifying TablesFull Notes Page
-
8/14/2019 Less06 Schema Tb
15/466-15 Copyright 2005, Oracle. All rights reserved.
Understanding Data Integrity
JOB_HISTORYEMPLOYEE_ID(PK,FK)START_DATE (PK)END_DATEJOB_ID (FK)DEPARTMENT_ID (FK)
EMPLOYEESEMPLOYEE_ID (PK)FIRST_NAMELAST_NAMEEMAILPHONE_NUMBERHIRE_DATEJOB_ID (FK)SALARYCOMMISION_PCTMANAGER_ID (FK)DEPARTMENT_ID (FK)
DEPARTMENTSDEPARTMENT_ID (PK)DEPARTMENT_NAMEMANAGER_IDLOCATION_ID (FK)
JOBSJOB_ID (PK)JOB_TITLEMIN_SALARYMAX_SALARY
REGIONSREGION_ID (PK)REGION_NAME
COUNTRIESCOUNTRY_ID (PK)COUNTRY_NAMEREGION_ID (FK)
LOCATIONSLOCATION_ID (PK)STREET_ADDRESSPOSTAL_CODECITYSTATE_PROVINCECOUNTRY_ID (FK)
-
8/14/2019 Less06 Schema Tb
16/466-16 Copyright 2005, Oracle. All rights reserved.
Understanding Data IntegrityFull Notes Page
-
8/14/2019 Less06 Schema Tb
17/466-17 Copyright 2005, Oracle. All rights reserved.
Constraint States
ENABLE NOVALIDATE
ENABLE VALIDATE
Existing dataNew data
DISABLE NOVALIDATE
DISABLE VALIDATE
NoDML
-
8/14/2019 Less06 Schema Tb
18/466-18 Copyright 2005, Oracle. All rights reserved.
Constraint States(Notes Only Slide)
-
8/14/2019 Less06 Schema Tb
19/466-19 Copyright 2005, Oracle. All rights reserved.
Defining Constraints
-
8/14/2019 Less06 Schema Tb
20/466-20 Copyright 2005, Oracle. All rights reserved.
Constraint Checking
Case: DML statement, followed by COMMIT
Nondeferred constraintschecked
COMMITissued
Deferred constraints checked
COMMITcomplete
1
3
2
4
Constraints are checked at the time of: Statement execution, for non-deferred constraints COMMIT, for deferred constraints
-
8/14/2019 Less06 Schema Tb
21/466-21 Copyright 2005, Oracle. All rights reserved.
Creating Constraints with SQL: Examples
ALTER TABLE countries ADD (UNIQUE(country_name) ENABLE NOVALIDATE);
ALTER TABLE employees ADD CONSTRAINT pk PRIMARY KEY(employee_id)
CREATE TABLE t1 (pk NUMBER PRIMARY KEY, fk NUMBER, c1 NUMBER,c2 NUMBER,CONSTRAINT ri FOREIGN KEY (fk) REFERENCES t1, CONSTRAINT ck1CHECK (pk > 0 and c1 > 0));
a
c
b
-
8/14/2019 Less06 Schema Tb
22/466-22 Copyright 2005, Oracle. All rights reserved.
Viewing the Columns in a Table
-
8/14/2019 Less06 Schema Tb
23/466-23 Copyright 2005, Oracle. All rights reserved.
Viewing the Contents of a Table
-
8/14/2019 Less06 Schema Tb
24/466-24 Copyright 2005, Oracle. All rights reserved.
Actions with Tables
-
8/14/2019 Less06 Schema Tb
25/466-25 Copyright 2005, Oracle. All rights reserved.
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 referentialintegrity constraints
PURGE: No flashback possible
DROP TABLE hr.employees PURGE;
-
8/14/2019 Less06 Schema Tb
26/466-26 Copyright 2005, Oracle. All rights reserved.
Truncating a Table
Truncating a table makes its row data unavailable,and optionally releases used space.
Corresponding indexes are truncated.
TRUNCATE TABLE hr.employees;
-
8/14/2019 Less06 Schema Tb
27/46
6-27 Copyright 2005, Oracle. All rights reserved.
Indexes
22
22
Index Table
KeyRow
pointer
WHERE key = 22
-
8/14/2019 Less06 Schema Tb
28/46
6-28 Copyright 2005, Oracle. All rights reserved.
Types of Indexes
These are several types of index structures availableto you, depending on the need: A B-tree index is in the form of a binary tree and is
the default index type. A bitmap index has a bitmap for each distinct
value indexed, and each bit position represents arow that may or may not contain the indexedvalue. This is best for low-cardinality columns.
-
8/14/2019 Less06 Schema Tb
29/46
6-29 Copyright 2005, Oracle. All rights reserved.
B-Tree Index
Index entry header Key column length
Key column value
ROWID
Root
Branch
Leaf
Index entry
-
8/14/2019 Less06 Schema Tb
30/46
6-30 Copyright 2005, Oracle. All rights reserved.
-
8/14/2019 Less06 Schema Tb
31/46
6-31 Copyright 2005, Oracle. All rights reserved.
Bitmap Indexes
key
start
ROWID
end
ROWID bitmap
Table
Index
Block 10
Block 11
Block 12
File 3
-
8/14/2019 Less06 Schema Tb
32/46
6-32 Copyright 2005, Oracle. All rights reserved.
-
8/14/2019 Less06 Schema Tb
33/46
6-33 Copyright 2005, Oracle. All rights reserved.
Index Options
A unique index ensures that every indexed valueis unique.
An index can have its key values stored inascending or descending order.
A reverse key index has its key value bytes storedin reverse order.
A function-based index is an index based on afunctions return value.
A compressed index has repeated key valuesremoved.
-
8/14/2019 Less06 Schema Tb
34/46
6-34 Copyright 2005, Oracle. All rights reserved.
Creating Indexes
CREATE INDEX my_index ONemployees(last_name, first_name);
-
8/14/2019 Less06 Schema Tb
35/46
6-35 Copyright 2005, Oracle. All rights reserved.
What Is a View?
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');
COUNTRYtable
LOCATION table
View
-
8/14/2019 Less06 Schema Tb
36/46
6-36 Copyright 2005, Oracle. All rights reserved.
Creating Views
-
8/14/2019 Less06 Schema Tb
37/46
6-37 Copyright 2005, Oracle. All rights reserved.
Sequences
A sequence is a mechanism for automaticallygenerating integers that are guaranteed to be unique.
10011002
10031004
10051006
10071008
1009
10101011
1000000 12
34
56
78
999995999990
999985999980
999975
-
8/14/2019 Less06 Schema Tb
38/46
6-38 Copyright 2005, Oracle. All rights reserved.
Sequences
A sequence has a name, which is how it isreferenced when the next value is requested.
A sequence is not associated with any particular table or column.
The order of progression can be ascending or descending.
The interval between numbers can be of any size. Sequence values can be cached to improve
performance. A sequence can cycle when a limit is reached.
-
8/14/2019 Less06 Schema Tb
39/46
6-39 Copyright 2005, Oracle. All rights reserved.
Creating a Sequence
-
8/14/2019 Less06 Schema Tb
40/46
6-40 Copyright 2005, Oracle. All rights reserved.
What Is a Sequence?Full Notes Page
-
8/14/2019 Less06 Schema Tb
41/46
6-41 Copyright 2005, Oracle. All rights reserved.
Using a Sequence
-
8/14/2019 Less06 Schema Tb
42/46
6-42 Copyright 2005, Oracle. All rights reserved.
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 to all sessions for use without
affecting each others private data
-
8/14/2019 Less06 Schema Tb
43/46
6-43 Copyright 2005, Oracle. All rights reserved.
Temporary Tables(Notes only slide)
-
8/14/2019 Less06 Schema Tb
44/46
6-44 Copyright 2005, Oracle. All rights reserved.
Temporary Tables: Considerations
Use the GLOBAL TEMPORARYclause to createtemporary tables:
Use the TRUNCATE TABLE command to delete thecontents 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;
-
8/14/2019 Less06 Schema Tb
45/46
6-45 Copyright 2005, Oracle. All rights reserved.
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 Explain the usage of types of indexes Create views
Create and use sequences Delete the data in a table in the most efficient
manner Explain the use of temporary tables
-
8/14/2019 Less06 Schema Tb
46/46
Practice Overview:Administering Schema Objects
This practice covers the following topics: Creating tables with columns Creating constraints:
Primary key Check Foreign key
Creating indexes