Microsoft SQL Server comes with a whole range of system databases, which I visited in my post – How to programmatically identify system and user databases on a SQL Server instance. System databases are special, and there some very specific and peculiar characteristics of each (documentation for which can be found from Books On Line here).
Model v/s TempDB
One such system database is the “model” database. As the name suggests, user databases created on the particular SQL Server instance are copies of the model database.
The TempDB on the other hand, is a special system database. It’s special because every time the SQL Server is restarted, the TempDB is re-created, and we receive a clean copy of the TempDB. This got me thinking:
- How does SQL Server recreate TempDB?
- Does it copy the Model database and use that as the base?
- How is it that all but one of the restrictions on the Model database are also applicable to the TempDB?
The only way to answer these questions, is to run a small test.
WARNING: The tests demonstrated in this post involve changing your default SQL Server installation. Please check with your SQL Server Administrator before performing these steps in your development and quality assurance environments. These tests should not be performed on a production database.
Modifying the Model database
On my test system, the model database is a clean one – no modifications have been made after SQL Server was installed. On such a clean model database, let’s create a table, and fill it with some test data.
USE model
GO
CREATE TABLE DefaultTableTest
( DataId INT IDENTITY(1,1),
RandomId INT DEFAULT (RAND()*1000),
DataName VARCHAR(10) DEFAULT 'DEFAULT'
)
GO
/******************************************
Insert some test data into the table
******************************************/
USE model
GO
INSERT INTO DefaultTableTest DEFAULT VALUES
GO 600000
Now, let’s restart the SQL Server service. Take care to restart the service using the steps mentioned in my post here. Do not use the Services.msc console to restart Microsoft SQL Server.
Check the TempDB
Once the SQL Server restart is complete, it’s time to check the TempDB. First of all, let’s navigate to the physical drive where TempDB is located. We all know that typically, TempDB is 8MB in size upon SQL Server restart by default.
Because TempDB is larger than expected, it definitely has something more than usual.
Next, let’s refresh the Object Explorer pane in SSMS. We see that under TempDB, we have the DefaultTableTest table created for us. Therefore, let’s attempt to SELECT from the table.
Conclusion
TempDB is a copy of the model database – just as is the case with any other user database. No other system database is a copy of the model database – which adds to the reasons why TempDB is special.
There is also a lesson to be learnt from this. Whenever a change is made to the “model” database, one needs to be very careful about the change – it will directly impact one of the most used databases – the TempDB.
I hope you liked today’s post. Leave your feedback before you go.
Until we meet next time,
Be courteous. Drive responsibly.