Creating New SQL Database And Table With Azure

Introduction

In this article, we will see in detail about how to create our first SQL Server database in Azure and creating a table for Azure SQL database from our SQL Server Management Studio.

Prerequisites

  • Azure Account: If you don’t have free account, you can create from this link.
  • SQL Server 2014 or above.

Create new SQL Database on Azure

Using your Azure account, login to Azure site. From the dashboard page on the left side menu, we can see SQL databases. Click menu to create our first SQL database in Azure.



We can see the Add icon at the top. To create our fist SQL database, again click Add icon.



Here, we give our new database name and select or create our resource group.



Creating SQL Server on Azure

If we don’t have any SQL Server created, first we need to create a new SQL Server to create our database.

Here, we give our new Server name, login ID and Password for login to our Azure SQL Server.



Once our Server was created, select the newly added SQL Server to create our database.



Click Create button at the bottom to create our new database on the selected Server. Now, we can see a confirmation message as Validation Successful.


We can see now our new database has been created.



We can see all the details for newly created database from the database dashboard page.



To know our Azure SQL connection, we can click on the “Show database connection strings”. We can see the connection string for ADO.NET, ODBC, PHP and JDBC. We can use this connection string in our technology Applications to connect Azure databases. For example, to work with Microsoft.NET, we can select ADO.NET for working with JSP or Java, we can use JDBC or ODBC and to work with PHP Application, we can use the PHP connection strings.



Connecting Azure SQL Serve from Local SQL Server

Now, we have created our SQL database in Azure. To create tables and insert sample records, we will use SQL Server Management studio to connect to our Azure Server.

We need to give our Azure SQL Server name with our given ID and password.



When we try to connect for the first time, we may get the error, as shown below-



This error is due to the Firewall rule, which is not set by the selected Azure Server.

Setting Azure SQL Server firewall rule

You can find “Set server Firewall” at the top. Click to set our Firewall rule.



Click “Add Client IP” to set our new Firewall rule.



Click save to add our new Firewall Setting


After saving the new Firewall setting, now again connect to our Azure SQL Server from our local SQL Server Management Studio.




Creating Tables for Azure SQL Database

Now, we have connected our Azure SQL Server. Let’s create tables and insert record in our Azure database.

In Query Analyzer, run Create SQL script to create our tables, which is given below.

  1. IF EXISTS ( SELECT [nameFROM sys.tables WHERE [name] = 'HotelMaster' )   
  2. DROP TABLE HotelMaster   
  3. GO   
  4.   
  5. CREATE TABLE HotelMaster   
  6. (   
  7. RoomID int identity(1,1),   
  8. RoomNo VARCHAR(100) NOT NULL ,   
  9. RoomType VARCHAR(100) NOT NULL ,  
  10. Prize VARCHAR(100) NOT NULL   
  11. CONSTRAINT [PK_HotelMaster] PRIMARY KEY CLUSTERED   
  12. (   
  13. RoomID ASC   
  14.   
  15. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]   
  16. ON [PRIMARY]   
  17.   
  18. Insert into HotelMaster(RoomNo,RoomType,Prize) Values('101','Single','50$')  
  19. Insert into HotelMaster(RoomNo,RoomType,Prize) Values('102','Double','80$')  
  20.   
  21. select * from HotelMaster   
  22.   
  23.   
  24. IF EXISTS ( SELECT [nameFROM sys.tables WHERE [name] = 'RoomBooking' )   
  25. DROP TABLE RoomBooking   
  26. GO   
  27.   
  28. CREATE TABLE RoomBooking   
  29. (   
  30. BookingID int identity(1,1),   
  31. RoomID int ,   
  32. BookedDateFR VARCHAR(20) NOT NULL ,   
  33. BookedDateTO VARCHAR(20) NOT NULL ,  
  34. BookingStatus VARCHAR(100) NOT NULL,   
  35. PaymentStatus VARCHAR(100) NOT NULL,   
  36. AdvancePayed VARCHAR(100) NOT NULL,  
  37. TotalAmountPayed VARCHAR(100) NOT NULL,  
  38. CONSTRAINT [PK_RoomBooking] PRIMARY KEY CLUSTERED   
  39. (   
  40. [BookingID] ASC   
  41.   
  42. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]   
  43. ON [PRIMARY]   
  44.   
  45. select * from RoomBooking   
Now, we can see our new tables have been created in Azure SQL database.