Вы находитесь на странице: 1из 7

Difference between varchar and varchar2

Varchar Varchar2

1) Varchar can identify NULL and empty string 1) Varchar2 cannot identify both separately. Both

separately. considered as same for this.

2) Varchar can store minimum 1 and maximum 2000 2) Varchar2 can store minimum 1 and maximum

bytes of character data. 4000 bytes of character data.

3) Allocate fixed size of data irrespective of the

input. 3) Allocate variable size of data based on input.

Ex: We defined varchar (15) and entered only 10 Ex: We defined varchar2 (15) and entered only 10
characters. But it allocates space for entire 15 characters. Then varchar2 will allocate space for 10
characters. characters only but not for 15.

4) For varchar data, extra spaces are padded to the

right side. 4) For varchar2 extra spaces will be truncated.

5) Varchar is ANSI Sql standard 5) Varchar2 is Oracle standard

6) Varchar definition may change in future. 6) Varchar2 definition will not change. It is standard.

7) Varchar is an external datatype. 7) Varchar2 is an internal datatype.


TRUNCATE

TRUNCATE is a DDL command


TRUNCATE is executed using a table lock and whole table is locked for remove all records.
We cannot use Where clause with TRUNCATE.
TRUNCATE removes all rows from a table.
Minimal logging in transaction log, so it is performance wise faster.
TRUNCATE TABLE removes the data by deallocating the data pages used to store the table
data and records only the page deallocations in the transaction log.
Identify column is reset to its seed value if table contains any identity column.
To use Truncate on a table you need at least ALTER permission on the table.
Truncate uses the less transaction space than Delete statement.
Truncate cannot be used with indexed views.

DELETE

DELETE is a DML command.


DELETE is executed using a row lock, each row in the table is locked for deletion.
We can use where clause with DELETE to filter & delete specific records.
The DELETE command is used to remove rows from a table based on WHERE condition.
It maintain the log, so it slower than TRUNCATE.
The DELETE statement removes rows one at a time and records an entry in the transaction log
for each deleted row.
Identity of column keep DELETE retain the identity.
To use Delete you need DELETE permission on the table.
Delete uses the more transaction space than Truncate statement.
Delete can be used with indexed views.
TRUNCATE DROP
DELETE
Purpose Deletes some or all Deletes all rows of a Removes all rows
rows of a table table and also the table
definition, including
indexes, triggers,
grants, storage
parameters
Command Type DML DDL DDL
Space Usage and Uses UNDO space. Does not use UNDO Does not use UNDO
Release Released blocks space. space.
that go to the Deallocates all space Unless the PURGE
freelist for the used by the table clause is specified,
table, to be used for except does not result in
subsequent MINEXTENTS. space being released.
inserts/updates.
Does not deallocate
space.

Commit required? Yes No No


Undo possible? Uncommitted Cannot be rolled A dropped table can
deletes can be back – once be reinstated from
rolled back truncated, gone the recycle
forever bin (more on this in
a future article)
Selective deletion Yes. Filter criteria No filter criteria No filter criteria
possible? be specified via allowed, removes all allowed, removes all
WHERE clause rows rows
Triggers fired? Yes, DELETE No triggers fired No triggers fired
triggers fired
What if foreign Can delete data Cannot delete data if Can drop the table
keys (FKs) based even if FKs are FKs are enabled; with the CASCADE
on the table exist? enabled, provided FKs need to be CONSTRAINTS
the data violates no disabled/dropped. option. This will also
FK constraint Exception: Truncate remove the
is possible if the FK associated FKs
is self-referential.

Efficiency DELETE can be TRUNCATE is DROP may not be as


slow especially if most efficient for efficient as
the table has many deleting all rows, TRUNCATE, as
triggers, even more than dropping and re-
indexes,and other dropping and creating the table
dependencies recreating the table requires you to re-
using DROP grant object
privileges, re-create
indexes, constraints,
etc.
Privileges required DELETE privilege. DROP ANY DROP ANY TABLE
to issue the DELETE ANY TABLE system system privilege.
command TABLE allows you privilege.
to delete rows
from anytable of
 
any schema.

Grants DELETE privilege TRUNCATE DROP ANY


on a specific table privilege on a privilege on a
can be granted to specific table cannot specific table cannot
another user or be granted to be granted to another
role. another user or role. user or role.
Can work outside Yes, as long as the No. A table can be No. A table can be
the user’s schema? user has the truncated in one’s dropped in one’s
DELETE privilege own schema only. own schema only.
on the object.
Can work on a Yes No. You will have toYes
table that is part of truncate the whole
a cluster? cluster, or use either
DELETE or DROP.
Delete: The DELETE command is used to remove rows from a table. A WHERE clause can be used
to only remove some rows. If no WHERE condition is specified, all rows will be removed. After
performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the
change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table
to fire.
Drop: The DROP command removes a table from the database. All the tables' rows, indexes and
privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back.

Delete
 Delete remove the Data only, the Table structure remains intact.
 This is a DML Statement
 Rollback possible
 No Commit is performed neither before nor after. (Because it is a DML Statement).
 They take locks on rows,
 They generate redo (lots of it)
 They require segments in the UNDO tablespace.
 A delete does not relinquish segment space thus a table in which all records have been
deleted retains all of its original blocks.
 It can activate the triggers.
Drop
 Drop permanently removes both the Data as well as the Table Structure.
 This is a DDL Statement
 Rollback not possible
 It issues a COMMIT before it acts and another COMMIT afterward so no rollback of the
transaction is possible. (Because it is a DDL Statement)
 No row-level locks are taken.
 No redo or rollback is generated.
 They do not require segments in the UNDO tablespace.
 All extents bar the initial are de-allocated from the table
 It does not activate the triggers.
While working on database, we are using Delete and Truncate without knowing the differences
between them. In this article we will discuss the difference between Delete and Truncate in Sql.

Delete:

 Delete is a DML command.


 Delete statement is executed using a row lock,each row in the table is locked for deletion.
 We can specify filters in where clause.
 It deletes specified data if where condition exists.
 Delete activities a trigger because the operation are logged individually.
 Slower than Truncate because it Keeps logs
Truncate

 Truncate is a DDL command.


 Truncate table always lock the table and page but not each row.As it removes all the data.
 Cannot use Where condition.
 It Removes all the data.
 Truncate table cannot activate a trigger because the operation does not log individual row
deletions.
 Faster in performance wise, because it doesn't keep any logs.
Note: Delete and Truncate both can be rolled back when used with Transaction. If Transaction is
done, means committed then we can not rollback Truncate command, but we can still rollback Delete
command from Log files, as delete write records them in Log file in case it is needed to rollback in
future from log files.
The difference between  UNION and  UNION ALL is that  UNION will omit duplicate records
whereas  UNION ALL will include duplicate records.
Union Result set is sorted in ascending order whereas  UNION ALL Result set is not sorted
UNION performs a DISTINCT on its Result set so it will eliminate any duplicate rows.
Whereas UNION ALL won't remove duplicates and therefore it is faster than UNION.*
Note: The performance of  UNION ALL will typically be better than  UNION, since  UNION requires the
server to do the additional work of removing any duplicates. So, in cases where it is certain that
there will not be any duplicates, or where having duplicates is not a problem, use of  UNION
ALL would be recommended for performance reasons.

Вам также может понравиться