# Create a Data Extension

Use a data extension to store information about your subscribers.

## Data Extension Considerations

Keep these considerations in mind when you use SOAP API to create data extensions:

- Limit data extension external keys to 36 characters. You can specify an external key that exceeds this limit, but doing so can negatively impact other processes.
- Don't use `Date` values as primary keys. When you retrieve a column that contains the `Date` data type, SOAP API returns a value that’s rounded to the nearest second, followed by AM or PM. This behavior occurs even when the source timestamp contains millisecond data. As a result, if you use a `Date` column as a primary key, it can prevent you from retrieving unique rows.

## Sample SOAP Create Request

This sample SOAP envelope represents a request to create a data extension called DataExtensionFromAPI. The data extension contains 4 fields: EmailAddress, ChannelUser, ChannelUser_EmailAddress, and Demographic_Address.

```xml
<soapenv:Body>
  <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
    <Options></Options>
    <Objects
      xmlns:ns1="http://exacttarget.com/wsdl/partnerAPI"
      xsi:type="ns1:DataExtension">
      <CustomerKey>DataExtensionFromAPI</CustomerKey>
      <Name>DataExtensionFromAPI</Name>
      <IsSendable>true</IsSendable>
      <SendableDataExtensionField>
        <CustomerKey>EmailAddress_Key</CustomerKey>
        <Name>EmailAddress</Name>
        <FieldType>EmailAddress</FieldType>
      </SendableDataExtensionField>
      <SendableSubscriberField>
        <Name>Email Address</Name>
        <Value></Value>
      </SendableSubscriberField>
      <Fields>
        <Field>
          <CustomerKey>EmailAddress_Key</CustomerKey>
          <Name>EmailAddress</Name>
          <FieldType>EmailAddress</FieldType>
        </Field>
        <Field>
          <CustomerKey>ChannelUser_Key</CustomerKey>
          <Name>ChannelUser</Name>
          <FieldType>Text</FieldType>
        </Field>
        <Field>
          <CustomerKey>ChannelUser_EmailAddress_Key</CustomerKey>
          <Name>ChannelUser_EmailAddress</Name>
          <FieldType>EmailAddress</FieldType>
        </Field>
        <Field>
          <CustomerKey>Demographic_Address_Key</CustomerKey>
          <Name>Demographic_Address</Name>
          <FieldType>Text</FieldType>
        </Field>
      </Fields>
    </Objects>
  </CreateRequest>
</soapenv:Body>
```

## Sample SOAP Create Response

If the API accepts the request to create a data extension, the response includes the external key, name, and fields for the data extension.

```xml
<soap:Body>
  <CreateResponse xmlns="http://exacttarget.com/wsdl/partnerAPI">
    <Results>
      <StatusCode>OK</StatusCode>
      <StatusMessage>Data Extension created.</StatusMessage>
      <OrdinalID>0</OrdinalID>
      <NewID>0</NewID>
      <NewObjectID>1a3e2c14-580e-de11-b30f-001cc494ae9e</NewObjectID>
      <Object xsi:type="DataExtension">
        <ObjectID>1a3e2c14-580e-de11-b30f-001cc494ae9e</ObjectID>
        <CustomerKey>DataExtensionFromAPI</CustomerKey>
        <Name>DataExtensionFromAPI</Name>
        <IsSendable>true</IsSendable>
        <SendableDataExtensionField>
          <ObjectID xsi:nil="true" />
          <CustomerKey>EmailAddress_Key</CustomerKey>
          <Name>EmailAddress</Name>
          <FieldType>EmailAddress</FieldType>
        </SendableDataExtensionField>
        <SendableSubscriberField>
          <Name>Email Address</Name>
          <Value />
        </SendableSubscriberField>
        <Fields>
          <Field>
            <ObjectID xsi:nil="true" />
            <CustomerKey>EmailAddress_Key</CustomerKey>
            <Name>EmailAddress</Name>
            <FieldType>EmailAddress</FieldType>
          </Field>
          <Field>
            <ObjectID xsi:nil="true" />
            <CustomerKey>ChannelUser_Key</CustomerKey>
            <Name>ChannelUser</Name>
            <FieldType>Text</FieldType>
          </Field>
          <Field>
            <ObjectID xsi:nil="true" />
            <CustomerKey>ChannelUser_EmailAddress_Key</CustomerKey>
            <Name>ChannelUser_EmailAddress</Name>
            <FieldType>EmailAddress</FieldType>
          </Field>
          <Field>
            <ObjectID xsi:nil="true" />
            <CustomerKey>Demographic_Address_Key</CustomerKey>
            <Name>Demographic_Address</Name>
            <FieldType>Text</FieldType>
          </Field>
        </Fields>
      </Object>
    </Results>
    <RequestID>27a2d45f-0eef-4118-b4de-fde288b09657</RequestID>
    <OverallStatus>OK</OverallStatus>
  </CreateResponse>
</soap:Body>
```

