Oracle_ch3.pptx

Embed Size (px)

Citation preview

  • 8/10/2019 Oracle_ch3.pptx

    1/44

    Dr. Chen, Oracle Database System (Oracle) 1

    Chapter 3:

    Table Creation and Management:Creating and Modifying

    Database Tables

    Jason C. H. Chen, Ph.D.

    Professor of MIS

    School of Business Administration

    Gonzaga University

    Spokane, WA 99258

    [email protected]

  • 8/10/2019 Oracle_ch3.pptx

    2/44

    Dr. Chen, Oracle Database System (Oracle) 2

    Objectives

    Become acquainted with Structured Query Language(SQL)

    Identify the table name and structure

    Create a new table using the CREATE TABLE command

    Use a subquery to create a new table Add a column to an existing table

    Modify the definition of a column in an existing table

    Delete a column from an existing table

    Mark a column as unused and then delete it at a later time Rename a table

    Truncate a table

    Drop a table

  • 8/10/2019 Oracle_ch3.pptx

    3/44

    Dr. Chen, Oracle Database System (Oracle) 3

    Database Objects and Queries

    An Oracle database consists of multiple user accounts

    Each user account owns database objects Tables

    Views

    Stored programs, etc.

    Query: command to perform operation on databaseobject

    Structured Query Language (SQL) Industry standard query language for most of relational

    databases

    Consists of about 30 commands

  • 8/10/2019 Oracle_ch3.pptx

    4/44

    Dr. Chen, Oracle Database System (Oracle) 4

    Basic SQL Concepts and Commands

    SQL (Structured Query Language) is used to

    manipulate the database.

    There are twobasictypes of SQL commands:

    DDL commands work with the structureof the objects

    (tables, indexes, views) in the database.

    DML commands work with the datain the database

    (i.e.,manipulate the data).

    Reserved words - SQL command words

    Data Definition Language (DDL)

    Data Manipulation Language (DML)

  • 8/10/2019 Oracle_ch3.pptx

    5/44

    Dr. Chen, Oracle Database System (Oracle) 5

    Security -

    Granting Table Privileges

    Security is the prevention of unauthorized access

    to the database. Within an organization, the

    database administrator determines the types of

    access various users need for the database. Some users might be able to retrieve and update

    data in the database. Other users might be able to

    retrieve any data from the database but not make

    any changes to it. Still other users might be ableto access only a portion of the database.

  • 8/10/2019 Oracle_ch3.pptx

    6/44

  • 8/10/2019 Oracle_ch3.pptx

    7/44

    Dr. Chen, Oracle Database System (Oracle) 7

    A. Names and PropertiesWhy need a name?

  • 8/10/2019 Oracle_ch3.pptx

    8/44

    Dr. Chen, Oracle Database System (Oracle) 8

    Creating a Table

    CREATE TABLE tablename

    (fieldname1data_type (size),

    fieldname2 data_type (size),

    );

  • 8/10/2019 Oracle_ch3.pptx

    9/44

    Dr. Chen, Oracle Database System (Oracle) 9

    Defining Oracle10g Database Tables

    To create a table, you must specify:

    Table name

    Field names

    Field data types

    Field sizes

    Constraints

    restrictions on the data values that a field can store

  • 8/10/2019 Oracle_ch3.pptx

    10/44

    Dr. Chen, Oracle Database System (Oracle) 10

    I. Names and Properties: Conventions

    1. From 1 to 30 characters

    2. Only alphanumeric characters, and special

    characters ($ , _, #)3. Must begin with a letter and can not contain blank

    spaces or hyphens

    4. Must be unique and No reserved words are allowed

    Series of rules Oracle Corporation established for

    naming all database objects

    Are the following names valid? Why?

    customer order

    customer-order

    #order

    Customer_#

    Customer#

  • 8/10/2019 Oracle_ch3.pptx

    11/44

    Dr. Chen, Oracle Database System (Oracle) 11

    Oracle 11g Data Types

    Data typeSpecifies kind of data that column stores

    Provides means for error checking

    Enable DBMS to use storage space moreefficiently by internally storing different types of

    data in different ways

    Basic types

    Character

    Number

    Date/time

  • 8/10/2019 Oracle_ch3.pptx

    12/44

    Dr. Chen, Oracle Database System (Oracle) 12

    II. Data Types

    Built-inprovided by the system

    Librarybuilt by the software

    vendor or a third party

    User-definedbuilt by users

  • 8/10/2019 Oracle_ch3.pptx

    13/44

    Dr. Chen, Oracle Database System (Oracle) 13

    Basic Built-In Data Types

    Character VARCHAR2

    CHAR

    Numeric NUMBER

    DATE

    OTHERS: LOB, BLOB, LONG, RAW, LONG RAW

  • 8/10/2019 Oracle_ch3.pptx

    14/44

    Dr. Chen, Oracle Database System (Oracle) 14

    Character Data Types

    1. VARCHAR2

    Stores variable-length character data upto a maximum of 4,000 characters

    Values in different records can have adifferent number of characters

    fieldname VARCHAR2(maximum_size)

    (e.g.) emp_name VARCHAR2(20); an instance: Jason Chen

  • 8/10/2019 Oracle_ch3.pptx

    15/44

    Dr. Chen, Oracle Database System (Oracle) 15

    Character Data Types (cont.)

    2. CHAR

    Fixed-length character data (

  • 8/10/2019 Oracle_ch3.pptx

    16/44

    Dr. Chen, Oracle Database System (Oracle) 16

    Character Subtypes

    Examples:

    VARCHAR2(5) Smith or Smi

    CHAR(5) Smith or Smi

    LONG Smith...

    Note that you do not specify a size for

    LONG.

    To include a single quote in a literal

    character string, use two in a row:

    This is Heralds string.

  • 8/10/2019 Oracle_ch3.pptx

    17/44

    Dr. Chen, Oracle Database System (Oracle) 17

    Question: Which query will possibly generatestudent information?

    What data type should be used if there is any

    chance that all column spaces will NOT be filled?

    Answer: VARCHAR2

    s_Last VARCHAR2(15);

    SELECT s_Last, s_First,

    s_AddressFROM student

    WHERE s_Last = Smith;

    s_Last CHAR(15);

    SELECT s_Last, s_First,

    s_AddressFROM student

    WHERE s_Last = Smith;

    L

  • 8/10/2019 Oracle_ch3.pptx

    18/44

    Dr. Chen, Oracle Database System (Oracle) 18

    When use Query:

    SELECT s_last, s_first, ssn, telephoneFROM student

    WHERE s_last = Smith;

    Case is sensitive within the single

    quotation.

    SQL Plus commands are NOTcasesensitive, but Query within the single

    quotation are case sensitive.

  • 8/10/2019 Oracle_ch3.pptx

    19/44

    Dr. Chen, Oracle Database System (Oracle) 19

    3. Number Data Types

    Stores negative, positive, fixed, and floating pointnumbers between

    10 -130

  • 8/10/2019 Oracle_ch3.pptx

    20/44

    Dr. Chen, Oracle Database System (Oracle) 20

    Number Data Types (examples)

    a) Integer: Number (n) e.g. s_id NUMBER(5)

    12345

    b) Fixed-point numbers

    e.g. current_price NUMBER (5, 2); 259.99 33.89

    c) Fixed-point numbers (cont.)

    e.g. total_mileage NUMBER (5, 1);

    259.9 33.8 d) Floating-point Numberwith a variable number of

    decimal places

    e.g. s_gpa NUMBER;

    3.89 2.7569 3.2

  • 8/10/2019 Oracle_ch3.pptx

    21/44

    Dr. Chen, Oracle Database System (Oracle) 21

    4. Date and Time Data Types

    Date, time data subtypes Store actual date and time values

    DATE

    Dates from December 31, 4712 BC to December 31, 4712

    AD Default format DD-MON-YY

    Default time format HH:MI:SS A.M.

    fieldnameDATE

    Sample declaration: OrderDate DATENOT NULL;

    Use one of the following format masks: TO_DATE( , MM/DD/YY)

    TO_DATE( , DD-MON-YYYY)

    TO_DATE( , HH:MI AM

    )

  • 8/10/2019 Oracle_ch3.pptx

    22/44

    Dr. Chen, Oracle Database System (Oracle) 22

    Table Design (continued)

    Table 3-2 Oracle 11g Datatypes

  • 8/10/2019 Oracle_ch3.pptx

    23/44

    Dr. Chen, Oracle Database System (Oracle) 23

    Table Creation

    Column definition list must be enclosed in

    parentheses Datatype must be specified for each column

    Maximum of 1,000 columns

    Defining Columns

    Figure 3-1 CREATE TABLE syntax

  • 8/10/2019 Oracle_ch3.pptx

    24/44

    Dr. Chen, Oracle Database System (Oracle) 24

    acctmanager

    Exercise: Create a new table of acctmanager based on

    the following information

    CREATE TABLE

    (

    NOTNULL);

    Amid Amfirst Amlast AmeDate Amsal Amcomm Region

    VARCHAR2(4) VARCHAR2(12) VARCHAR2(12) DATE NUMBER(8,2) NUMBER(7,2) CHAR(2)

    GROUP WORK

    Complete the

    CREATE command

    manually!

    acctmanager

    amid

  • 8/10/2019 Oracle_ch3.pptx

    25/44

    Dr. Chen, Oracle Database System (Oracle) 25

    acctmanager

    Exercise: Create a new table of acctmanager based on

    the following information

    CREATE TABLE

    (

    amfirst VARCHAR2(12) NOT NULL,

    amlast VARCHAR2(12) NOT NULL,

    amedate DATE DEFAULT SYSDATE,

    amsal NUMBER(8,2),amcomm NUMBER(7,2) DEFAULT 0,

    region CHAR(2) NOT NULL);

    Amid Amfirst Amlast AmeDate Amsal Amcomm Region

    VARCHAR2(4) VARCHAR2(12) VARCHAR2(12) DATE NUMBER(8,2) NUMBER(7,2) CHAR(2)

    SELECT *

    FROM acctmanager;

    acctmanager

    amid VARCHAR2(4) PRIMARY KEY,

  • 8/10/2019 Oracle_ch3.pptx

    26/44

    Dr. Chen, Oracle Database System (Oracle) 26

    Refresh the Database

    1. Create a new folder on c:\ as follows:

    c:\oradata\chapter3

    2. Go to Blackboard and download data

    files from Oracle chapter3 and save under

    c:\oradata\chapter3\

    3. Run the following script file

    Start c:\oradata\chapter3\JLDB_Build_3.sql

  • 8/10/2019 Oracle_ch3.pptx

    27/44

    Dr. Chen, Oracle Database System (Oracle) 27

    ExerciseYour Turn

    Type the following commands:1) SELECT TABLE_NAME FROM USER_TABLES;

    2) DROP TABLE ACCTMANAGER CASCADE

    CONSTRAINTS;

    3) DROP TABLE ACCTMANAGER2 CASCADE

    CONSTRAINTS;

    4) SELECT TABLE_NAME FROM USER_TABLES;

    You now are able to create acctmanagertable

  • 8/10/2019 Oracle_ch3.pptx

    28/44

    Dr. Chen, Oracle Database System (Oracle) 28

    CREATE TABLE Command Example

    Virtual Column/

    (Derived/computed)

    Figure 3-2 The creation of the ACCTMANAGER table

    What is the difference

    between these two versions:

    Is this a good approach?

  • 8/10/2019 Oracle_ch3.pptx

    29/44

    Dr. Chen, Oracle Database System (Oracle) 29

    Viewing Table Structures: DESCRIBE

    DESCRIBE displays the structure of a specified table

    Figure 3-4 The DESCRIBE command

  • 8/10/2019 Oracle_ch3.pptx

    30/44

    Dr. Chen, Oracle Database System (Oracle) 30

    Table Creation through Subqueries

    You can use subqueries to retrieve datafrom an existing table

    Requires use of AS keyword

    New column names can be assigned

    CREATE TABLEAS

    Figure 3-8 CREATE TABLE AS command syntax

  • 8/10/2019 Oracle_ch3.pptx

    31/44

    Dr. Chen, Oracle Database System (Oracle) 31

    CREATE TABLEAS Command

    Example

    Figure 3-7 Creating a table based on a subquery

    SELECT * FROM cust_mkt;

  • 8/10/2019 Oracle_ch3.pptx

    32/44

  • 8/10/2019 Oracle_ch3.pptx

    33/44

    Dr. Chen, Oracle Database System (Oracle) 33

    ALTER TABLEADD Command

    Figure 3-12 The ALTER TABLE ADD command

  • 8/10/2019 Oracle_ch3.pptx

    34/44

    Dr. Chen, Oracle Database System (Oracle) 34

    ALTER TABLEMODIFY Command

    Figure 3-18 The ALTER TABLE MODIFY command to increase the column width

  • 8/10/2019 Oracle_ch3.pptx

    35/44

    Dr. Chen, Oracle Database System (Oracle) 35

    Modification Guidelines

    Column must be as wide as the data it

    already contains

    If a NUMBER column already contains

    data, size cannot be decreased

    Adding or changing default data does not

    affect existing data

  • 8/10/2019 Oracle_ch3.pptx

    36/44

    Dr. Chen, Oracle Database System (Oracle) 36

    ALTER TABLESET UNUSED Command

    Once marked for deletion, a column cannot berestored

    Storage space is freedat a later time

    36

  • 8/10/2019 Oracle_ch3.pptx

    37/44

    Dr. Chen, Oracle Database System (Oracle) 3737

    ALTER TABLEDROP UNUSED Command

    Frees up storage space from columns previouslymarked as unused

    However, once a table is set UNUSED it cant be

    DROPPED using the following (regular) command:ALTER TABLE tablename

    DROP COLUMN colname; Show: test_UNUSED.sql

    and test_UNUSED.doc

  • 8/10/2019 Oracle_ch3.pptx

    38/44

    Dr. Chen, Oracle Database System (Oracle) 38

    AuthorID LName FName

    VARCHAR2(4) VARCHAR2(10) VARCHAR2(10)

    AUTHOR

    SQL> SELECT * FROM author;

    AUTH LNAME FNAME

    ---- ---------- ----------

    S100 SMITH SAM

    J100 JONES JANICE

    A100 AUSTIN JAMES

    M100 MARTINEZ SHEILA

    K100 KZOCHSKY TAMARAP100 PORTER LISA

    A105 ADAMS JUAN

    B100 BAKER JACK

    P105 PETERSON TINA

    W100 WHITE WILLIAM

    W105 WHITE LISA

    AUTH LNAME FNAME

    ---- ---------- ----------

    R100 ROBINSON ROBERT

    F100 FIELDS OSCAR

    W110 WILKINSON ANTHONY

    14 rows selected.

    AUTHOR

  • 8/10/2019 Oracle_ch3.pptx

    39/44

    Dr. Chen, Oracle Database System (Oracle) 39

    AuthorID LName FName

    VARCHAR2(4) VARCHAR2(10) VARCHAR2(10)

    AUTHOR

    SQL> describe author;Name Null? Type

    ------------ -------- ---------------

    AUTHORID NOT NULL VARCHAR2(4)

    LNAME VARCHAR2(10)

    FNAME VARCHAR2(10)

    SQL>

    SQL> ALTER TABLE author SET UNUSED

    COLUMN fname;

    Table altered.

    SQL> describe author;

    Name Null? Type

    ----------- -------- -------------

    AUTHORID NOT NULL VARCHAR2(4)LNAME VARCHAR2(10)

    SQL>

    SQL> ALTER TABLE author DROP COLUMN

    lname;

    Table altered.

    SQL>ALTER TABLE author DROP COLUMN

    fname;

    ALTER TABLE author DROP COLUMN fname

    *

    ERROR at line 1:

    ORA-00904: "FNAME": invalid identifier

    SQL> ALTER TABLE author

    2 DROP UNUSED COLUMNS;

    Table altered.

    SQL> describe author;

    Name Null? Type

    ----------- -------- -------------

    AUTHORID NOT NULL VARCHAR2(4)

    SQL>

    Show: test_UNUSED.sql

    and test_UNUSED.doc

    AUTHOR

  • 8/10/2019 Oracle_ch3.pptx

    40/44

    Dr. Chen, Oracle Database System (Oracle) 40

    AuthorID LName FName

    VARCHAR2(4) VARCHAR2(10) VARCHAR2(10)

    AUTHOR

    SQL> SELECT * FROM author;

    AUTH

    ----

    A100

    A105

    B100

    F100

    J100K100

    M100

    P100

    P105

    R100

    S100

    AUTH

    ----

    W100

    W105

    W110

    14 rows selected.

  • 8/10/2019 Oracle_ch3.pptx

    41/44

    Dr. Chen, Oracle Database System (Oracle) 41

    Practice all the rest of examples in the text.

    A Script file is available on the Bb (file

    name: Ch3Queries.sql)

    After completing all examples, do the HW.

    H k H d O A i

  • 8/10/2019 Oracle_ch3.pptx

    42/44

    Dr. Chen, Oracle Database System (Oracle) 42

    Homework - Hands-On Assignments

    Read and Practice all examples on Chapters 3

    1. Run the script files (in the folder \oradata\chapter3\):

    JLDB_Build_3.sql 2. Read Oracle assignment and create a script file

    Oracle_ch3_Lname_Fname.sql for questions (#1 to#8; p.94)on Hands-on Assignments. .

    3. Execute and test one problem at a time and make surethey are all running successfully.

    4. When you done, spool the script files (see next slidefor spooling instructions) and email the file(Oracle_ch3_Spool_Lname_Fname.txt) to me by themidnight before the next class. Turn in a hardcopy to mein the class.

    Email me with one attachment

    (Oracle_ch3_Spool_Lname_Fname.) to:

    [email protected] subject title of

    bmis441_Oracle_ch3

  • 8/10/2019 Oracle_ch3.pptx

    43/44

    Dr. Chen, Oracle Database System (Oracle) 43

    How to Spool your Script and Output Files

    After you tested the script file of Oracle_ch3_Lname_Fname.sql successfully,follow the instructions below to spool both script and output files:

    Step 0. Run the following script file from SQL*Plus (since you have createdJLDB tables)

    Start c:\oradata\chapter3J\LDB_Build_3.sql

    1. type the following on SQL> Spool c:\oradata\Oracle_ch3_Spool_Lname_Fname.txt(make sure your name is entered)

    2. open Oracle_ch3_Lname_Fname.sql that you already tested 3. copy and paste all the SQL commands (including all comments) to the

    SQL*PLUS

    4. type Spool Off on the SQL>

    The output should contain your personal information, all SQL commands and

    their solution on the .txt file and saved in C: drive (oradata\ folder)

    Email me with the spooled file (.txt) with attachment to:

    [email protected]

    with subject title of

    bmis441_Oracle_ch3

  • 8/10/2019 Oracle_ch3.pptx

    44/44

    End of chapter 3