When working with tables in Microsoft Power Platform, especially in the context of the PL-200 Microsoft Power Platform Functional Consultant exam, it is crucial to understand how to create alternate keys for tables. Alternate keys help facilitate data integration by ensuring the uniqueness of records and allowing an entity to be identified by an attribute other than the primary key (e.g., email address, customer number, etc.).
Understanding Alternate Keys
In the data structure, each table consists of a primary key, which uniquely identifies each record. However, in many business scenarios, primary keys alone may not be sufficient to prevent duplicate records. This is where alternate keys come into play.
Alternate keys are user-defined unique keys that are instrumental in synchronizing and integrating data with external systems. They not only enforce uniqueness but also improve the performance of Retrieve, RetrieveMultiple, Update, and Delete operations.
Creation of Alternate Keys
Creating alternate keys involves several steps. These include:
- Specify Entity: Choose the entity for which you want to create the alternate key.
- Define Key: In the Keys tab, click ‘New’ to define a new alternate key. You are required to provide a display name and the schema name, both of which must be unique within the entity.
- Add Attributes: Next, add one or more attributes to the key. Remember, the combination of values in these attributes must be unique across all records in the entity.
- Save and Activate: Save the key and activate it. Once activated, a system job runs in the background to validate the uniqueness across all records in the entity.
It’s important to note that you can’t deactivate or delete a key if it’s used in an entity relationship or if it’s part of a managed solution.
Let’s use the Contact entity as an example. Assume you want to ensure that each contact is uniquely identified by a combination of their FirstName, LastName, and Email fields. Here is how you would create an alternate key:
<code>
// specify entity
var entityMetadata = new EntityMetadata { SchemaName = “contact” };
// define key
var keyMetadata = new KeyMetadata
{
SchemaName = “new_ContactAlternateKey”,
DisplayName = new Label(“Contact Alternate Key”, 1033)
};
// add attributes
keyMetadata.KeyAttributes = new StringCollection
{
“firstname”,
“lastname”,
“emailaddress1”
};
// add the key to the entity and create it
entityMetadata.Keys = new KeyMetadata[] { keyMetadata };
_serviceProxy.Create(entityMetadata);
</code>
In this example, the system will throw an error if you try to create a new contact with the same combination of FirstName, LastName, and Email as an existing one.
Conclusion
Understanding and effectively using alternate keys in Power Platform can improve data integrity, facilitate data integration, and enhance performance. It is an essential skill for anyone preparing for the PL-200 Microsoft Power Platform Functional Consultant exam.
The key to creating successful alternate keys lies in identifying the appropriate attributes that accurately and uniquely define each record. It requires a fundamental understanding of the business context and anticipated usage of the data in the system.
Practice Test
True or false: It is not possible to create an alternate key for a table in Microsoft Power Platform.
- True
- False
Answer: False
Explanation: Microsoft Power Platform support the creation of alternate keys for a table, which provides a unique identifier to each record and helps in data manipulation.
Select the correct statement for alternate key:
- a. It can be used to identify a record uniquely in a table
- b. Alternate keys consist of one or more fields/columns in a table
- c. Both (a) and (b)
Answer: c. Both (a) and (b)
Explanation: An alternate key is composed of one or more fields in a table, which uniquely identifies a record.
True or False: You can use special characters when defining an alternate key?
- True
- False
Answer: False
Explanation: You cannot use special characters (such as “! @ # $ % ^ & * ( )” etc.), spaces, or the use of only numbers when defining an alternate key.
In Microsoft Power Platform, what happens when you create an alternate key in table?
- a. It creates a unique constraint on the table
- b. It creates a duplicate constraint on the table
- c. It creates a null value constraint on the table
- d. It creates a foreign key constraint on the table
Answer: a. It creates a unique constraint on the table
Explanation: An alternate key in Power Platform sets a unique constraint on the table, ensuring that each record in the table is unique.
True or false: You can define multiple alternate keys for one table.
- True
- False
Answer: True
Explanation: It is possible to define more than one alternate key for a single table. This can enable more efficient and flexible data management and retrieval.
Multiple select: Which of the following are benefits of creating alternate keys?
- a. Simplify data lookups
- b. Increase data redundancy
- c. Increase data integrity
- d. Simplify data management
Answer: a. Simplify data lookups, c. Increase data integrity, d. Simplify data management
Explanation: Alternate keys simplify data lookups, management and increase data integrity by ensuring uniqueness of records.
True or false: Alternate keys allow users to identify and retrieve records using non-primary key data.
- True
- False
Answer: True
Explanation: The primary use of alternate keys is to locate and retrieve records using unique data which is not the primary key.
True or False: You do not have to publish the changes after adding an alternate key for a table.
- True
- False
Answer: False
Explanation: After adding an alternate key for a table, it is necessary to publish the changes.
Regarding alternate keys in Microsoft Power Platform, you can create an alternate key using:
- a. Single field
- b. Multiple fields
- c. Both a and b
Answer: c. Both a and b
Explanation: An alternate key can be based on a single field or combination of multiple fields.
What happens if there is a duplicate value in a field that is part of an alternate key?
- a. The system will duplicate the record
- b. Records with duplicate values are ignored
- c. The system will throw an error
- d. None of the above
Answer: c. The system will throw an error
Explanation: When an attempt is made to insert or update a record with a duplicate value in an alternate key field, the system will throw an error.
Interview Questions
What is the main function of alternate keys in tables?
The main function of alternate keys is to ensure data integrity in tables. They provide a way to prevent duplicate records based on fields other than the primary key.
True or False: An alternate key can include multiple columns, each of which may have different data types.
True. An alternate key can indeed consist of more than one column, but each column must be of a data type that can form a key.
Can alternate keys be considered as a unique key in SQL?
Yes, alternate keys in SQL are unique keys, that is, they uniquely identify a record in the database.
How many alternate keys can you have per table?
There can be multiple alternate keys per table.
What happens when you try to insert a duplicate value into an alternate key in a table?
When you attempt to insert a duplicate value into a field that is part of an alternate key, the platform enforces uniqueness and prevents the creation or update of the record.
Are alternate keys automatically indexed?
Yes, alternate keys are automatically indexed, improving query performance when filtering on alternate keys.
How are alternate keys used in Microsoft Power Platform?
In the Microsoft Power Platform, alternate keys can be used for upserting records and also for looking up records, apart from maintaining uniqueness in records.
Are there any limitations on the data type for fields that can be part of an alternate key?
Yes, only those fields having data types that can form a key can be used to create an alternate key.
Can Boolean type data be used to create an alternate key?
No, Boolean type data cannot be used to create an alternate key.
How do you create an alternate key in Microsoft Power Platform?
To create an alternate key in Microsoft Power Platform, you go to the Power Apps portal, choose solutions, select the custom solution that includes the entity, choose configure, select keys, and then go on to add the alternate key.
Can you delete an alternate key in Microsoft Power Platform?
Yes, you can delete an alternate key in Microsoft Power Platform, but you have to be sure that it is not being referenced anywhere before deleting it.
Can you modify an existing alternate key in Microsoft Power Platform?
No, you cannot modify an existing alternate key. If you need to make changes, you must delete the key and then recreate it.
Can a relationship be created on an alternate key?
Yes, you can create relationships on alternate keys.
Why would you create an alternate key on a table rather than a primary key?
You may want to create an alternate key on a table when you want to enforce uniqueness for a field other than the primary key and ensure relational integrity for lookup fields and relationships.
Can you create an alternate key on a system-maintained field?
No, you cannot create an alternate key on a system-maintained field.