Dynamic Data Masking

Secure your sensitive data in Azure SQL Database with built-in masking capabilities.

What is Dynamic Data Masking?

Dynamic Data Masking (DDM) is a feature designed to limit sensitive data exposure by transforming it to authorized users. It helps prevent unauthorized access to sensitive data by allowing you to define rules that mask specific data elements. This is particularly useful for applications that need to display data to various user roles, some of whom should not see sensitive information.

DDM works by rewriting queries at runtime, so the masking is applied without any modification to the underlying database schema. This means your application logic remains largely unaffected.

How it Works

You can define masking rules for specific columns in your tables. When a user queries these columns, DDM applies a masking function. There are several built-in masking formats:

Enabling Dynamic Data Masking

You can enable and configure DDM using Transact-SQL (T-SQL) or through the Azure portal.

Using T-SQL

To add a masking rule to a column:


ALTER TABLE YourTable
ALTER COLUMN YourColumn ADD MASKED WITH (FUNCTION = 'email()');
            

To remove a masking rule:


ALTER TABLE YourTable
ALTER COLUMN YourColumn NOT MASKED;
            

You can also define custom masking functions:


CREATE FUNCTION dbo.MaskSSN(@SSN VARCHAR(11))
RETURNS VARCHAR(11)
AS
BEGIN
    RETURN 'XXX-XX-' + RIGHT(@SSN, 4);
END;
GO

ALTER TABLE Employees
ALTER COLUMN SocialSecurityNumber ADD MASKED WITH (FUNCTION = 'dbo.MaskSSN(@SSN)');
            

Using the Azure Portal

Navigate to your Azure SQL Database in the Azure portal. Under the "Security" section, find "Dynamic Data Masking". You can add or remove masking rules directly from the UI.

1

Go to your Azure SQL Database resource.

In the left-hand menu, under "Security", select "Dynamic data masking".

2

Click "Add mask".

Select the table, column, and the desired masking function.

3

Click "Save".

Permissions and Unmasking

By default, all users will see masked data. However, you can grant the UNMASK permission to specific users or roles who require access to the original, sensitive data. This is typically assigned to administrators or specific application service accounts.


GRANT UNMASK TO YourUserOrRole;
            
Important: The UNMASK permission is a privileged permission. Grant it judiciously.

Benefits of Dynamic Data Masking