Avatar of KaranGupta
 asked on

System databases Vs User created databases


I am using following query to get the list of db

select * from sys.databases (or select * from sysdatabases)

My question is how can we know that which db is system database and which one is user created?

Karan Gupta
Microsoft SQL Server 2008Microsoft SQL Server 2005Microsoft SQL Server

Avatar of undefined
Last Comment

8/22/2022 - Mon
Barry Cunney

-- User Databases
Select *
From sys.databases
Where database_id > 5

--System Databases
Select *
From sys.databases
Where database_id <= 5

is 1-4 id fixed for system dbs in all versions of sql server. What is the role of sid field

SID is a system-level identification number.

when an object is created it is assigned an id - SID, that number identifies the object as a securable within SQL Server and then the object is deleted, the id is recycled.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

you can also use

SELECT * FROM sys.databases
WHERE owner_sid != 1

Open in new window


so what is good practice using sid to identify db or using id what BCUNNEY has suggested

either one will work
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I think it would be fair to split the points with BCUNNEY