Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Table Storage - remove columns

I think this is not possible, but however I ask the question, maybe I have missed something.

Can we add/remove columns from an azure table?

For example by default we get those columns: PartitionKey, RowKey, Timestamp, ETag. Can I add for example another 3: FirstName, LastName, Email columns?

After that I will insert some values and I want to remove column Email and add instead column Address. Can we do this?

like image 794
user2818430 Avatar asked Dec 07 '22 23:12

user2818430


2 Answers

As Igor rightly said, Azure Tables do not have the concept of rows and columns. A table can contain zero or more entities and each entity can have a maximum of 255 attributes (an attribute is a name/value/type). Off of these 255 attributes, 3 of them are system attributes (PartitionKey, RowKey and Timestamp) which you can't update through your code. When creating an entity you define PartitionKey and RowKey and after that they become readonly properties. So when it comes to updating an entity, you can only update 252 attributes.

To manage data in an Azure Table, there's a REST API and you provide attributes of an entity in the request body. Azure Storage provides two ways by which you can update an entity - Update and Merge.

When you tell Azure Table Service to Update an entity, it simply drops all the existing attributes for that entity, and inserts the attributes defined in the request payload.

When you tell Azure Table Service to Merge an entity, it looks at the existing entity attributes and compares them with the attributes defined in the request payload. If it finds matching attributes, it simply updates those attributes. If the attributes are not present in existing entity but are defined in request payload, those attributes get added to the entity. If the attribute are present in existing entity but are not defined in request payload, they are not changed.

Now coming to your problem.

So let's say you already have an entity where you just defined PartitionKey and RowKey. Now you want to add FirstName, LastName, Email attributes. Because these attributes are not there in the entity, you can either use Merge or Replace to update the entity and these attributes will be added to the entity.

Now you want to drop Email attribute from an entity and instead add Address attribute to that entity. What you will do is perform an Update operation on that entity where your request body will have FirstName, LastName and Address attributes only (no Email attribute). When you update the entity with this request payload, Email attribute will be removed from the entity.

like image 61
Gaurav Mantri Avatar answered Jan 04 '23 23:01

Gaurav Mantri


Azure Tables do not have "columns" as SQL tables do. Azure Tables have entities. Each entity has up to 255 properties. Most tools that let you look at an Azure table, choose to visualize the data in it tabular with columns. However, in reality, each entity (row) is a collection of properties.

Therefore, you can save objects/entities into an Azure Table with different properties, everytime you do a save. It makes things somewhat confusing, but you can do it.

HTH

like image 43
Igorek Avatar answered Jan 04 '23 23:01

Igorek