SQL Server:如何判断数据库是否是系统数据库?

发布于 2024-08-13 03:33:06 字数 190 浏览 12 评论 0原文

我知道到目前为止(至少在MSSQL 2005之前),系统数据库有master、model、msdb和tempdb。

事实是,据我所知,这不能保证将来会被保留。 sys.databases 视图和 sys.sysdatabases 视图都没有告诉我数据库是否被视为系统数据库。

是否有地方可以获得这些信息(数据库是否被视为系统数据库)?

I know that so far (until MSSQL 2005 at least), system databases are master, model, msdb and tempdb.

Thing is, as far as I can tell, this is not guaranteed to be preserved in the future. And neither the sys.databases view nor the sys.sysdatabases view tell me if a database is considered as a system database.

Is there someplace where this information (whether a database is considered a system database or not) can be obtained?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若能看破又如何 2024-08-20 03:33:06

刚刚深入研究 Microsoft.SqlServer.Management.Smo.Database 对象(由 Microsoft 本身提供!)
他们只是使用以下语句来执行此操作:

CAST(case when dtb.name in ('master','model','msdb','tempdb') 
   then 1 
   else dtb.is_distributor end AS bit) AS [IsSystemObject]

简而言之:如果数据库名为 mastermodelmsdbtempdb >,它是一个系统数据库;
如果视图 sys.databases 中的字段 is_distributor = 1,它也是一个系统数据库。

希望这对

吉米有帮助

Just dived into Microsoft.SqlServer.Management.Smo.Database object (which is provided by Microsoft itself!)
They simply do this using following statement:

CAST(case when dtb.name in ('master','model','msdb','tempdb') 
   then 1 
   else dtb.is_distributor end AS bit) AS [IsSystemObject]

In short: if a database is named master, model, msdb or tempdb, it IS a system db;
it is also a system db, if field is_distributor = 1 in the view sys.databases.

Hope this helps

Jimmy

独木成林 2024-08-20 03:33:06

SQL Server Management Studio将使用此功能

如果您在“对象资源管理器”中展开“系统数据库”(从wireshark中看到),

SELECT dtb.name AS [Database_Name]
FROM master.sys.databases AS dtb
WHERE (CAST(case when dtb.name in ('master','model','msdb','tempdb') then 1 else dtb.is_distributor end AS bit)=1)

:为了简单起见,我删除了不相关的列,删除了orderby并将@_msparam_0变量替换为其值1

SQL Server Management Studio uses this

if you expand "System Databases" in "Object Explorer" (seen from wireshark):

SELECT dtb.name AS [Database_Name]
FROM master.sys.databases AS dtb
WHERE (CAST(case when dtb.name in ('master','model','msdb','tempdb') then 1 else dtb.is_distributor end AS bit)=1)

For the sake of simplicity I removed irrelevant columns, removed orderby and replaced @_msparam_0 variable by its value 1

远山浅 2024-08-20 03:33:06

您可以依靠 DB_ID() 函数 <= 4

您必须非常努力地改变这一点......

You can rely on the DB_ID() function <= 4

You'd have to work very hard to change this...

寒尘 2024-08-20 03:33:06

仅对于系统数据库,owner_sid 等于 0x01。
所以你可以用它来识别数据库是否是系统DB。

select * from sys.databases
where owner_sid != 0x01

owner_sid is equal to 0x01 just for system databases.
So you can use it to recognise if the database is a system DB or not.

select * from sys.databases
where owner_sid != 0x01
忆沫 2024-08-20 03:33:06

不,据我所知,没有这样的选项。我想你可以检查 sys.databases.owner_sid = 0x01 的 id。

我认为您不必担心微软更改系统数据库名称。如果他们真的这么做了,你至少 20 年就不用担心了:)

no there's no such option AFAIK. i guess you could check the id the sys.databases.owner_sid = 0x01.

i don't think you have to worry about MS changing the system db names. if they did theat you wouldn't have to worry about it for at least 20 years :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文