Partager via


Create Database (classic)

 

THIS TOPIC APPLIES TO: noSQL Server yesAzure SQL DatabaseyesAzure SQL Data Warehouse noParallel Data Warehouse

Creates a new Microsoft Azure SQL Database.

Important

The recommended REST commands to work with SQL Database are the Resource Manager based Azure SQL Database REST API. While there is no change to SQL Database, be aware that the classic deployment model command in this article is scheduled for deprecation on December 1, 2019. For a table providing links to the specific new commands you should use, see Operations for Azure SQL Databases.

Request

The Create Database request must be specified as follows:

  • Replace {subscriptionId} with your subscription ID.

  • Replace {serverName} with the name of the server to contain the new database.

Method Request URI HTTP Version
POST https://management.core.windows.net:8443/{subscriptionId}/services/sqlservers/servers/{serverName}/databases HTTP/1.1

To determine if the database is available for use, use Database Operation Status (classic).

If an operation on databases, excluding those that cause Service Level Objective (SLO) changes, succeeds, the operation is complete and changes are effective immediately. If an operation fails because of a user error, no changes will be applied. If there is a communication error or an internal server error, query the operation’s status with Database Operation Status (classic) or check the existence of the database with Get Database (classic).

For database operations with SLO changes (or those not in shared SLO), the operation will return success if initial validation succeeds. The actual operation will be carried out asynchronously. You can query the operation’s progress with Database Operation Status (classic) or check the existence of the database with Get Database (classic).

This method is not re-entrant.

URI Parameters

None.

Request Headers

The following table describes the required and optional request headers:

Request Header Description
x-ms-version Required. Specifies the version of the operation to use for this request. This header should be set to 2012-03-01.
x-ms-client-request-id Optional. Provides a client-generated, opaque value with a 1 KB character limit. Using this header is highly recommended for correlating client-side activities with requests received by the server.

Request Body

The format of the request body is as follows:

<ServiceResource xmlns="https://schemas.microsoft.com/windowsazure">  
  <Name>testdb</Name>  
  <Edition>Standard</Edition>  
  <CollationName>SQL_Latin1_General_CP1_CI_AS</CollationName>  
  <MaxSizeBytes>10737418240</MaxSizeBytes>  
  <ServiceObjectiveId>1b1ebd4d-d903-4baa-97f9-4ea675f5e928</ServiceObjectiveId>  
</ServiceResource>  

The following table describes the elements of the request body.

Element Name Description
Name Required. The name for the new database. See Naming Requirements in Azure SQL Database General Guidelines and Limitations and Database Identifiers for more information.
Edition Optional. The Service Tier (Edition) for the new database. If omitted, the default is Web. Valid values are Web, Business, Basic, Standard, and Premium. See Azure SQL Database Service Tiers (Editions) and Web and Business Edition Sunset FAQ for more information.
CollationName Optional. The database collation. This can be any collation supported by SQL. If omitted, the default collation is used. See SQL Server Collation Support in Azure SQL Database General Guidelines and Limitations for more information.
MaxSizeBytes Optional. Sets the maximum size, in bytes, for the database. This value must be within the range of allowed values for Edition. If omitted, the default value for the edition is used. See Azure SQL Database Service Tiers (Editions) for current maximum databases sizes. Convert MB or GB values to bytes. 1 MB = 1048576 bytes. 1 GB = 1073741824 bytes.
ServiceObjectiveId Optional. The GUID corresponding to the performance level for Edition. See List Service Level Objectives (classic) for current values.

 Basic: dd6d99bb-f193-4ec1-86f2-43d3bccbc49c

 Standard (S0): f1173c43-91bd-4aaa-973c-54e79e15235b

 Standard (S1): 1b1ebd4d-d903-4baa-97f9-4ea675f5e928

 Standard (S2): 455330e1-00cd-488b-b5fa-177c226f28b7

 Standard (S3): 789681b8-ca10-4eb0-bdf2-e0b050601b40

 Premium (P1): 7203483a-c4fb-4304-9e9f-17c71c904f5d

 Premium (P2): a7d1b92d-c987-4375-b54d-2b1d0e0f5bb0

 Premium (P3): a7c4c615-cfb1-464b-b252-925be0a19446

 Web: Not recommended. See Web and Business Edition Sunset FAQ.

 Business: Not recommended. See Web and Business Edition Sunset FAQ.

Response

The response includes an HTTP status code, a set of response headers, and a response body.

Status Code

Response Headers

