These types of keys are used for indexing, relational mapping, and other operations within databases. As we focus on exam DP-420: Designing and Implementing Native Applications using Microsoft Azure Cosmos DB, understanding these keys proves critical.
Understanding Primary Keys
A primary key, in its simplest form, is a unique identifier for records in a database. It’s a fundamental concept in database design and serves universally across different types of databases, including Microsoft Azure Cosmos DB.
Each record in a table must have a primary key. Primary keys encourage data integrity by ensuring each record is unique and can be quickly located. They also provide a point of connectivity, linking data across various tables in a relational database.
Here is a simple example of a primary key in a “Users” table:
UserID (Primary Key) | Name | |
---|---|---|
1 | John | john@email.com |
2 | Clara | clara@email.com |
3 | Mike | mike@email.com |
In this table, the ‘UserID’ acts as the primary key for each record.
In Azure Cosmos DB, you designate a partition key when creating a new container. The partition key is a property of the items within the Azure Cosmos DB container. It could also be seen as a special kind of primary key that Azure Cosmos DB uses to distribute data across several partitions.
Understanding Unique Keys
A unique key, on the other hand, isn’t a unique identifier for records, but it ensures the uniqueness of the data within its column or set of columns. Unlike primary keys, there can be various unique keys within a single table.
The unique key constraint provides a way to ensure that no column under the unique key constraint contains duplicate values. It helps maintain the integrity of a column or a set of columns in which we are interested in storing only distinct values.
Here is a simple example where “Email” acts as a unique key in the same “Users” table:
UserID (Primary Key) | Name | Email (Unique Key) |
---|---|---|
1 | John | john@email.com |
2 | Clara | clara@email.com |
3 | Mike | mike@email.com |
Here, each user’s email address acts as a unique identifier although it’s not necessarily a primary key. Note how the email field requires unique entries to avoid identity confusion.
Interestingly, Azure Cosmos DB, unlike most relational databases, does not directly support unique key constraints across multiple documents. However, developers can create ‘unique constraints’ at the application layer using stored procedures in Cosmos DB.
Primary Keys vs Unique Keys: A Comparison
Primary and unique keys are similar and ensure uniqueness in some aspects, but they have meaningful differences too:
Attribute | Primary Key | Unique Key |
---|---|---|
Uniqueness | Must be unique | Must be unique |
Null Values | Cannot be null | Can contain null |
Table allowance | Only one | Multiple per table allowed |
Types of Constraints | Clustered | Can be clustered or non-clustered |
Auto-increment feature | Possible | Not possible |
Understanding primary keys and unique keys is essential to modeling and working with data in any database system, including Azure Cosmos DB. By creating and implementing these keys correctly, developers can maintain data integrity and make data management tasks much more efficient.
For exam DP-420: Designing and Implementing Native Applications using Microsoft Azure Cosmos DB, candidates must not only understand these concepts but also know how Azure Cosmos DB implements these keys, or these concepts in terms of partitioning and indexing.
Practice Test
True/False: Primary key in Microsoft Azure Cosmos DB is mandatory for each item in the container.
- Answer: True
Explanation: Each item in Azure Cosmos DB must have a unique primary key within a partition. The primary key is comprised of one or more properties within the item.
True/False: Primary and Unique Keys in Azure Cosmos DB are the same.
- Answer: False
Explanation: While both Primary and Unique Keys assist in uniquely identifying items, they have different functionalities. A Primary Key is a mandatory key to identify an item, whereas Unique Keys enforce the uniqueness of one or more values within a logical partition.
In Azure Cosmos DB, a Primary Key __.
- a) Speeds up database engine operations.
- b) Can’t be a combination of fields.
- c) Can only be made up of alphanumeric characters.
- d) Cannot consist of more than one property.
- Answer: a) Speeds up database engine operations.
Explanation: Primary keys are used to speed up the reads and writes of data. However, they can consist of more than one property, be a combination of fields, and are not restricted to alphanumeric characters.
The Unique Key policy in Azure Cosmos DB is scoped to __ .
- a) Partition Key
- b) Primary Key
- c) Both a and b
- d) None of the above
- Answer: a) Partition Key.
Explanation: The uniqueness constraint is scoped to the partition key, which means the Azure Cosmos DB is able to ensure the uniqueness of each unique key within that logical partition.
True/False: Primary keys can be updated or modified in Azure Cosmos DB.
- Answer: False.
Explanation: Once set, primary keys cannot be modified or changed in Azure Cosmos DB.
Multi-select: Which of the following are supported key types for Azure Cosmos DB primary key?
- a) String
- b) Number
- c) Boolean
- d) Null
- Answer: a) String, b) Number.
Explanation: The supported key types for primary key in Azure Cosmos DB are string and number.
Single select: If you have a container with a defined primary partition key, which key could guarantee the uniqueness of an item in a partition.
- a) Partition Key
- b) Unique Key
- c) Foreign Key
- d) Composite Key
- Answer: b) Unique Key.
Explanation: Unique keys in Azure Cosmos DB provide a way to ensure the uniqueness of one or more values per logical partition.
True/False: Cosmos DB automatically indexes all properties for all items in a container without you having to deal with schema or index management.
- Answer: True.
Explanation: Azure Cosmos DB is a schema-agnostic database service. By default, it automatically indexes all properties in all items.
True/False: A container in Azure Cosmos DB can have more than one unique key policy.
- Answer: False.
Explanation: Each container in Azure Cosmos DB can have only one unique key policy, which may have multiple unique key constraints.
Single Select: In Azure Cosmos DB operations, what would a primary key provide for efficiency?
- a) Rapid retrieval of data
- b) Larger storage capacity
- c) More complex queries
- d) All of the above
- Answer: a) Rapid retrieval of data.
Explanation: The primary key in Azure Cosmos DB allows for rapid indexed accesses to data and enhances the speed of read and write operations.
True/False: A partition key and primary key in Azure Cosmos DB can be same.
- Answer: True.
Explanation: A partition key and primary key can be the same. The primary key can be a combination of some properties including the partition key, as well.
Interview Questions
What is a primary key in the context of Microsoft Azure Cosmos DB?
A primary key in Azure Cosmos DB is a special kind of identifier that is unique for every item in a container. It is used to ensure data integrity and enforce unique values in a specific set of fields.
What do you mean by a unique key in Azure Cosmos DB?
A unique key in Azure Cosmos DB is a user-defined set of one or more properties that Azure Cosmos DB guarantees will have unique values within a partition. No two items within a logical partition can have the same value for these properties.
What is the mandate while choosing a primary key in Cosmos DB?
The mandate while choosing a primary key in Cosmos DB is to ensure that the primary key values are distributed evenly. If all operations target a single partition key value, they will effectively become a bottleneck, limiting scalability.
What happens if we change the unique key policy in Azure Cosmos DB?
A unique key policy cannot be changed after you’ve created it in Azure Cosmos DB without dropping and recreating the entire container.
How many unique keys can we specify in an Azure Cosmos container?
An Azure Cosmos DB container can have up to 32 unique keys.
Can we have a primary key and a unique key on the same collection in Azure Cosmos DB?
Yes, it’s possible. Primary and unique keys are not mutually exclusive and can be used to manage data integrity in Azure Cosmos DB.
Is Primary Key enforcement supported in Azure Cosmos DB?
Yes. Azure Cosmos DB supports primary key enforcement to maintain data integrity.
Can a logical partition key contain more than one unique key in Azure Cosmos DB?
Yes, in Azure Cosmos DB, a logical partition key can contain more than one unique key.
Can we delete a primary key in Azure Cosmos DB?
No, primary keys cannot be deleted or altered in Azure Cosmos DB. To change a primary key, a new collection with the desired key would need to be created.
Is partition key the same as primary key in Azure Cosmos DB?
No, a partition key in Azure Cosmos DB is used to group data and distribute it across multiple physical partitions, while a primary key is designed to ensure uniqueness and integrity throughout the dataset.
Can a primary key contain null or empty values in Azure Cosmos DB?
No, a primary key in Azure Cosmos DB cannot contain null or empty values.
Can a unique key in Azure Cosmos DB be a composite key?
Yes, a unique key in Azure Cosmos DB can be a composite key that is made up of multiple properties.
Is ‘id’ always a primary key in Azure Cosmos DB?
No, ‘id’ is not always the primary key. ‘id’ is automatically enforced as a unique key within a partition by Azure Cosmos DB, but the primary key can be a different attribute or a composite key.
Can a unique key in Azure Cosmos DB have duplicate values in different logical partitions?
Yes, a unique key can have duplicate values across different logical partitions because the scope of a unique key constraint is confined to the logical partition.
How can unique constraints be implemented in Azure Cosmos DB?
Unique constraints can be implemented in Azure Cosmos DB by defining a unique key policy when creating a container. The policy lists the paths to the properties that should have unique values.