SQL Server系统数据库–模型数据库
介紹 (Introduction)
This is my fourth article about SQL Server system databases. In previous articles of the series, I wrote about the tempdb database, the master database and the msdb database.
這是我關于SQL Server系統數據庫的第四篇文章。 在本系列的前幾篇文章中,我寫了有關tempdb數據庫, master數據庫和msdb數據庫的文章。
This article focuses on the model database, the last this series about SQL Server system databases. In each SQL Server instance you will find at least the next system databases:
本文重點介紹模型數據庫,這是有關SQL Server系統數據庫的最后系列文章。 在每個SQL Server實例中,您至少會找到以下系統數據庫:
模型用法 (Model usage)
SQL Server uses the model database as a template to create new databases.
SQL Server使用模型數據庫作為模板來創建新數據庫。
Creating user objects in the model database is possible, but remember that after doing that every new database that will be created will have those objects as well, meaning that new databases inherit everything (to be accurate, mostly everything as you will see forward) from the model database.
可以在模型數據庫中創建用戶對象,但是請記住,在此之后,將要創建的每個新數據庫也將具有這些對象,這意味著新數據庫將從以下對象繼承所有內容(準確地說,基本上是您將看到的所有內容): 模型數據庫。
Note: tempdb is also created by using the model database as a template so keep in mind that any object created in the model database it will exist in the tempdb on the next time that SQL Server instance restarts since tempdb is recreated every time the SQL Server service starts.
注意: tempdb也是通過使用模型數據庫作為模板來創建的,因此請記住,在模型數據庫中創建的任何對象在下次SQL Server實例重新啟動時都會存在于tempdb中 ,因為每次SQL Server都會重新創建tempdb服務啟動。
客制化 (Customization)
When performing a configuration change in the model database it will affect the new databases created after the configuration change so, as a DBA, is always a good practice to change some of the default settings of the model database to ensure that new databases will follow a desired customization policy.
在模型數據庫中執行配置更改時,它將影響配置更改后創建的新數據庫,因此,作為DBA,更改模型數據庫的某些默認設置以確保新數據庫遵循以下規則始終是一個好習慣。所需的自定義策略。
For example, the default Recovery Model for this system database is Full. If you change it to Simple, the next database that you will create will be created with the Simple Recovery Model. This is a good change to perform on non-Production SQL Server instances when you do not need to restore to a certain point in time.
例如,此系統數據庫的默認恢復模型為“完整”。 如果將其更改為“簡單”,則將使用“簡單恢復模型”創建要創建的下一個數據庫。 當您不需要還原到某個特定時間點時,這是對非生產型SQL Server實例執行的很好的更改。
Besides the above example of changing the Recovery Model you can also configure the data and transaction log files to make new databases created with a minimum file sizes and with an auto growth value that guarantee a minimal impact of the large number of Virtual Log Files (VLF).
除了上述更改恢復模型的示例之外,您還可以配置數據和事務日志文件,以創建具有最小文件大小和自動增長值的新數據庫,以確保對大量虛擬日志文件(VLF)的影響最小)。
These configuration customizations will be only used as default values for the CREATE DATABASE command. You can always overwrite them by providing a different value (the tempdb database is always created with Simple Recovery Model no matter the Recovery Model set in the model database).
這些配置定制將僅用作CREATE DATABASE命令的默認值。 您始終可以通過提供一個不同的值來覆蓋它們( tempdb數據庫始終使用簡單恢復模型創建,而不管模型數據庫中設置的恢復模型如何)。
The following is an example of how to create a new user database in SQL Server 2016 by overwriting the default values existing in the model database for this SQL Server version. In this example, I am providing an initial file size of 64MB instead of the default 8MB, with 128MB for the file auto growth instead of the default 64MB and then set the database to a Simple Recovery Model instead of the default Full Recovery Model:
以下是如何通過覆蓋此SQL Server版本的模型數據庫中存在的默認值在SQL Server 2016中創建新用戶數據庫的示例。 在此示例中,我提供的初始文件大小為64MB,而不是默認的8MB,其中128MB用于文件自動增長,而不是默認的64MB,然后將數據庫設置為簡單恢復模型,而不是默認的完全恢復模型:
?
CREATE DATABASE [NewDB]ON??PRIMARY
( NAME = N'NewDB', FILENAME = N'S:\Program Files\Microsoft SQL
Server\MSSQL13.MSSQL2016\MSSQL\DATA\NewDB.mdf' , SIZE = 65536KB , FILEGROWTH = 131072KB )LOG ON
( NAME = N'NewDB_log', FILENAME = N'S:\Program Files\Microsoft SQL
Server\MSSQL13.MSSQL2016\MSSQL\DATA\NewDB_log.ldf' , SIZE = 65536KB , FILEGROWTH =
131072KB )
GO
ALTER DATABASE [NewDB] SET RECOVERY SIMPLE
Note: One of the configuration changes that do not affect the new databases is the Read-Only status. You can set the model database to a Read-Only nut the new databases will be always created without this status, i.e. with Read-Only = False.
注意:不影響新數據庫的配置更改之一是“只讀”狀態。 您可以將模型數據庫設置為只讀螺母,新數據庫將始終在沒有此狀態的情況下創建,即使用只讀= False。
運作方式 (Operations)
權限 (Permissions)
Regular users by default, cannot access the model database. If they need to access to the model database, keep in mind that the user will be also have the same permissions on any new databases that will be created since the CREATE DATABASE will use the model database as a template.
默認情況下,普通用戶無法訪問模型數據庫。 如果他們需要訪問模型數據庫,請記住,由于CREATE DATABASE會將模型數據庫用作模板,因此該用戶還將對將要創建的任何新數據庫具有相同的權限。
Backups
后備
Usually changes in the model database only occur when a customization is performed. When any of these changes occurs, it is also recommended to perform a backup of the model database.
通常,僅在執行定制時才發生模型數據庫中的更改。 當發生任何這些更改時,還建議對模型數據庫執行備份。
Alternatively, you can have regular backups of the model database as part of the regular system databases backup plan.
或者,您可以將模型數據庫的常規備份作為常規系統數據庫備份計劃的一部分。
移動文件位置 (Move file locations)
Model data and transaction log files can be moved to another location if and when needed to. The following code is an example on how to move the default data and transaction log files into a new location. In this case I have defined the folder S:\SysDB:
如果需要,可以將模型數據和事務日志文件移動到另一個位置。 以下代碼是有關如何將默認數據和事務日志文件移動到新位置的示例。 在這種情況下,我定義了文件夾S:\ SysDB:
?
ALTER DATABASE model
MODIFY FILE (NAME = modeldev, FILENAME = 'S:\SysDB\Data\ model.mdf');
GO
ALTER DATABASE model
MODIFY FILE (NAME = modellog, FILENAME = 'S:\SysDB\Log\ modellog.ldf');
After executing the above command, stop the respective SQL Server instance service and copy the model default data and transaction log files into the new folder location. When the copy finishes successfully, the respective SQL Server service can be started. After that verify if there are no issues with the database.
執行上述命令后,停止相應SQL Server實例服務,并將模型默認數據和事務日志文件復制到新文件夾位置。 復制成功完成后,可以啟動相應SQL Server服務。 之后,請驗證數據庫是否沒有問題。
限制條件 (Restrictions)
The model database has restrictions. I will explain some of them.
模型數據庫有限制。 我將解釋其中的一些。
刪除數據庫 (Drop database)
The model database cannot be dropped. If you try to drop it an error will be raised informing you that a system database cannot be dropped:
不能刪除模型數據庫。 如果嘗試刪除它,將引發錯誤,通知您不能刪除系統數據庫:
Set offline
設為離線
Setting the model database offline is not allowed. If you try to do it the related error will be raised:
不允許將模型數據庫設置為脫機。 如果您嘗試執行此操作,則會引發相關錯誤:
Database rename
數據庫重命名
Renaming the model database is not possible. When trying to do it the respective error will be raised informing you that action is not allowed:
重命名模型數據庫是不可能的。 嘗試執行此操作時,將引發相應的錯誤,通知您不允許采取的措施:
Change database owner
變更資料庫擁有者
Changing the owner of the model database is not possible. When trying to do it the respective error will be raised informing you that action is not allowed:
無法更改模型數據庫的所有者。 嘗試執行此操作時,將引發相應的錯誤,通知您不允許采取的措施:
Change Data Capture (CDC)
更改數據捕獲(CDC)
Enabling the CDC feature on the model database is not possible. Trying to do it the respective error will be raised informing you that action is not supported:
無法在模型數據庫上啟用CDC功能。 嘗試執行此操作時,將引發相應錯誤,通知您不支持該操作:
Add filegroup
添加文件組
It is not possible to add a filegroup for the model database name. If you try to do it you will receive an error stating that adding a filegroup is not allowed on the database:
無法為模型數據庫名稱添加文件組。 如果嘗試這樣做,將會收到一條錯誤消息,指出不允許在數據庫上添加文件組:
添加文件 (Add file)
It is not possible to add a file for the model database name. If you try to do it you will receive the error stating that cannot have files added to the database:
無法為模型數據庫名稱添加文件。 如果嘗試這樣做,將會收到錯誤消息,指出無法將文件添加到數據庫中:
More restrictions
更多限制
There are some other restrictions that are worth to be known:
還有其他一些值得注意的限制:
- model database’s primary file group cannot be set to READ_ONLY status (but the database can); 模型數據庫的主文件組設置為READ_ONLY狀態(但是數據庫可以);
- model database does not allow to rename the primary file group; 模型數據庫不允許重命名主文件組。
- model database cannot be deleted; 模型數據庫的主文件組以及主數據和主日志文件;
- model database is the SQL Server instance collation and cannot be changed; 模型數據庫的默認排序規則是SQL Server實例排序規則,不能更改;
- model database cannot be part of a database mirroring solution. 模型數據庫不能是數據庫鏡像解決方案的一部分。
Previous articles in this series:
本系列以前的文章:
- Configuration, operations and restrictions of the tempdb SQL Server system database tempdb SQL Server系統數據庫的配置,操作和限制
- SQL Server system databases – the master database SQL Server系統數據庫–主數據庫
- SQL Server system databases – the msdb database SQL Server系統數據庫– msdb數據庫
參考文獻: (References:)
- model Database 模型數據庫
- Move system databases 移動系統數據庫
- Rebuild system databases 重建系統數據庫
翻譯自: https://www.sqlshack.com/sql-server-system-databases-model-database/
總結
以上是生活随笔為你收集整理的SQL Server系统数据库–模型数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 1576 A/B
- 下一篇: 数论 欧几里得与扩展欧几里得