Sei sulla pagina 1di 7

Difference Between Primary Key and Foreign Key in

tabular format :

Primary Key Foreign Key

Primary Key is a chosen candidate key that Foreign key in a table refers to the primary key of
uniquely defines a tuple in a relation. other table.
Primary key value can never be NULL. Foreign key accepts NULL value.
No two tuples in a relation carry duplicate values Tuples can carry duplicate value for a foreign key
for a primary key attribute. attribute.

There can be only one primary key of a relation. There can be multiple foreign keys in a relation.

Primary key constraint can be defined on the Foreign Key constraint can not be defined on the
temporary tables. temporary tables.
Foreign key is not clustered indexed
By default, a primary key is clustered indexed.
automatically; it has to be done manually.

We can insert a value to a primary key attribute, We can not insert a value to a foreign key, if that
even if the referencing foreign key does not have value is not present in the referenced primary key
that value in its column. column.

Before you delete a primary key value, make You can delete a value from foreign key column
sure that value is not still present in the without bothering, whether that value is present
referencing foreign key column of referencing in referenced primary key column of referenced
table. relation.
Difference between SQL and PL SQL in tabular format :

SQL Stands for Structured Query language in PL SQL stands for Procedural Language
which single SQL statement is executed at a Structured Query Language which is used as
time. programming language in databases.

PL SQL is nothing but the block of code embed

SQL simply used to perform DDLs (Data
with SQL statements which is used to add
Definition Language) and DMLs with using
business logic with using programming
single query at a time.

SQL is called as declarative language which is PL SQL is called as procedural language which
used to define what needs to be done. is used to define how the things should be done.

PL SQL executes as a block. The code of PL

SQL executes as a single statement.
SQL is always written in to the block.

The main use of SQL statement is to process and PL SQL statements are used to perform the
manipulate the data. The SQL statements are transactions to the databases. PL SQL is mainly
mainly used for reporting purpose. used to build the application logic.

SQL directly interacts with database server There is no interaction between database server.
User can add SQL in to PL SQL statements User can not add PL SQL in to SQL statements
Difference Between Where and Having Clause Bullet-Points
In this section i would like to give you bullet-points of difference between Where and having

Point 1 :

The Where clause is used to fetch the results directly from the table.

The Having clause will fetch the data from table where result-set is processed and it will filter
the summarized data or grouped data.

Point 2 :

User can use the where clause with update and delete statements.

User can not use having clause in update and delete statements.Having clause is always used
with select statement.

Point 3 :

Where clause will filter the rows and it will process each and every row.

Having clause will process the bunch of rows or groups in SQL.

Point 4 :

Where clause is used before group by clause.

Having clause is used after group by clause.

Point 5 :

Where clause is used with Having clause when there is need of aggregation.

Where clause alone is used when the aggregation is not needed.

Difference between ROWID & ROWNUM in tabular format:


1.ROWID is nothing but Physical memory

1. ROWNUM is nothing but the sequence which
2.ROWID is permanant to that row which is allocated to that data retreival bunch.
identifies the address of that row.
2. ROWNUM is tempararily allocated sequence to
3.ROWID is 16 digit Hexadecimal number the rows.
which is uniquely identifies the rows.
3.ROWNUM is numeric sequence number
4.ROWID returns PHYSICAL ADDRESS of allocated to that row temporarily.
that row.
4.ROWNUM returns the sequence number to that
5. ROWID is automatically generated unique id row.
of a row and it is generated at the time of
insertion of row. 5. ROWNUM is an dynamic value automatically
retrieved along with select statement output.
6. ROWID is the fastest means of accessing
data. 6.ROWNUM is not related to access of data.
Difference between Delete and Truncate in tabular format :

Truncate Delete

Truncate command is faster than delete as it

Delete command is slower than truncate
works directly on the table and it will process
command as it processes the rows one by one.
the rows in bulk.

Delete command stores the data in to logs after

Truncate command does not do the logging. deleting it. Delete command stores the logs on
rows basis.

Truncate does not fire the trigger. Delete does fire the trigger.
Rollback is not possible in truncate until and
Rollback is possible in delete.
unless it is supported by Vendor like oracle.

If user want to purge tables then truncate that If user want to purge the table don’t use delete
table and then purge. command.

Truncate will reset the identity column in table if Delete will not reset the identity column in table
any. if any.
Truncate is Data Definition Language. Delete is Data Manipulation Language.
Truncate Does not support Where clause. Delete does support where clause.

Delete table Employee where name like
Truncate table Employee;

If there is scenario where user needs to Empty the 1 million data from Employee table
which command will you prefer?Why?

Answer :

By considering the performance tuning of the data i can use the truncate command. I will use this
command due to following reasons:

1. Truncate command is directly used on to table and table data is directly truncated.
2. It processes rows in bulk format.