## Create a Data Extension with Non-Nullable Fields

This sample SOAP envelope creates a data extension with a non-nullable field by including `IsPrimaryKey` and `IsRequired` values.

```xml
<soapenv:Body>
  <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
    <Options />
    <Objects xsi:type="ns2:DataExtension"
      xmlns:ns2="http://exacttarget.com/wsdl/partnerAPI">
      <CustomerKey>yoni_test_10</CustomerKey>
      <Name>yoni_test_1</Name>
      <Description>Created from API on 2024-02-08</Description>
      <IsSendable>true</IsSendable>
      <IsTestable>true</IsTestable>
      <SendableDataExtensionField xsi:type="ns2:DataExtensionField">
        <Name>userid</Name>
        <FieldType>EmailAddress</FieldType>
      </SendableDataExtensionField>
      <SendableSubscriberField>
        <Name>Subscriber Key</Name>
        <Value />
      </SendableSubscriberField>
      <Fields>
        <Field xsi:type="ns2:DataExtensionField">
          <CustomerKey>userid</CustomerKey>
          <Name>userid</Name>
          <Label>userid</Label>
          <IsRequired>true</IsRequired>
          <IsPrimaryKey>true</IsPrimaryKey>
          <FieldType>EmailAddress</FieldType>
        </Field>
        <Field xsi:type="ns2:DataExtensionField">
          <CustomerKey>geoid</CustomerKey>
          <Name>geoid</Name>
          <DataType>Number</DataType>
          <Label>geoid</Label>
          <FieldType>Number</FieldType>
        </Field>
        <Field xsi:type="ns2:DataExtensionField">
          <CustomerKey>order</CustomerKey>
          <Name>order</Name>
          <DataType>Number</DataType>
          <Label>order</Label>
          <FieldType>Number</FieldType>
        </Field>
        <Field xsi:type="ns2:DataExtensionField">
          <CustomerKey>url</CustomerKey>
          <Name>url</Name>
          <DataType>Text</DataType>
          <Label>url</Label>
          <MaxLength>1000</MaxLength>
          <IsNillable>true</IsNillable>
          <IsPrimaryKey>false</IsPrimaryKey>
          <FieldType>Text</FieldType>
        </Field>
        <Field xsi:type="ns2:DataExtensionField">
          <CustomerKey>time</CustomerKey>
          <Name>time</Name>
          <DataType>Date</DataType>
          <Label>time</Label>
          <IsNillable>true</IsNillable>
          <IsPrimaryKey>false</IsPrimaryKey>
          <FieldType>Date</FieldType>
        </Field>
      </Fields>
    </Objects>
  </CreateRequest>
</soapenv:Body>
```

## Create a Data Extension that uses Different Field Types

This sample SOAP envelope includes an example of data extension field types.

```xml
<soapenv:Body>
  <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
    <Options />
    <Objects xsi:type="DataExtension">
      <PartnerKey xsi:nil="true" />
      <ObjectID xsi:nil="true" />
      <Name>FieldTypeTest</Name>
      <IsSendable>false</IsSendable>
      <Fields>
        <Field>
          <Name>EmailAddress_type</Name>
          <FieldType>EmailAddress</FieldType>
          <IsPrimaryKey>true</IsPrimaryKey>
          <IsRequired>true</IsRequired>
        </Field>
        <Field>
          <Name>Text_type</Name>
          <FieldType>Text</FieldType>
          <IsRequired>false</IsRequired>
        </Field>
        <Field>
          <Name>Date_type</Name>
          <FieldType>Date</FieldType>
          <DefaultValue>getdate()</DefaultValue>
          <IsRequired>true</IsRequired>
        </Field>
        <Field>
          <Name>Decimal_type</Name>
          <FieldType>Decimal</FieldType>
          <IsRequired>true</IsRequired>
          <Precision>10</Precision>
          <Scale>2</Scale>
        </Field>
        <Field>
          <Name>Boolean_type</Name>
          <FieldType>Boolean</FieldType>
          <DefaultValue>true</DefaultValue>
          <IsRequired>true</IsRequired>
        </Field>
        <Field>
          <Name>Number_type</Name>
          <FieldType>Number</FieldType>
          <IsRequired>false</IsRequired>
        </Field>
        <Field>
          <Name>Phone_type</Name>
          <FieldType>Phone</FieldType>
          <IsRequired>false</IsRequired>
        </Field>
        <Field>
          <Name>Locale_type</Name>
          <FieldType>Locale</FieldType>
          <IsRequired>false</IsRequired>
        </Field>
      </Fields>
      <CustomerKey>FieldTypeTest</CustomerKey>
    </Objects>
  </CreateRequest>
</soapenv:Body>
```

## Create a Data Extension with Row-Based Data Retention

This example shows how to create a data extension that uses row-based data retention. In a data extension that uses row-based retention, individual rows are deleted when their ages exceed the specified period.

```xml
<s:Envelope
  xmlns:s="http://www.w3.org/2003/05/soap-envelope"
  xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
  xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <s:Header>
    <a:Action s:mustUnderstand="1">Create</a:Action>
    <a:To s:mustUnderstand="1">https://YOUR_SUBDOMAIN.soap.marketingcloudapis.com/Service.asmx</a:To>
    <fueloauth xmlns="http://exacttarget.com">YOUR_ACCESS_TOKEN</fueloauth>
  </s:Header>
  <s:Body
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
      <Objects xsi:type="DataExtension">
        <Client>
          <ID>987654</ID>
        </Client>
        <CustomerKey>c7b0df2e-e34a-11ef-a842-325096b39f69</CustomerKey>
        <Name>DE with Row-Based Retention</Name>
        <IsSendable>true</IsSendable>
        <SendableDataExtensionField>
          <CustomerKey>SubscriberKey</CustomerKey>
          <Name>SubscriberKey</Name>
          <FieldType>Text</FieldType>
        </SendableDataExtensionField>
        <SendableSubscriberField>
          <Name>Subscriber Key</Name>
          <Value></Value>
        </SendableSubscriberField>
        <DataRetentionPeriodLength>48</DataRetentionPeriodLength>
        <DataRetentionPeriod>Days</DataRetentionPeriod>
        <RowBasedRetention>true</RowBasedRetention>
        <Fields>
          <Field>
            <CustomerKey>SubscriberKey</CustomerKey>
            <Name>SubscriberKey</Name>
            <FieldType>Text</FieldType>
            <MaxLength>50</MaxLength>
            <IsRequired>true</IsRequired>
            <IsPrimaryKey>true</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>First_Name</CustomerKey>
            <Name>First_Name</Name>
            <FieldType>Text</FieldType>
            <MaxLength>50</MaxLength>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>Last_Name</CustomerKey>
            <Name>Last_Name</Name>
            <FieldType>Text</FieldType>
            <MaxLength>50</MaxLength>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>Email</CustomerKey>
            <Name>Email</Name>
            <FieldType>EmailAddress</FieldType>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>Rewards_Points</CustomerKey>
            <Name>Rewards_Points</Name>
            <FieldType>Number</FieldType>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
        </Fields>
      </Objects>
    </CreateRequest>
  </s:Body>
</s:Envelope>
```

