Quickstart: Create and query Azure SQL Database

This guide will walk you through the process of creating an Azure SQL Database and performing basic queries using a simple command-line tool.

Prerequisites

  • An Azure account with an active subscription. If you don't have one, create a free account.
  • Azure CLI installed. If you don't have it, install it now.

Step 1: Sign in to Azure

Open your terminal or command prompt and sign in to your Azure account:

az login

This command will open a browser window for you to authenticate with Azure. Follow the instructions on the screen.

Step 2: Create an Azure SQL Server

A logical SQL server is a management container for your Azure SQL databases. You'll need to choose a unique name for your server.

az sql server create --name mystratossqlserver1234 --resource-group MyResourceGroup --location eastus --admin-user mystratossqladmin --admin-password 'YourComplexPassword!123'

Important: Replace mystratossqlserver1234 with a unique server name, MyResourceGroup with your desired resource group name (or create a new one), eastus with your preferred region, and 'YourComplexPassword!123' with a strong password.

Step 3: Create a SQL Database

Now, let's create a database on the server you just created.

az sql db create --resource-group MyResourceGroup --server mystratossqlserver1234 --name MySampleDatabase --edition Basic

Note: The --edition Basic specifies a cost-effective tier for basic workloads. Other options like Standard or Premium are available for more demanding scenarios.

Step 4: Configure Firewall Rules

To allow connections from your local machine, you need to configure the server's firewall.

az sql server firewall-rule create --resource-group MyResourceGroup --server mystratossqlserver1234 --name AllowMyClientIP --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0

This command allows all IP addresses to connect. For better security, you should replace 0.0.0.0 with your specific IP address or a range. You can find your public IP address by searching "what is my IP" on Google.

Step 5: Connect and Query the Database

You can use various tools to connect to your Azure SQL Database, such as Azure Data Studio, SQL Server Management Studio (SSMS), or command-line tools like sqlcmd.

Using sqlcmd (Example)

First, you'll need to install sqlcmd if you haven't already. Refer to sqlcmd documentation.

Connect to your database using the following command:

sqlcmd -S mystratossqlserver1234.database.windows.net -d MySampleDatabase -U mystratossqladmin -P 'YourComplexPassword!123'

Once connected, you can run SQL queries:

CREATE TABLE Customers (CustomerID int PRIMARY KEY, CompanyName varchar(255));
INSERT INTO Customers (CustomerID, CompanyName) VALUES (1, 'Microsoft');
SELECT * FROM Customers;
GO

The output should display the inserted row.

Next Steps