Quickstart: Create and query an Azure SQL Database

Learn how to quickly create an Azure SQL Database and query it using Transact-SQL (T-SQL) with tools like Azure Data Studio or SQL Server Management Studio (SSMS).

In this quickstart, you will:

  • Create an Azure SQL Database server and database.
  • Configure firewall rules.
  • Connect to the database.
  • Create a table and insert data.
  • Query the data.

Prerequisites

Before you begin, make sure you have the following:

Step 1: Create an Azure SQL Database Server and Database

You can create an Azure SQL Database server and an empty database using the Azure portal.

  1. Go to the Azure portal.
  2. In the search bar, type Azure SQL and select Azure SQL from the results.
  3. Under SQL databases, select Create.
  4. On the Basics tab, fill in the following information:
    • Subscription: Select your Azure subscription.
    • Resource group: Select or create a new resource group (e.g., myResourceGroup).
    • Name: Enter a unique name for your SQL database (e.g., mysqldb).
    • Server: Select Create new and enter a globally unique server name (e.g., myserver123), choose a location, and set the Authentication method to SQL authentication. Enter an Admin username (e.g., sqladmin) and a strong Password.
    • Workload environment: Choose Development for this quickstart.
    • Compute + storage: Select Configure database and choose a Service tier (e.g., GeneralPurpose) and Compute tier (e.g., Serverless).
  5. Click Review + create, then Create.
💡
Tip: For a production environment, consider using Azure SQL Database Managed Instance for more advanced features and control.

Step 2: Configure Server Firewall

Azure SQL Database has a firewall that prevents external applications and tools from connecting to the server. You need to add your client IP address to the firewall rules.

  1. Once your SQL database is deployed, navigate to your SQL database resource in the Azure portal.
  2. In the left-hand menu, under Settings, select Firewalls and virtual networks.
  3. Click Add client IP to add your current IP address to the allowed list.
  4. Click Save.
⚠️
Note: It's recommended to set a specific IP range rather than allowing all client IPs for enhanced security.

Step 3: Connect to the Database

Now, connect to your Azure SQL Database using your preferred tool.

Using Azure Data Studio

  1. Open Azure Data Studio.
  2. In the Connections pane, click New Connection.
  3. Enter the following details:
    • Server: Your server name (e.g., myserver123.database.windows.net).
    • Authentication type: SQL Login.
    • User name: Your admin username (e.g., sqladmin).
    • Password: Your admin password.
    • Database: Select your database name (e.g., mysqldb) from the dropdown.
  4. Click Connect.

Using SQL Server Management Studio (SSMS)

  1. Open SSMS.
  2. In the Connect to Server dialog, enter the following:
    • Server name: Your server name (e.g., myserver123.database.windows.net).
    • Authentication: SQL Server Authentication.
    • Login: Your admin username (e.g., sqladmin).
    • Password: Your admin password.
  3. Click Connect.

Step 4: Create a Table and Insert Data

Execute the following T-SQL commands to create a table named Customers and insert some data.


-- Create a new table called 'Customers'
CREATE TABLE Customers (
    CustomerID int NOT NULL PRIMARY KEY,
    FirstName varchar(50),
    LastName varchar(50),
    Email varchar(100)
);

-- Insert data into the table
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
VALUES
    (1, 'Alice', 'Smith', 'alice.smith@example.com'),
    (2, 'Bob', 'Johnson', 'bob.johnson@example.com'),
    (3, 'Charlie', 'Williams', 'charlie.williams@example.com');
            

Step 5: Query the Data

Execute the following T-SQL command to query the data from the Customers table.


-- Select all data from the Customers table
SELECT * FROM Customers;
            

You should see the following output:


CustomerID | FirstName | LastName | Email
-----------|-----------|----------|---------------------------
1          | Alice     | Smith    | alice.smith@example.com
2          | Bob       | Johnson  | bob.johnson@example.com
3          | Charlie   | Williams | charlie.williams@example.com
            

Next Steps

Congratulations! You have successfully created an Azure SQL Database, connected to it, and performed basic operations. Consider exploring these resources for more advanced topics: