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:
- An Azure subscription. If you don't have one, create a free account before you begin.
- Azure Data Studio or SQL Server Management Studio (SSMS) installed.
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.
- Go to the Azure portal.
- In the search bar, type
Azure SQL
and select Azure SQL from the results. - Under SQL databases, select Create.
- 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
).
- Click Review + create, then Create.
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.
- Once your SQL database is deployed, navigate to your SQL database resource in the Azure portal.
- In the left-hand menu, under Settings, select Firewalls and virtual networks.
- Click Add client IP to add your current IP address to the allowed list.
- Click Save.
Step 3: Connect to the Database
Now, connect to your Azure SQL Database using your preferred tool.
Using Azure Data Studio
- Open Azure Data Studio.
- In the Connections pane, click New Connection.
- 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.
- Server: Your server name (e.g.,
- Click Connect.
Using SQL Server Management Studio (SSMS)
- Open SSMS.
- 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.
- Server name: Your server name (e.g.,
- 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: