database objects 1

Upload: santosh-shettar

Post on 02-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Database Objects 1

    1/26

    Dell ConfidentialDell Confidential1

    Database

    Objects

  • 8/10/2019 Database Objects 1

    2/26

    Dell ConfidentialDell Confidential2

    1)Overview on database objects

    2)Table and its structure

    3)Temporary tables

    4)External tables

    Agenda

  • 8/10/2019 Database Objects 1

    3/26

    Dell Confidential

    Database objects

    Database objects are the logical entities which Oraclemanages for users.

    Users will interact with them through Oracle.

    Each object has a unique name.

    These objects fit together to form a database.

    Database objects are classified into 2 main types:

    1)Schema Objects

    2)Non Schema Objects

  • 8/10/2019 Database Objects 1

    4/26

    Dell Confidential

    Introduction to Schema Objects

    .

    A schema is a collection of logical structures of data, or schema objects.

    A schema is owned by a database user and has the same name as

    that user.

    Each user owns a single schema.

    Schema objects are logical structures created by users to contain, or

    reference, their data.

    Schema objects include structures like tables, views, and indexes etc.

    In a relational database, the schema defines the tables, the fields in eachtable, and the relationships between fields and tables.

  • 8/10/2019 Database Objects 1

    5/26

    Dell Confidential

    illustrates the relationship among

    objects, tablespaces, and datafiles.

  • 8/10/2019 Database Objects 1

    6/26

    Dell Confidential

    Overview of TablesTables are the basic unit of data storage in an Oracle database.

    Data is stored in rows and columns.

    You define a table with a table name (such as employees) and set of

    columns. You give each column a column name (such as employee_id,

    last_name, and job_id), a datatype (such as VARCHAR2, DATE, orNUMBER), and a width.

    The width can be predetermined by the datatype, as in DATE. If columns

    are of the NUMBER datatype, define precision and scale instead of width.

    A row is a collection of column information corresponding to a single

    record.

  • 8/10/2019 Database Objects 1

    7/26Dell Confidential

    More on tables

    After you create a table, insert rows of data usingSQL statements.

    Table data can then be queried, deleted, or updated

    using SQL.

    You can specify rules for each column of a table.

    These rules are called integrity constraints.

    One example is a NOTNULLintegrity constraint.

    This constraint forces the column to contain a value

    in every row.

  • 8/10/2019 Database Objects 1

    8/26Dell Confidential

    The EMP Table

  • 8/10/2019 Database Objects 1

    9/26Dell ConfidentialDell Confidential9

    When a table is created, a data segment is allocated in atablespace to hold the table's future data.

    Controlling the allocation and use of space for a table's data

    segment is done in the following ways:

    Setting the storage parameters for the data segment.

    Setting the PCTFREE and PCTUSED parameters for the

    data segment.

    How Table Data Is Stored

  • 8/10/2019 Database Objects 1

    10/26Dell ConfidentialDell Confidential10

    Managing Storage Parameters

    The Storage parameters lets you specify how Oracle Database

    should store a database object.

    Storage parameters affect both how long it takes to access data

    stored in the database and how efficiently space in the

    database

    The Oracle Database server manages extents for locallymanaged tablespaces.

    UNIFORM

    AUTOALLOCATE (system-managed)

    INITIAL, NEXT, PCTINCREASE, and MINEXTENTS are usedtogether to compute the initial size of the segment. After the

    segment size is computed, internal algorithms determine the

    size of each extent.

  • 8/10/2019 Database Objects 1

    11/26Dell ConfidentialDell Confidential11

    The PCTFREE parameter sets the minimum percentage of a

    data block to be reservedas free space for possible updates

    to rows that already exist in that block.

    The PCTFREE Parameter

  • 8/10/2019 Database Objects 1

    12/26Dell ConfidentialDell Confidential12

    After a data block becomes full as determined by PCTFREE,

    Oracle does not consider the block for the insertion of new rowsuntil the percentage of the block being used falls below the

    parameter PCTUSED.

    The PCTUSED Parameter

  • 8/10/2019 Database Objects 1

    13/26Dell ConfidentialDell Confidential13

    PCTFREE and PCTUSED work together to optimize the use ofspace in the data blocks of the extents within a data segment.

    Figure illustrates the interaction of these two parameters.

    How PCTFREE and PCTUSED Work

    Together

  • 8/10/2019 Database Objects 1

    14/26Dell ConfidentialDell Confidential14

  • 8/10/2019 Database Objects 1

    15/26Dell Confidential

    Format of a Table Row Piece

  • 8/10/2019 Database Objects 1

    16/26Dell Confidential

    Temporary TablesAddition to permanent tables, oraclecreates temporary table to hold sessionprivate data.

    Each session can only see and modify itsown data.

    If the TRUNCATE statement is issuedagainst a temporary table, only the sessionspecific data is truncated. There is no affecton the data of other sessions.

  • 8/10/2019 Database Objects 1

    17/26Dell Confidential

    Data in temporary tables isautomatically delete at the end of thedatabase session, even if it ends

    abnormally. Indexes ,views and triggers can be

    created on temporary tables. Thecontent of the index and the scope of

    the index is that same as the databasesession.

  • 8/10/2019 Database Objects 1

    18/26Dell Confidential

    .

    Temporary tables are ideal for holdingintermediate data used by the current

    SQL session.

    EX:Select *

    into global temporary customer_india

    from customer

    where state=KARNATAKA

  • 8/10/2019 Database Objects 1

    19/26Dell Confidential

    Creation..

    The data in a temporary table is privatefor the session that created it and can besession-specific or transaction-specific. Ifthe data is to deleted at the end of the

    transaction the table should be definedas follows:CREATE GLOBAL TEMPORARY TABLEmy_temp_table (

    column1 NUMBER,column2 NUMBER) ON COMMIT DELETE ROWS;

  • 8/10/2019 Database Objects 1

    20/26Dell Confidential

    ..

    If on the other hand that data shouldbe preserved until the session ends it

    should be defined as follows:

    CREATE GLOBAL TEMPORARYTABLE my_temp_table (

    column1 NUMBER,

    column2 NUMBER) ON COMMIT PRESERVE ROWS;

  • 8/10/2019 Database Objects 1

    21/26Dell Confidential

    External Tables

    21

    External tables access data in external sources asif it were in a table in the database

    External tables are largely used as a convenient

    way of moving data into and out of the database.

  • 8/10/2019 Database Objects 1

    22/26Dell Confidential

    Creating an external table

    22

    When creating an external table you specify following attributes :

    TYPE - specifies the type of external table

    The ORACLE_LOADER access driver is the default.

    It can perform only data loads, and the data must come from text datafiles.

    The ORACLE_DATAPUMP access driver can perform both loads andunloads. The data must come from binary dump files.

    DEFAULT DIRECTORY - specifies the default location of files that are read

    or written by external tables.

    ACCESS PARAMETERS - describe the external data source andimplements the type of external table that was specified.

    LOCATION - specifies the location of the external data.

    The location is specified as a list of directory objects.

  • 8/10/2019 Database Objects 1

    23/26

    Dell Confidential

    Creating and Loading an External

    Table Using ORACLE_LOADER

    23

    1. Assume your .dat file looks as follows:

    56november, 15, 1980 baker mary alice

    87december, 20, 1970 roper lisa marie

    2. Since an external table's data is in the operating system, its

    data file needs to be in a place Oracle can access it. So the first

    step is to create a directory and grant access to it.

    CREATE DIRECTORY ext_tab_dir AS '/usr/apps/datafiles';

    GRANT READ ON DIRECTORY ext_tab_dir TO SCOTT;

  • 8/10/2019 Database Objects 1

    24/26

    Dell Confidential

    Creating and Loading an External

    Table Using ORACLE_LOADER

    24

    3. Create a traditional table named emp:

    CREATE TABLE emp (emp_no CHAR(6), last_name CHAR(25),

    first_name CHAR(20), middle_initial CHAR(1));

    4. Create an external table named emp_load:

    CREATE TABLE emp_load (employee_number CHAR(5),employee_last_name CHAR(20),

    employee_first_name CHAR(15),

    employee_middle_name CHAR(15))

    ORGANIZATION EXTERNAL (type oracle loader default directory ext_tab_dir

    ACCESS PARAMETERS (records delimited by

    newline fields(employee_number CHAR(2),employee_dob CHAR(20),

    employee_last_name CHAR(18),

    employee_first_name CHAR(11),

    employee_middle_name CHAR(11)))

    LOCATION ('info.dat'));

  • 8/10/2019 Database Objects 1

    25/26

    Dell Confidential

    Creating and Loading an External

    Table Using ORACLE_LOADER

    25

    5.Load the data from the external table emp_load into the table emp:

    Insert into emp (emp_no, first_name, middle_initial, last_name)

    values(SELECT employee_number, employee_first_name,

    substr(employee_middle_name, 1, 1),

    employee_last_name FROM emp_load);

    6.Perform the following select operation to verify that the information in the

    .dat file was loaded into the emp table:

    SQL> SELECT * FROM emp; EMP_NO LAST_NAME FIRST_NAME M

    ------ ------------------------- -------------------- -56 baker mary a

    87 roper lisa m

  • 8/10/2019 Database Objects 1

    26/26

    External Table Restrictions

    26

    An external table does not describe any data that is stored in the database.

    An external table does not describe how data is stored in the external source.

    This is the function of the access parameters.

    An external table cannot load data into a LONG column.

    When identifiers (for example, column or table names) are specified

    in the external table access parameters, certain values are considered

    to be reserved words by the access parameter parser. If a reserved word

    is used as an identifier, it must be enclosed in double quotation marks.