HomeMYSQLDifference between temporary and regular table?

Difference between temporary and regular table?

Here’s a clear comparison between temporary tables and regular tables in MySQL:

FeatureTemporary TableRegular Table
DefinitionA table that exists only for the duration of a session (or until explicitly dropped).A normal table stored permanently in the database until dropped.
LifetimeAutomatically dropped when the session ends (or can be explicitly dropped).Exists until explicitly dropped.
ScopeOnly visible to the session that created it. Other sessions cannot access it.Accessible by any session/user with appropriate privileges.
Data StorageStored in the tmp database internally (depends on storage engine, often in memory).Stored in the database permanently using the chosen storage engine (InnoDB, MyISAM, etc.).
IndexingCan have indexes like a regular table, but usually small and temporary.Fully supports indexing, constraints, and foreign keys.
ConstraintsForeign keys are generally not allowed.Supports all constraints including primary key, foreign key, unique, etc.
Use CaseUsed for intermediate calculations, session-specific processing, temporary storage in complex queries.Used to store persistent data of the application.
Creation SyntaxCREATE TEMPORARY TABLE temp_table_name (...);CREATE TABLE table_name (...);
CollisionMultiple sessions can create temporary tables with the same name without conflict.Table names must be unique within a database.

Key Notes:

  1. Temporary tables are session-specific; if two users create a temporary table with the same name, they won’t interfere with each other.
  2. Regular tables are permanent and shared across sessions.

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *