Anúncio
Anúncio

Mais conteúdo relacionado

Anúncio

Manage schema object.ppt

  1. 7-1 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects
  2. 7-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Define schema objects and data types • Create and modify tables • Define constraints • User • User accounts • Create indexes • Naming Schema Object • Object Namespaces • Explain the use of temporary tables
  3. 7-3 Copyright © 2005, Oracle. All rights reserved. Database Schema  A database schema defines how data is organized within a relational database.  This is inclusive of logical constraints such as, table names, fields, data types, and the relationships between these entities.
  4. 7-4 Copyright © 2005, Oracle. All rights reserved. What Is a Schema? HR schema HR user owns > Schema Constraints Indexes Views Temp Tables
  5. 7-5 Copyright © 2005, Oracle. All rights reserved. Schema Objects
  6. 7-6 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 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.
  7. 7-7 Copyright © 2005, Oracle. All rights reserved. Object Namespaces  A Namespace is a container for a set of identifiers(symbols or names).  A namespace defines a group of object types, within which all names must be uniquely identified, by schema and name. Objects in different namespaces can share the same name. These object types all share the same namespace:  Tables  Views  Sequences  Private synonyms
  8. 7-8 Copyright © 2005, Oracle. All rights reserved. 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 A.D. December 31, 9999 • NUMBER(p,s): Number with precision p and scale s
  9. 7-9 Copyright © 2005, Oracle. All rights reserved. Creating and Modifying Tables Specify the table name and schema. Specify the column names, data types, and lengths.
  10. 7-10 Copyright © 2005, Oracle. All rights reserved. DBA User  A database administrator is a database user of the database user class DBA.  Database administrators can manage database users and database objects.  The database system administrator is a special database user of database user class SYSDBA (administrator).
  11. 7-11 Copyright © 2005, Oracle. All rights reserved. User Accounts  For users to access your database, you must create user accounts and grant appropriate database access privileges to those accounts.  A user account is identified by a user name and defines the attributes of the user, including the following:  Authentication method  Password for database authentication  Default tablespaces for permanent and temporary data storage  Tablespace quotas  Account status (locked or unlocked)  Password status (expired or not)
  12. 7-12 Copyright © 2005, Oracle. All rights reserved. Defining Constraints
  13. 7-13 Copyright © 2005, Oracle. All rights reserved. Constraint Violations Examples of how a constraint can be violated are: • 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
  14. 7-14 Copyright © 2005, Oracle. All rights reserved. Constraint States ENABLE NOVALIDATE ENABLE VALIDATE Existing data New data DISABLE NOVALIDATE DISABLE VALIDATE No DML
  15. 7-15 Copyright © 2005, Oracle. All rights reserved. Constraint Checking Case: DML statement, followed by COMMIT Nondeferred constraints checked COMMIT issued Deferred constraints checked COMMIT complete 1 3 2 4 Constraints are checked at the time of: • Statement execution, for nondeferred constraints • COMMIT, for deferred constraints
  16. 7-16 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 ck1 CHECK (pk > 0 and c1 > 0)); a c b
  17. 7-17 Copyright © 2005, Oracle. All rights reserved. Viewing the Columns in a Table
  18. 7-18 Copyright © 2005, Oracle. All rights reserved. Viewing the Contents of a Table
  19. 7-19 Copyright © 2005, Oracle. All rights reserved. Indexes 22 22 Index Table Key Row pointer … WHERE key = 22 Schema Constraints > Indexes Views Temp Tables
  20. 7-20 Copyright © 2005, Oracle. All rights reserved. Types of Indexes These are several types of index structures available to 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 a row that may or may not contain the indexed value. This is best for low-cardinality columns.
  21. 7-21 Copyright © 2005, Oracle. All rights reserved. B-Tree Index Index entry header Key column length Key column value ROWID Root Branch Leaf Index entry
  22. 7-22 Copyright © 2005, Oracle. All rights reserved. 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
  23. 7-23 Copyright © 2005, Oracle. All rights reserved. Index Options • A unique index ensures that every indexed value is unique. • An index can have its key values stored in ascending or descending order. • A reverse key index has its key value bytes stored in reverse order. • A composite index is one that is based on more than one column. • A function-based index is an index based on a function’s return value. • A compressed index has repeated key values removed.
  24. 7-24 Copyright © 2005, Oracle. All rights reserved. Creating Indexes CREATE INDEX my_index ON employees(last_name, first_name);
  25. 7-25 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 other’s private data Schema Constraints Indexes Temp Tables
  26. 7-26 Copyright © 2005, Oracle. All rights reserved. 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;
  27. 7-27 Copyright © 2005, Oracle. All rights reserved. Thank You
Anúncio