## Create a Data Extension with Object-Based Data Retention

This example shows how to create a data extension that uses object-based data retention. In a data extension that uses object-based retention, all of the content in the data extension is deleted after the specified period. Optionally, you can delete the data extension itself (rather than just the data within it) at the end of the retention period by setting `DeleteAtEndOfRetentionPeriod` to `false`. If you set this to `true`, only the content is deleted after the specified period, but the data extension persists.

Compared to the row-based retention code example, this example omits the `RowBasedRetention` property and adds the `ResetRetentionPeriodOnImport` and `DeleteAtEndOfRetentionPeriod` properties.

```xml
<s:Envelope
  xmlns:s="http://www.w3.org/2003/05/soap-envelope"
  xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
  xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <s:Header>
    <a:Action s:mustUnderstand="1">Create</a:Action>
    <a:To s:mustUnderstand="1">https://YOUR_SUBDOMAIN.soap.marketingcloudapis.com/Service.asmx</a:To>
    <fueloauth xmlns="http://exacttarget.com">YOUR_ACCESS_TOKEN</fueloauth>
  </s:Header>
  <s:Body
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
      <Objects xsi:type="DataExtension">
        <Client>
          <ID>987654</ID>
        </Client>
        <CustomerKey>c7b0df2e-e34a-11ef-a842-325096b39f69</CustomerKey>
        <Name>DE with Object-Based Retention</Name>
        <IsSendable>true</IsSendable>
        <SendableDataExtensionField>
          <CustomerKey>SubscriberKey</CustomerKey>
          <Name>SubscriberKey</Name>
          <FieldType>Text</FieldType>
        </SendableDataExtensionField>
        <SendableSubscriberField>
          <Name>Subscriber Key</Name>
          <Value></Value>
        </SendableSubscriberField>
        <DataRetentionPeriodLength>48</DataRetentionPeriodLength>
        <DataRetentionPeriod>Days</DataRetentionPeriod>
        <ResetRetentionPeriodOnImport>true</ResetRetentionPeriodOnImport>
        <DeleteAtEndOfRetentionPeriod>false</DeleteAtEndOfRetentionPeriod>
        <Fields>
          <Field>
            <CustomerKey>SubscriberKey</CustomerKey>
            <Name>SubscriberKey</Name>
            <FieldType>Text</FieldType>
            <MaxLength>50</MaxLength>
            <IsRequired>true</IsRequired>
            <IsPrimaryKey>true</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>First_Name</CustomerKey>
            <Name>First_Name</Name>
            <FieldType>Text</FieldType>
            <MaxLength>50</MaxLength>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>Last_Name</CustomerKey>
            <Name>Last_Name</Name>
            <FieldType>Text</FieldType>
            <MaxLength>50</MaxLength>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>Email</CustomerKey>
            <Name>Email</Name>
            <FieldType>EmailAddress</FieldType>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
          <Field>
            <CustomerKey>Rewards_Points</CustomerKey>
            <Name>Rewards_Points</Name>
            <FieldType>Number</FieldType>
            <IsRequired>false</IsRequired>
            <IsPrimaryKey>false</IsPrimaryKey>
          </Field>
        </Fields>
      </Objects>
    </CreateRequest>
  </s:Body>
</s:Envelope>
```

## Create a Data Extension that Includes a Date Value in a Date-type Field

When you create a data extension field that contains a date value, pass the time in 24-hour format.

```
<CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
  <Options/>
  <Objects xsi:type="DataExtensionObject">
    <ObjectID xsi:nil="true"/>
    <CustomerKey>DateFormatTest</CustomerKey>
    <Properties>
      <Property>
        <Name>DateField</Name>
        <Value><![CDATA[01/02/2024 16:00:00]]></Value>
      </Property>
    </Properties>
  </Objects>
</CreateRequest>
```

## See Also

- [Retrieve Data from a Data Extension](/docs/marketing/marketing-cloud/guide/retrieving_data_from_a_data_extension.md)
