Tutorial: Getting Started with Azure SQL Database
This tutorial will guide you through the process of creating and managing your first Azure SQL Database.
Create an Azure SQL Database Server
First, you need to create a logical SQL server in Azure. This server will host your databases.
- Sign in to the Azure portal.
- In the search bar, type
Azure SQLand select Azure SQL from the results. - Click Create.
- Under SQL databases, select Single database and click Create.
- On the Basics tab, fill in the following:
- Subscription: Select your Azure subscription.
- Resource group: Create a new one (e.g.,
myResourceGroup) or select an existing one. - Database name: Enter a unique name for your database (e.g.,
mySampleDatabase). - Server: Click Create new.
- Server name: Enter a globally unique name for your server (e.g.,
mysqldbsvr12345). - Location: Choose an Azure region.
- Administrator username: Enter a username (e.g.,
sqladmin). - Password: Enter a strong password.
- Click OK.
- Server name: Enter a globally unique name for your server (e.g.,
- Want to use SQL elastic pool?: Select No for this tutorial.
- Compute + storage: Click Configure database. For simplicity, select Basic tier and leave defaults. Click Apply.
- Click Review + create, then Create.
Configure Firewall Rules
By default, Azure SQL Database servers deny all external connections. You need to configure firewall rules to allow access.
- Once the deployment is complete, navigate to your newly created SQL server resource.
- In the server menu, under Settings, select Firewalls and virtual networks.
- Click Add client IP to allow connections from your current IP address.
- You can also specify a range of IP addresses if needed.
- Click Save.
Connect to Your Database
You can connect to your Azure SQL Database using various tools like SQL Server Management Studio (SSMS) or Azure Data Studio.
Using SQL Server Management Studio (SSMS):
- Open SSMS.
- In the Connect to Server dialog:
- Server type: Database Engine
- Server name: Enter your fully qualified server name (e.g.,
mysqldbsvr12345.database.windows.net). You can find this on your SQL server's overview page in the Azure portal. - Authentication: SQL Server Authentication
- Login: Enter the administrator username you created (e.g.,
sqladmin). - Password: Enter the password you created.
- Click Connect.
- If prompted about an untrusted certificate, click Yes.
Once connected, you can expand your server and see your database under the Databases folder.
Create a Table and Insert Data
Now, let's execute some Transact-SQL (T-SQL) commands to create a table and add data.
- In SSMS, right-click on your database (e.g.,
mySampleDatabase) and select New Query. - Paste the following T-SQL code into the query editor:
- Click the Execute button (or press F5).
CREATE TABLE Product (
ProductID INT PRIMARY KEY IDENTITY(1,1),
ProductName VARCHAR(100),
Price DECIMAL(10, 2)
);
INSERT INTO Product (ProductName, Price) VALUES ('Laptop', 1200.00);
INSERT INTO Product (ProductName, Price) VALUES ('Keyboard', 75.50);
INSERT INTO Product (ProductName, Price) VALUES ('Mouse', 25.00);
You should see messages indicating that the commands were completed successfully.
To verify the data, execute the following query:
SELECT * FROM Product;
This will display the rows you just inserted.
Clean Up Resources (Optional)
To avoid ongoing charges, you can delete the resource group you created.
- In the Azure portal, navigate to your Resource group.
- Click Delete resource group.
- Confirm the deletion by typing the resource group name.
- Click Delete.
Congratulations!
You have successfully created an Azure SQL Database, connected to it, and performed basic operations. This tutorial provides a foundational understanding. Explore the Azure portal and MSDN documentation for more advanced features like performance tuning, security, and high availability.