You are on page 1of 15

DATABASE MANAGEMENT

SYSTEMS
OBJECTIVES
 Creating a Table from a Table using CREATE statement.
 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
CREATING A TABLE FROM A TABLE
USING CREATE STATEMENT.
Syntax: CREATE TABLE <TableName>(<ColumnName 1>, …,
<ColumnName N>) AS SELECT <ColumnName 1>, …,
<ColumnName N> FROM <TableName>

 The Source table is a table identified in the SELECT section 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 the source 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.
3
CREATING A TABLE FROM A TABLE
USING CREATE STATEMENT.
 Example:

create table d10history as select


empno,ename,job,hiredate,sal from emp;
create table empstruct as select
empno,ename,job,hiredate,sal from emp where
1=2;

create table empspec(eno,empname,


designation,salary) as select
empno,ename,job,sal from emp; 4
INSERTING DATA INTO A TABLE FROM
ANOTHER TABLE USING INSERT STATEMENT.

Syntax: INSERT INTO <TableName>


SELECT <ColumnName1>, …, <ColumnName N>
FROM < TableName> [WHERE <Condition>];

 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 are not
specify the WHERE clause then all the from source
table to target table is copied.
5
INSERTING DATA INTO A TABLE FROM
ANOTHER TABLE USING INSERT STATEMENT.

Syntax: INSERT INTO <TableName>


SELECT <ColumnName1>, …, <ColumnName N> FROM
< TableName> [WHERE <Condition>];

 Example:
 insert into empstruct select
empno,ename,job,hiredate,sal from emp where
dpetno = 10 or job = ’Assistant Professor’;

6
DELETE OPERATIONS
Removal of All Rows:.
Syntax: DELETE FROM <TableName>;
Example: Empty the emp table.
DELETE FROM empstruct;

Removal of Specific Rows:


Syntax: DELETE FROM <TableName> WHERE
<Condition>;
Example: DELETE FROM empstruct WHERE job =
’Assistant Professor’;

 The DELETE command deletes rows from the table that


satisfies the condition provided by its WHERE clause, and
returns the number of records deleted. 7
UPDATING THE CONTENTS OF A TABLE
Updating of All Rows:
Syntax: UPDATE <TableName> SET <ColumnName 1>
= <Expression 1 or Value 1>, <ColumnName N>
= <Expression N or Value N>;

Example: UPDATE d10history SET deptno = 20, job


=’Professor’;
Updating of Records Conditionally:
Syntax: UPDATE <TableName> SET <ColumnName 1> =
<Expression 1 or Value 1>, <ColumnName N> =
<Expression N or Value N> WHERE <Condition>;
Example: UPDATE empspec SET empname=’Ajay
Patel’, designation=’Assistant Professor’ where
eno = ’e001’; 8
 The UPDATE command is used to change or modify data
values in table.
MODIFYING THE STRUCTURE OF TABLE
 The structure of a table can be modified by using ALTER
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:
 Change the name of the table.
 Change the name of the column.
9
 Decrease the size of a column if table data exists
MODIFYING THE STRUCTURE OF TABLE
Adding New Columns:
Syntax: ALTER TABLE <TableName> ADD
(<NewColumnName> <DataType>(<Size>),
<NewColumnName> <DataType>(<Size>) , …);

Example:
ALTER TABLE emp ADD(spouse
varchar2(40));

ALTER TABLE emp ADD(address


varchar2(250),city varchar2(50));

10
MODIFYING THE STRUCTURE OF TABLE

Dropping a Column from a Table:


Syntax: ALTER TABLE <TableName> DROP
COLUMN <ColumnName>;
Example: ALTER TABLE emp DROP COLUMN spouse;

Modifying Existing Columns:


Syntax: ALTER TABLE <TableName> MODIFY
(<ColumnName>
<NewDataType>(<NewSize>));
Example: ALTER TABLE emp MODIFY(empno
varchar2(20));
ALTER TABLE emp MODIFY(sal 11
number(9,2),job varchar2(50));
RENAMING TABLE
Syntax: RENAME <TableName> TO <NewTableName>;

Example: RENAME d10history TO d10;

 Oracle allows renaming of tables. The rename operation is


done atomically, which means that no other thread can
access any of the tables while the rename process is running.

12
TRUNCATING TABLE
Syntax: TRUNCATE TABLE <TableName>;
Example: TRUNCATE TABLE da;
 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:
 Truncate operations drop and re-create table, which is
much faster than deleting rows one by one.
 Truncate operations are not transaction-safe (i.e. an error
will occur if an active transaction or an active table lock
exists).
 The numbers of deleted rows are not returned.

13
DESTROYING TABLE
Syntax: DROP TABLE <TableName>;
Example: DROP TABLE da;

 Sometimes tables within a particular database


become obsolete and need to be discarded. 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
lost and cannot be recovered.

14
Thank You

You might also like