Build a database

Completed

In the previous module, we developed a vitals bot to collect basic health information from users. To store the information, build a database.

Follow these steps to build a database:

  1. In the Azure portal, sign in to your account.

  2. Select Create a resource.

  3. In the Categories menu, select Databases. Then select SQL Database.

    Screenshot showing how to create a database. In the Categories list, Databases is selected.

  4. Select Create.

    Screenshot of the Create SQL Database page.

  5. Use the following information to configure the SQL Database:

    • Subscription: Select your subscription.

    • Resource group: Select Create new and enter a unique name for the resource group, such as Healthbot. Then select OK.

    • Database name: Name your database. Name your database healthbot.

    • Server: Select Create new, and then fill in the following:

      • Server name: Add a server name, such as healthbot.
      • Server admin login: Add a username, such as learnbot.
      • Location: Select a location close to you.
      • Authentication method: Select Use SQL authentication.
      • Password: Add a unique password for the server. Use healthbot@123.
      • Confirm password: Retype the password you provided in the Password field.
      • Select OK.

      Screenshot showing how to create a server.

    • Want to use SQL elastic pool?: Select No.

    • Compute + storage: Select Configure database. Select Service tier.

      Screenshot that shows selecting the Basic compute plan.

    • Select the Basic option from the drop-down. Note its cost. Then select Apply.

    • Backup storage redundancy: Select Geo-redundant backup storage.

  6. Select Review + create.

    Screenshot showing the review of the database. The Review + create button is selected.

  7. On the summary page that appears, make sure you entered all details accurately. Then select Create.

  8. After the deployment succeeds, select Go to resource.

  9. Make a note of the server name that's listed under Essentials.

    Screenshot showing the database's query editor. The server name is listed on the right.

  10. Return to the Overview page of the SQL database, and then select Set server firewall.

  11. Under Public network access, select Selected networks.

  12. Under Exceptions, select the checkbox for Allow Azure services and resources to access this server.

    Screenshot showing how to add all I P addresses to the allowlist.

  13. Under Firewall rules, select Add a firewall rule, and then enter the following details in the three text boxes:

    • Rule name: ALL
    • Start IP: 0.0.0.0
    • End IP: 255.255.255.255
  14. Select Save.

  15. In the menu on the left, select Query editor (preview). Then enter the credentials you created in the previous steps.

    • Login ID: learnbot
    • Password: healthbot@123
  16. Select Tables to create a new table.

    Screenshot showing a SQL query in the editor.

    In the code section, paste the following code:

         CREATE TABLE Vitals (
                name varchar(255),
                Age int,
                Weight int,
                Height int,
                ColumnDateTime DATETIME DEFAULT CURRENT_TIMESTAMP
         );
    
  17. Select Run.

  18. To save this code for future use, select Save query.

Allow other users to edit your database

We often work with multiple people on the same project and want to be able to share our current work with them. You can assign the other users as administrators for our database. Then they can view and modify the database like we do.

Follow these steps to give access to another user:

  1. In the Azure portal, select the SQL server you created.

    Screenshot showing the selected sequel server.

  2. In the menu on the left side of the page, select Microsoft Entra ID.

    Screenshot showing the Microsoft Entra selection. The Set admin tab is also selected.

  3. Select Set admin to open the Microsoft Entra ID pane.

  4. Search for the user who needs access. Then choose Select.

    Screenshot showing the Microsoft Entra pane.

Visualize the database as a table

Even though we know our database works, we can visualize the data that's logged into a table. To visualize it, we can use Azure Data Studio.

  1. On the Azure portal landing page, select the SQL database you created.

  2. Select Connect with > Azure Data Studio.

    Screenshot showing how to connect a database for visualization.

  3. Select Download Azure Data Studio. Follow the steps to download it. If Data Studio is already installed, select Launch it now.

    Screenshot showing how to download or open Azure Data Studio.

  4. Select the New connection button next to the Servers list. The Connection pane opens on the right.

    Screenshot showing the New connection button and the Connection pane. The Connect button is selected.

  5. Fill in information for the following fields:

    • Connection type: Select Microsoft SQL Server.
    • Server: Paste the server name you created in step 9.
    • Authentication type: Select SQL login.
    • Username: Use the username you used to sign in to the query editor. We used learnbot.
    • Password: Use the password you used to sign in to the query editor. We used healthbot@123.
    • Database: Select the name of the database you created on Azure.
    • Server group: Leave the selection as Default.
  6. Select Connect.

  7. In the Servers list, select healthbotserver > Databases.

  8. Right-click healthbot and then select New query.

  9. In the query, enter SELECT * FROM Vitals.

  10. Select Run to see the table of the currently logged data.

    Screenshot showing how to run a query in Azure Data Studio. The Run button is selected.

Note

  • The query SELECT * FROM Vitals asks to select all the data from the table named Vitals.
  • The table name is case sensitive. The asterisk (*) denotes ALL.
  • Follow steps 8 through 10 every time you want to visualize the table.