CHAPTER 12
Azure SQL Database and Cosmos DB
Updated: May 15, 2026
20 min read
# CHAPTER 12
Azure SQL Database and Cosmos DB
1. Introduction
You *could* install a SQL database directly onto an Azure Virtual Machine. However, this means you are responsible for updating the Windows Server OS, patching SQL security flaws, configuring Always-On Availability Groups, and writing scripts to perform nightly backups. If you make a mistake, you lose your company's data. To eliminate this massive operational burden, Azure offers Azure SQL Database (Relational) and Azure Cosmos DB (NoSQL). In this chapter, we will deploy production-grade databases that maintain themselves.2. Learning Objectives
By the end of this chapter, you will be able to:- Differentiate between IaaS (Self-Managed) and PaaS (Managed Database Services).
- Define Azure SQL Database and its relational capabilities.
- Define Azure Cosmos DB and its NoSQL, multi-model capabilities.
- Deploy an Azure SQL Database.
- Secure the database connection using Azure Firewall rules.
3. Beginner-Friendly Explanation
Imagine maintaining a classic car.- Self-Managed (Virtual Machine): You own the car, but you also have to change the oil, rebuild the engine when it breaks, and lock the garage yourself. It requires intense mechanical knowledge.
- Managed Service (Azure SQL Database): You lease a car that comes with an invisible mechanic. The mechanic automatically changes the oil while you are driving, instantly swaps the engine if it fails, and guarantees the car will never be stolen. You just get in and drive.
4. Azure SQL Database (Relational)
Azure SQL Database is a fully managed Platform as a Service (PaaS) Database Engine. It runs the exact same Microsoft SQL Server engine used by millions of enterprises.- Automated Backups: Azure automatically takes full, differential, and transaction log backups (Point-in-Time Restore allows you to rewind the database to any specific second in the past 35 days!).
- High Availability: Built-in fault tolerance. If the underlying hardware fails, Azure instantly moves your database to a healthy node.
5. Azure Cosmos DB (NoSQL)
Relational databases are rigid. If you have a massive, unpredictable mobile app (like a global multiplayer game), you need flexibility and infinite horizontal scalability. Azure Cosmos DB is Microsoft's globally distributed, multi-model NoSQL database.- Schema-less: You don't create rigid tables with columns. You just throw flexible JSON documents into it.
- Turnkey Global Distribution: With one click, your database replicates perfectly across 5 different continents, ensuring users in Tokyo and users in New York both experience single-digit millisecond latency.
6. Security and Connectivity
Databases are the crown jewels of your architecture. They should never be open to the public internet. By default, an Azure SQL Database blocks ALL traffic. You must configure its Server Firewall to allow your specific IP address (so you can manage it from your laptop) and check a box to "Allow Azure services and resources to access this server" (so your Azure App Service web app can talk to it). *For true enterprise security, you disable the Public Endpoint entirely and use Azure Private Link to connect the database directly to your VNet.*7. Mini Project: Create a Managed SQL Database
Let's provision a database that we never have to maintain.Step-by-Step Tutorial:
- 1. In the Azure Portal, search for SQL databases.
- 2. Click + Create.
-
3.
Resource group:
rg-database-demo.
-
4.
Database name:
my-production-db.
- 5. Server: Click Create new.
-
Server name:
my-unique-sql-server-1234
-
Location:
East US
- Authentication method: Use SQL authentication
-
Server admin login:
sqladmin
- Password: Create a highly secure password. Click OK.
- 6. Want to use SQL elastic pool? No.
- 7. Compute + storage: Click Configure database. Select Basic (For less demanding workloads) to save money. Apply.
- 8. Click Next: Networking.
- 9. Connectivity method: Public endpoint.
- 10. Firewall rules:
- Allow Azure services: Yes.
- Add current client IP address: Yes (This lets you connect from your current laptop).
- 11. Click Review + create, then Create.
- 12. Once created, you can download a free tool like *SQL Server Management Studio (SSMS)* or *Azure Data Studio* to connect to your shiny new managed database!
8. Real-World Scenarios
An e-commerce company relies on Azure SQL Database for its shopping cart system. A junior developer accidentally runs a SQL command that deletes the entire "Users" table at 3:00 PM. Panic ensues. However, because the Cloud Engineer utilizes Point-in-Time Restore (PITR), they simply click a button in the portal to restore a new copy of the database to its exact state at 2:59 PM. The crisis is averted in minutes.9. Best Practices
- Serverless SQL: Azure offers a "Serverless" compute tier for Azure SQL. If your database is only used by accountants from 9 AM to 5 PM, Serverless SQL will automatically PAUSE the database at night. You stop paying for compute completely, and Azure instantly wakes it up the next morning when someone connects.
10. Cost Optimization Tips
- Cosmos DB Request Units (RUs): Cosmos DB bills you based on "RUs"—the amount of CPU/Memory required to perform a read or write. If you write bad queries that scan the entire database instead of using an Index, you will consume massive RUs and receive a massive bill. Always optimize NoSQL queries!
11. CLI Examples
To create a SQL Server and Database via the terminal:
bash
12. Exercises
- 1. What is the fundamental difference between Azure SQL Database (Relational) and Azure Cosmos DB (NoSQL)?
- 2. Why does Azure SQL Database automatically block your laptop's IP address by default?
13. FAQs
Q: Can I use MySQL or PostgreSQL instead of Microsoft SQL Server? A: Yes! Azure provides fully managed PaaS versions of open-source databases: Azure Database for MySQL and Azure Database for PostgreSQL. They offer the exact same automated backups and HA features.14. Interview Questions
- Q: Contrast the scaling paradigms of a relational database (Azure SQL) and a globally distributed NoSQL database (Cosmos DB). Why is Cosmos DB inherently more suited for massive, globally dispersed read-heavy workloads?
- Q: A developer complains they cannot connect to a newly provisioned Azure SQL Database from their local development environment via SQL Server Management Studio. Detail the security configurations (Firewall rules and Public/Private endpoints) you must review to grant them secure access.