Skip to content

Latest commit

 

History

History
97 lines (71 loc) · 7.02 KB

create-master-key-transact-sql.md

File metadata and controls

97 lines (71 loc) · 7.02 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic ms.custom f1_keywords helpviewer_keywords dev_langs monikerRange
CREATE MASTER KEY (Transact-SQL)
CREATE MASTER KEY (Transact-SQL) creates a database master key in the database.
VanMSFT
vanto
wiassaf
01/18/2024
sql
t-sql
reference
ignite-2024
CREATE_MASTER_KEY_TSQL
CREATE MASTER KEY
encryption [SQL Server], Database Master Key
database master key [SQL Server]
CREATE MASTER KEY statement
cryptography [SQL Server], Database Master Key
database master key [SQL Server], creating
TSQL
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

CREATE MASTER KEY (Transact-SQL)

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics PDW FabricSQLDB]

Creates a database master key in the database.

Important

  • You should back up the master key by using BACKUP MASTER KEY, and store the backup in a secure, off-site location.
  • In SQL Server, you should also back up the service master key using BACKUP SERVICE MASTER KEY, and store the backup in a secure, off-site location.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

CREATE MASTER KEY [ ENCRYPTION BY PASSWORD ='password' ]
[ ; ]

Arguments

PASSWORD ='password'

The password that is used to encrypt the master key in the database. password must meet the Windows password policy requirements of the computer that is running the instance of [!INCLUDEssNoVersion]. password is optional in [!INCLUDEssSDS] and [!INCLUDEssazuresynapse-md].

Remarks

The database master key is a symmetric key used to protect the private keys of certificates and asymmetric keys that are present in the database and secrets in database scoped credentials. When it is created, the master key is encrypted by using the AES_256 algorithm and a user-supplied password. In [!INCLUDEsql2008-md] and [!INCLUDEsql2008r2], the Triple DES algorithm is used. To enable the automatic decryption of the master key, a copy of the key is encrypted by using the service master key and stored in both the database and in master. Typically, the copy stored in master is silently updated whenever the master key is changed. This default can be changed by using the DROP ENCRYPTION BY SERVICE MASTER KEY option of ALTER MASTER KEY. A master key that isn't encrypted by the service master key must be opened by using the OPEN MASTER KEY statement and a password.

The is_master_key_encrypted_by_server column of the sys.databases catalog view in master indicates whether the database master key is encrypted by the service master key.

Information about the database master key is visible in the sys.symmetric_keys catalog view.

For SQL Server and Parallel Data Warehouse, the master key is typically protected by the service master key and at least one password. In case of the database being physically moved to a different server (log shipping, restoring backup, etc.), the database will contain a copy of the master key encrypted by the original server service master key (unless this encryption was explicitly removed using ALTER MASTER KEY DDL), and a copy of it encrypted by each password specified during either CREATE MASTER KEY or subsequent ALTER MASTER KEY DDL operations. In order to recover the master key, and all the data encrypted using the master key as the root in the key hierarchy after the database has been moved, the user will have either use OPEN MASTER KEY statement using one of the passwords used to protect the master key, restore a backup of the master key, or restore a backup of the original service master key on the new server.

For [!INCLUDEssSDS] and [!INCLUDEssazuresynapse-md], the password protection isn't considered to be a safety mechanism to prevent a data loss scenario in situations where the database may be moved from one server to another, as the service master key protection on the master key is managed by Microsoft Azure platform. Therefore, the master key password is optional in [!INCLUDEssSDS] and [!INCLUDEssazuresynapse-md].

For SQL Database, the database master key can be created automatically to protect the secrets in database scoped credentials used for auditing and other features that require a database scoped credential to authenticate to an external resource, like an Azure Storage account. The master key is created with a strong randomly selected password. Users can't create the master key on a logical master database. The master key password is unknown to Microsoft and not discoverable after creation. For this reason, creating a database master key before creating a database scoped credential is recommended.

The service master key and database master keys are protected by using the AES-256 algorithm.

Permissions

Requires CONTROL permission on the database.

Examples

Use the following example to create a database master key in a database. The key is encrypted using a password.

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<strong password>';
GO

Verify the presence of the new key, ##MS_DatabaseMasterKey##:

SELECT * FROM sys.symmetric_keys;
GO

Related content