Database Security
SQL firewall & network access · Entra authentication · TDE & customer-managed keys · masking & Always Encrypted · auditing · Defender for Databases
By the tools4free.dev team · Last reviewed November 13, 2026
Practice questions for this topic →
Network access and authentication
A new logical server denies all connections by default. Firewall rules apply to Azure SQL Database only; for SQL Managed Instance see its own networking configuration.
| Control | Scope and notes |
|---|---|
| Server-level IP firewall rule | All databases on the server; stored in master; up to 256 per server. Portal, PowerShell, T-SQL (sp_set_firewall_rule) or CLI. Portal and PowerShell need subscription Owner or Contributor. |
| Database-level IP firewall rule | One database; T-SQL only (sp_set_database_firewall_rule); only after the first server-level rule exists; needs at least CONTROL DATABASE; up to 256 per database. Microsoft recommends them whenever possible. |
| Allow Azure services and resources to access this server | Off by default in the portal. Adds a rule for 0.0.0.0 that lets any Azure resource connect, including other customers' subscriptions. Import Export Service needs it. |
| Virtual network firewall rule | Allows traffic from selected subnets (service endpoints). |
| Private Link | Private endpoint for the logical server in a specific virtual network. |
| Network security perimeter | Controls public network access to SQL Database; in preview at the time of writing, per Microsoft Learn. Not for SQL Managed Instance. |
- Evaluation order: database-level rules first, then server-level rules; if neither matches, the connection fails.
- Rule changes can take up to 5 minutes. Rule names can't exceed 128 characters.
- Allow outbound TCP 1433 on the client side.
Authentication
- Microsoft Entra authentication and SQL authentication are supported. Entra-only authentication is a complete alternative to SQL authentication. Managed identities remove stored passwords, and Conditional Access can apply.
- Entra setup: Entra tenant, logical server or instance, assign a Microsoft Entra administrator, then create SQL principals mapped to Entra identities.
- Give permissions to roles, not directly to users. The server admin is in
db_owner. - TLS is always enforced (
ForceEncryption= Yes). UseEncrypt=Trueand avoidTrustServerCertificate=Truein production.
Encryption and data protection
| Feature | Protects | Key points |
|---|---|---|
| TDE (service-managed) | Data at rest: database, backups and logs | On by default for new databases; must be enabled manually for older ones. Built-in server certificate, AES 256 (CBC). Can't encrypt master; tempdb is always encrypted. |
| TDE with customer-managed key | Same, with your key | Protector in Key Vault or Managed HSM; set at server (or instance) level, optionally per database; DEK re-encrypted online when you switch. |
| Always Encrypted | Data in use in specific columns, even from DBAs | Decrypted only for processing by client applications with access to the key; the key is never exposed to SQL Database or SQL Managed Instance. |
| Dynamic data masking | Query results for non-privileged users | Data in the database isn't changed. Server admin, Entra admin and db_owner see unmasked data. |
| Row-Level Security | Rows returned to a user | Based on group membership or execution context. |
| Ledger | Tamper evidence | Cryptographic proof of data integrity. |
Customer-managed TDE requirements
- Key Vault needs soft delete and purge protection; Azure SQL checks this when you set the protector.
- With a firewall on the vault, turn on Allow trusted Microsoft services to bypass the firewall, unless you use private endpoints.
- RBAC model: server identity gets Key Vault Crypto Service Encryption User. Access policy model:
get,wrapKey,unwrapKey. Managed HSM uses Managed HSM Crypto User. - The server identity can be system-assigned or user-assigned. Permission changes on the vault can take about 10 minutes.
- If the key administrator revokes access, the encrypted database becomes inaccessible. A BACPAC export of a TDE database isn't encrypted.
Dynamic data masking
- Policy parts: excluded SQL users, masking rules, masking functions (Default, Credit card, Email, Random number, Custom text, Datetime).
- Portal setup is on SQL Database only; SQL Managed Instance uses T-SQL. Roles that can configure it: SQL Security Manager, SQL DB Contributor, SQL Server Contributor.
Auditing
Auditing writes database events to an Azure storage account, a Log Analytics workspace or Event Hubs. You can use any combination, and logs go to each.
| Setting | SQL Database | SQL Managed Instance |
|---|---|---|
| Policy levels | Server (all existing and new databases) or database | Server audit plus server or database audit specifications |
| Destinations | Storage, Log Analytics, Event Hubs | Azure Storage (blob container, URL target) |
| Default retention | 0 = unlimited | 0 = unlimited |
| Storage authentication | Managed identity (recommended) or storage access keys | SAS token credential (T-SQL) |
| Default action groups | BATCH_COMPLETED_GROUP, SUCCESSFUL_DATABASE_AUTHENTICATION_GROUP, FAILED_DATABASE_AUTHENTICATION_GROUP | You define audit specifications |
- If server auditing is on it always applies. Enabling database auditing as well doesn't override it; the database is audited twice in parallel. Microsoft recommends server-level only unless a database needs a different storage account, retention, workspace or event set.
- Changing retention from 0 applies only to logs written afterwards.
- Log Analytics or Event Hubs targets create a diagnostic setting with
SQLSecurityAuditEvents. If it's deleted, auditing fails silently; alert on its deletion. - Storage behind a VNet or firewall needs a general-purpose v2 account. Audit logs are append blobs in
.xelformat. In the portal the account must be in the same region as the database. - If you configure with PowerShell, CLI, REST or ARM, the server's managed identity needs Storage Blob Data Contributor on the storage account first; the portal assigns it for you.
- With Entra authentication, failed logins don't appear in the SQL audit log; use the Entra admin center.
tempdband temporary tables aren't audited. Statement text is truncated at 4,000 characters. - Immutable audit storage: Allow protected append writes must be Append blobs or Block and append blobs (not None), and the storage retention must be shorter than the SQL Auditing retention.
- Outbound network restrictions need the audit storage account's FQDNs allow-listed, and the configuration re-saved.
- Viewing logs inside the database needs
VIEW DATABASE SECURITY AUDIT.
SQL Managed Instance to blob (T-SQL)
- Create a private blob container in a storage account in the same region.
- Generate a SAS token (Blob service, start date yesterday) and remove the leading
?. CREATE CREDENTIALnamed after the container URL, identitySHARED ACCESS SIGNATURE, secret = the token.CREATE SERVER AUDIT ... TO URL (PATH = '<container_url>', RETENTION_DAYS = n); retention default is 0.- Create a server audit specification or database audit specification. Renew the SAS before it expires.
Defender for Databases
The Databases plan in Defender for Cloud bundles four plans, each priced separately. Switching Databases to On in Environment settings activates all four and protects all supported databases in the subscription; Select types lets you pick.
| Plan | Covers | Notes |
|---|---|---|
| Defender for Azure SQL Databases | Single databases and elastic pools, managed instances, Synapse dedicated SQL pools (and SQL Server products) | Vulnerability assessment and Advanced Threat Protection. Needs SQL security manager or a database or server admin role. Subscription level recommended; per-node pricing, one charge per server or instance. |
| Defender for SQL Servers on Machines | SQL Server on VMs and Azure Arc SQL Server instances | Subscription Owner to deploy; service account in sysadmin; outbound TCP 443 to *.<region>.arcdataservices.com. Moving to a new agent architecture. |
| Defender for Open-Source Relational Databases | Azure Database for PostgreSQL and MySQL Flexible Server (all pricing tiers); Amazon RDS | PaaS only; not Azure Arc-enabled machines. |
| Defender for Azure Cosmos DB | Cosmos DB accounts | Subscription or single account (portal, PowerShell, CLI, ARM template, Policy). |
- Threats detected: potential SQL injection, anomalous access and query patterns (for example brute force), suspicious database activity.
- Enable programmatically with the Pricings REST API,
az security pricing,Set-AzSecurityPricingor Azure Policy. - Defender for Cloud itself must be on for the subscription. Vulnerability assessment results go to an automatically created storage account.
- Use the coverage workbook to see which plans are enabled.
CLI and PowerShell
| Task | Azure CLI | PowerShell |
|---|---|---|
| Create a server-level firewall rule | az sql server firewall-rule create --resource-group myResourceGroup --server $servername -n ContosoIPRange --start-ip-address 192.168.1.0 --end-ip-address 192.168.1.255 | New-AzSqlServerFirewallRule -ResourceGroupName "myResourceGroup" -ServerName $servername -FirewallRuleName "ContosoIPRange" -StartIpAddress "192.168.1.0" -EndIpAddress "192.168.1.255" |
| Create a server-level firewall rule (T-SQL) | - | EXECUTE sp_set_firewall_rule @name = N'ContosoFirewallRule', @start_ip_address = '192.168.1.1', @end_ip_address = '192.168.1.10' |
| Enable SQL Database auditing with managed identity | az SQL server audit-policy update -g "sampleresourcegroup" -n "sampleauditingtestserver" --state Enabled --bsts Enabled --storage-endpoint https://<storageaccountname>.blob.core.windows.net --storage-key '""' | Set-AzSqlServerAudit -ResourceGroupName "sampleresourcegroup" -ServerName "sampleauditingtestserver" -BlobStorageTargetState Enabled -StorageAccountResourceId "/subscriptions/<SubscriptionID>/resourcegroups/sampleresourcegroup/providers/Microsoft.Storage/storageAccounts/auditingteststorageacc" -UseIdentity True |
Exam Tips
Tick each tip once you can answer it without looking.
“Add an IP rule for one database only, from the portal” → Not possible: database-level rules are T-SQL only. Trap: Server-level rules work in the portal but apply to every database on the server.
“Firewall check order for an internet client” → Database-level rules, then server-level rules. Trap: If neither matches the connection fails.
“Allow Azure services and resources to access this server” → Adds a 0.0.0.0 rule: any Azure resource, even other customers' subscriptions. Trap: It isn't limited to your subscription; prefer VNet rules or private endpoints.
“Firewall rules or network security perimeter for SQL Managed Instance” → Don't apply to Managed Instance. Trap: See the managed instance connectivity configuration instead.
“TDE on a new Azure SQL database” → On by default; older databases must be enabled manually. Trap: TDE can't encrypt master; tempdb is always encrypted.
“Control key rotation and revocation for TDE with no downtime” → Customer-managed TDE (BYOK) with the protector in Key Vault. Trap: Switching only re-encrypts the DEK, online.
“Server identity role for TDE protector (Key Vault RBAC)” → Key Vault Crypto Service Encryption User. Trap: Managed HSM uses Managed HSM Crypto User; access policy model needs get, wrapKey, unwrapKey.
“Key Vault for TDE must have” → Soft delete and purge protection on. Trap: Behind a firewall, allow trusted Microsoft services to bypass it unless private endpoints are used.
“Hide column data even from privileged DBAs” → Always Encrypted. Trap: Dynamic data masking leaves db_owner, server admin and Entra admin unmasked.
“Server auditing on plus database auditing on” → Database is audited twice in parallel. Trap: Database auditing doesn't override the server policy.
“Auditing to a storage account behind a firewall” → General-purpose v2 account. Trap: General-purpose v1 and Blob Storage accounts must be upgraded.
“Auditing to Log Analytics suddenly stops, no error” → Diagnostic setting SQLSecurityAuditEvents was deleted. Trap: Alert on deletion of diagnostic settings.
“Configure SQL auditing with CLI using managed identity” → Pre-assign Storage Blob Data Contributor to the server's managed identity. Trap: The portal does this automatically; scripts and templates don't.
“Failed Entra logins missing from audit log” → Look in the Entra admin center. Trap: With Entra authentication, failed login records don't appear in the SQL audit log; the Entra admin center logs them.
“Managed Instance audit credential rejected” → SAS token must not start with ?. Trap: Credential name is the container URL.
“Enable all database Defender plans at once” → Databases plan On for the subscription. Trap: Each plan is priced separately.
“Defender for SQL on machines deployment role” → Subscription Owner (includes Azure Policy). Trap: The service account must be sysadmin on each SQL instance.
Sources
- Security overview: Azure SQL Database and SQL Managed Instance
- Network access controls: Azure SQL Database
- IP firewall rules: Azure SQL Database
- Microsoft Entra authentication for Azure SQL
- Transparent data encryption
- Customer-managed transparent data encryption
- Dynamic data masking
- Auditing: Azure SQL Database
- Set up auditing: Azure SQL Database
- Auditing policy at the server and database level
- Auditing using managed identity
- Configure auditing: Azure SQL Managed Instance
- Microsoft Defender for SQL
- Protect your databases with Defender for Databases
- Defender for Azure SQL Databases
- Enable Defender for SQL Servers on Machines
- Defender for Open-Source Relational Databases
- Enable Defender for Azure Cosmos DB
Checked against Microsoft Learn on November 13, 2026.