Tüm Ürünler
Search
Belge Merkezi

Tablestore:Auto-increment primary key column

Son Güncelleme:Oct 02, 2026

Bu konu, .NET SDK kullanarak bir veri tablosu için otomatik artan birincil anahtar sütununun nasıl yapılandırılacağını ve bu sütuna veri yazarken üretilen değerlerin nasıl alınacağını açıklar.

Kullanım notları

Otomatik artan bir sütun için oluşturulan değerler, bölüm anahtarı düzeyinde benzersizdir ve kesinlikle artan sıradadır. Ancak, bu değerlerin ardışık olması garanti edilmez.

Ön koşullar

Tablestore İstemcisini Başlatın

Otomatik artan birincil anahtar sütunu yapılandırma

Bir veri tablosu oluştururken bölüm olmayan bir birincil anahtar sütununu otomatik artan sütun olarak yapılandırabilirsiniz. Mevcut veri tablolarında otomatik artan sütunlar yapılandıramazsınız.

Not

Yalnızca tamsayı türündeki bölüm olmayan birincil anahtar sütunlarını otomatik artan sütun olarak yapılandırabilirsiniz. Bir veri tablosunda en fazla bir otomatik artan sütun bulunabilir. Otomatik artan sütun için oluşturulan değerler 64 bitlik işaretli uzun tamsayılardır (long integer).

Örnek kod

Aşağıdaki örnek kod, test_table adlı bir veri tablosu oluşturur. Tablonun birincil anahtarı, bölüm anahtarı id ve otomatik artan sütun incr öğelerini içerir.

public static void CreateSample(OTSClient client)
{
    try
    {
        PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
        {
            { "id", ColumnValueType.String },
            { "incr", ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT}
        };
        TableMeta tableMeta = new TableMeta("test_table", primaryKeySchema);
        CapacityUnit reservedThroughput = new CapacityUnit(0, 0);

        CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
        client.CreateTable(request);
        Console.WriteLine("Create table succeeded.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Create table failed, exception:{ex.Message}");
    }
}

Veri yazma

Otomatik artan bir sütuna veri yazarken sütun değerini bir yer tutucu olarak ayarlayın. Sorgulama ve güncelleme işlemleri için oluşturulan otomatik artan değeri almak üzere RowPutChange öğesinin dönüş türünü ReturnType.RT_PK olarak ayarlayın.

Örnek kod

Aşağıdaki örnek kod, test_table tablosuna bir satır veri yazar ve yazılan satır verilerinin birincil anahtar bilgilerini alıp ekrana yazdırır.

public static void PutRowSample(OTSClient client)
{
    try
    {
        // Construct the primary key
        PrimaryKey primaryKey = new PrimaryKey
        {
            { "id", new ColumnValue("row1") },
            { "incr",  ColumnValue.AUTO_INCREMENT }
        };

        // Construct the row data to write
        AttributeColumns attribute = new AttributeColumns
        {
            { "col1", new ColumnValue("val1") }
        };
        PutRowRequest request = new PutRowRequest("test_table", new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
        // Set the return type to ReturnType.RT_PK to return the primary key information of the written row data
        request.RowPutChange.ReturnType = ReturnType.RT_PK;

        // Call the PutRow method to write the row data
        PutRowResponse response = client.PutRow(request);

        // RequestId and consumed CU
        Console.WriteLine("RequestId: " + response.RequestID);
        Console.WriteLine("Read CU Cost: " + response.ConsumedCapacityUnit.Read);
        Console.WriteLine("Write CU Cost: " + response.ConsumedCapacityUnit.Write);

        // Get and print the returned primary key information. If the return type is not set to ReturnType.RT_PK, the primary key information is not returned by default
        Console.WriteLine(response.Row.PrimaryKey.ToString());
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Put Row failed, exception:{ex.Message}");
    }
}