chapter -4 update,delete,alter,rename,create,insert,drop

Upload: bhishma-brahmbhatt

Post on 08-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    1/15

    DATABASE MANAGEMENT

    SYSTEMS

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    2/15

    OBJECTIVES

    Creating a Table from a Table using CREATEstatement.

    Inserting Data into a Table from another Table

    using INSERT statement.

    Delete Operations Updating the contents of a table

    Modifying the Structure of Table

    Renaming Table

    Truncating Table

    Destroying Table

    2

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    3/15

    CREATING ATABLE FROM ATABLE USING

    CREATE STATEMENT.

    3

    The Source table is a table identified in the SELECTsection of this SQL sentence. The Target table is one

    identified in the CREATE section of this SQL sentence.

    This table sentence populates the Target table with data

    from the Source table.

    To create a Target table without the records from thesource table (i.e. create the structure only), the select

    statement must be a have a WHERE clause. The

    WHERE clause must specify the condition that

    cannot be satisfied.

    Syntax: CREATE TABLE (, ,

    ) AS SELECT , ,

    FROM

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    4/15

    CREATING ATABLE FROM ATABLE USING

    CREATE STATEMENT.

    4

    Example:

    y create table d10history as select

    empno,ename,job,hiredate,sal from

    emp;

    y create table empstruct as select

    empno,ename,job,hiredate,sal from

    emp where 1=2;

    y create table empspec(eno,empname,

    designation,salary) as select

    empno,ename,job,sal from emp;

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    5/15

    INSERTING DATA INTOATABLE FROM ANOTHER

    TABLE USING INSERT STATEMENT.

    5

    To insert data one row at a time into a table, it

    is quite possible to populate a table with data

    that already exists in another table.

    Here the WHERE clause is optional. If you arenot specify the WHERE clause then all the

    from source table to target table is copied.

    Syntax: INSERT INTO

    SELECT , , FROM < TableName> [WHERE

    ];

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    6/15

    INSERTING DATA INTOATABLE FROM ANOTHER

    TABLE USING INSERT STATEMENT.

    6

    Example:

    y insert into empstruct select

    empno,ename,job,hiredate,sal from emp

    where dpetno = 10 or job = AssistantProfessor;

    Syntax: INSERT INTO

    SELECT , ,

    FROM < TableName> [WHERE ];

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    7/15

    DELETE OPERATIONS

    7

    The DELETE command deletes rows from the

    table that satisfies the condition provided by its

    WHERE clause, and returns the number of

    records deleted.

    Removal of All Rows:.Syntax: DELETE FROM ;

    Example: Empty the emp table.

    DELETE FROM empstruct;

    Removal ofSpecific Rows:Syntax: DELETE FROM WHERE

    ;

    Example: DELETE FROM empstruct WHERE job =

    Assistant Professor;

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    8/15

    UPDATING THE CONTENTS OF A TABLE

    8

    The UPDATE command is used to change or modify

    data values in table.

    Updating of All Rows:

    Syntax: UPDATE SET = , =

    ;

    Example: UPDATE d10history SET deptno = 20, job

    =Professor;

    Updating of Records Conditionally:

    Syntax: UPDATE SET =

    , =

    WHERE ;Example: UPDATE empspec SET empname=Ajay

    Patel, designation=Assistant Professor where

    eno = e001;

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    9/15

    MODIFYING THE STRUCTURE OF TABLE

    9

    The structure of a table can be modified by usingALTER TABLE command.

    ALTER TABLE allows changing the structure of an

    existing table.

    With ALTER TABLE it is possible to add or delete

    columns, change the data type of existing columns.

    Restrictions on the ALTER TABLE:

    The following tasks cannot be performed when

    using the ALTER TABLE clause:

    y Change the name of the table.

    y Change the name of the column.

    y Decrease the size of a column if table data exists

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    10/15

    MODIFYING THE STRUCTURE OF TABLE

    10

    Adding New Columns:

    Syntax: ALTER TABLE ADD

    (

    (),

    () , );Example:

    ALTER TABLE emp ADD(spouse

    varchar2(40));

    ALTER TABLE emp ADD(addressvarchar2(250),city varchar2(50));

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    11/15

    MODIFYING THE STRUCTURE OF TABLE

    11

    Dropping a Column from a Table:Syntax: ALTER TABLE DROP

    COLUMN ;

    Example: ALTER TABLE emp DROP COLUMN

    spouse;

    Modifying Existing Columns:

    Syntax: ALTER TABLE MODIFY

    (

    ());Example: ALTER TABLE emp MODIFY(empno

    varchar2(20));

    ALTER TABLE emp MODIFY(sal

    number(9,2),job varchar2(50));

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    12/15

    RENAMING TABLE

    12

    Oracle allows renaming of tables. Therename operation is done atomically,

    which means that no other thread can

    access any of the tables while the rename

    process is running.

    Syntax: RENAME TO

    ;

    Example: RENAME d10history TO d10;

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    13/15

    TRUNCATING TABLE

    13

    TRUNCATE TABLE empties a table completely.

    Logically, this is equivalent to a DELETE

    statement that deletes all rows, but there are

    practical differences under some circumstances.

    TRUNCATE TABLE differs from DELETE in the

    following ways:

    y Truncate operations drop and re-create table,

    which is much faster than deleting rows one by

    one.

    y Truncate operations are not transaction-safe

    (i.e. an error will occur if an active transaction

    or an active table lock exists).

    y The numbers of deleted rows are not returned.

    Syntax: TRUNCATE TABLE ;

    Example: TRUNCATE TABLE da;

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    14/15

    DESTROYING TABLE

    14

    Sometimes tables within a particular

    database become obsolete and need to bediscarded. In such situation using DROP

    TABLE statement with the table name

    can destroy a specific table. If a table is

    dropped all records held within it are lostand cannot be recovered.

    Syntax:

    DRO

    P TABLE;Example: DROP TABLE da;

  • 8/6/2019 Chapter -4 Update,Delete,Alter,Rename,Create,Insert,Drop

    15/15

    Thank You