The response for this operation includes the following headers. The response may also include additional standard HTTP headers. All standard headers conform to the HTTP/1.1 protocol specification.

Response Header Description
x-ms-request-id A value that uniquely identifies a request made against the database management service. This request id is used for request tracking. If a failure occurs that requires the user to contact Microsoft Support, the request id should be provided to Microsoft to assist in tracking and resolving the failure for the request.

Response Body

For information about the response body, see Get Database (classic).

Example

The following console application creates a database on the server.

Replace the following values:

  • {subscriptionId} – replace with the subscription identifier for your subscription.

  • {serverName} – replace with the name of your server.

  • {thumbprint} – replace with the thumbprint of a management certificate in your subscription.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.IO;  
using System.Xml;  
using System.Security;  
using System.Security.Cryptography;  
using System.Security.Cryptography.X509Certificates;  
using System.Net;  
  
namespace CreateDatabase  
{  
    /// <summary>  
    /// Creates a database on a server.  
    /// </summary>  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Uri requestUri = new Uri("https://management.core.windows.net:8443/"  
                                   + "{subscriptionId}"  
                                   + "/services"  
                                   + "/sqlservers/servers/{serverName}/databases");  
  
            // Create the request and specify attributes.  
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);  
  
            // Add a x-ms-version header to specify API version.  
            request.Headers.Add("x-ms-version", "2012-03-01");  
  
            // Generate a request ID that can be used to identify this request in the service logs.  
            string clientRequestId = Guid.NewGuid().ToString();  
            request.Headers.Add("x-ms-client-request-id", clientRequestId);  
            request.Method = "POST";  
            request.ContentType = "application/xml";  
  
            // Build the request body.  
            string newSubscription = "{subscriptionId}";  
            string dbName = "newDatabaseName";  
            string edition = "Standard";  
            string maxSizeBytes = "10737418240";  
            string collationName = "SQL_Latin1_General_CP1_CI_AS";  
            string xmlBody = String.Format(  
                    "<ServiceResource xmlns=\"https://schemas.microsoft.com/windowsazure\">"  
                  + "<Name>{0}</Name>"  
                  + "<Edition>{1}</Edition>"  
                  + "<MaxSizeBytes>{2}</MaxSizeBytes>"  
                  + "<CollationName>{3}</CollationName>"  
                  + "</ServiceResource>", dbName, edition, maxSizeBytes, collationName);  
  
            // Write the POST body into the request.  
            ASCIIEncoding encoding = new ASCIIEncoding();  
            byte[] data = encoding.GetBytes(xmlBody);  
            Stream newStream = request.GetRequestStream();  
            newStream.Write(data, 0, data.Length);  
  
            // The thumbprint value of the management certificate.  
            // Replace {thumbprint} with the thumbprint of a management certificate associated with your subscription.  
            // It must also be installed on the machine accessing the API.  
            string certThumbprint = "{thumbprint}";  
  
            // Create a reference to the My certificate store.  
            X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);  
  
            // Open the store.  
            certStore.Open(OpenFlags.ReadOnly);  
  
            // Find the certificate that matches the thumbprint.  
            X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certThumbprint, false);  
            certStore.Close();  
  
            // Verify the certificate was added to the collection.  
            if (0 == certCollection.Count)  
            {  
                throw new Exception("Error: No certificate found with thumbprint " + certThumbprint);  
            }  
  
            // Create an X509Certificate2 object using the matching certificate.  
            X509Certificate2 certificate = certCollection[0];  
  
            // Attach the certificate to the request.  
            request.ClientCertificates.Add(certificate);  
  
            try  
            {  
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())  
                using (Stream responseStream = response.GetResponseStream())  
                using (StreamReader reader = new StreamReader(responseStream))  
                {  
                    // Display the client request ID.  
                    Console.WriteLine("clientRequestId: " + clientRequestId);  
  
                    // Display the web response status code.  
                    Console.WriteLine("Response status code: " + response.StatusCode);  
  
                    // Display the request ID returned by Windows Azure.  
                    Console.WriteLine("x-ms-request-id: "  
                        + response.Headers["x-ms-request-id"]);  
  
                    // Display the raw response.  
                    Console.WriteLine("Received response:");  
                    Console.WriteLine(reader.ReadToEnd());  
                }  
            }  
            catch (Exception e)  
            {  
                Console.WriteLine(e.StackTrace);  
            }  
        }  
    }  
}  

See Also

Common REST API Error Codes
Azure SQL Database
Operations for Azure SQL Databases
Azure SQL Database Cmdlets
New-AzureSqlDatabase