# How to change the key in Data Type

**URL:** <https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438>\
**Category:** General\
**Tags:** 8-4-1, data-integration\
**Created:** [May 27, 2021, 2:22pm UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438 "2021-05-27T14:22:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KenshoTsuchihashi](https://yyz2.discourse-cdn.com/pega/user_avatar/forums.pega.com/kenshotsuchihashi/32/16482_2.png) [@KenshoTsuchihashi](https://forums.pega.com/u/KenshoTsuchihashi)\
**Post date:** [May 27, 2021, 2:22pm UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438/1 "2021-05-27T14:22:58Z")

</div>

Hi,

Sometimes database table design is changed for many reasons and developers may need to change the key defined in Data Type. If change is immediately after Data Type is created, you may simply delete all the rules generated and define Data Type from scratch again. However, what if you are asked to change the key a while after Data Type is defined, and all the rules are already locked in the old RSV? Actually, whether the rule is locked or not does not matter for changing the key. In this post, I will share the detailed steps.

- **Understanding “Key”**

Primary key, or just “key” is defined at two levels - one for Pega level, and one for DBMS level. The key in each level can be different - for example, for Work- derived class, “pyID” is used for the default key in Pega while primary key in DBMS is “pzInsKey”. For Blob table, regardless of which class it’s derived from (Work- or Data-), “pzInsKey” is used as a primary key at DBMS level. Since starting from Pega 7.4 CustomerData is adopted for Data Type’s default database ([https://collaborate.pega.com/discussion/customerdata-non-blob-vs-pegadata-blob](https://collaborate.pega.com/discussion/customerdata-non-blob-vs-pegadata-blob)), the out-of-the-box table that is generated by Data Type is Non-Blob. That means not only “pzPVStream” but “pzInsKey” does not exist as well. Whatever custom property you specify as a key in Data Type is configured as a Primary key in the DBMS as below. In this example, I have set MacAddress property as a key.

 ![Key in Data Type.png](https://pega1.discourse-cdn.com/pega/original/2X/f/f5b327fe0cac0a04386a5fce25e81bdbee2b3e92.png)

If you look at the physical table in DBMS, Primary key is configured on macaddress automatically.

 ![MacAddress is a Primary Key.png](https://pega1.discourse-cdn.com/pega/original/2X/1/1e81b7506cfd9db245eaeb52b706c91b2addeca3.png)

In Pega level, the key MacAddress is found at class form. Be noted that the key field is grayed-out, and this is because the table is already populated. The key is not editable when the table has records.

 ![Mac Address Key in Class.png](https://pega1.discourse-cdn.com/pega/original/2X/6/686a36006427d2dc2b21634c5476a1314b200cec.png)

- **How to change the key**

Let’s say, RSV is locked and requirement is changed - you are asked to change the key from MacAddress to SerialNo. Now all the rules are locked, but can we achieve this without modifying rules? Yes, we can. Here are the steps.

1. As explained, key in Pega is not editable in class form as long as the physical table has records. So, the first step is to truncate the table. If table contains a lot of important records, you may want to get backup manually. Here, I used pgAdmin tool to export all the records into a CSV file.

 ![Export records to CSV file.png](https://pega1.discourse-cdn.com/pega/original/2X/7/75f9c25dda5f8a09fb35d42c78808706986b231d.png)

1. In the dialog, specify Filename and Format for exporting. Click OK.

 ![Filename and Format.png](https://pega1.discourse-cdn.com/pega/original/2X/2/2862fcf9cf152471e5cef91e3537a7a0e2ba0219.png)

1. All records are exported into a CSV file.

 ![Exported CSV file.png](https://pega1.discourse-cdn.com/pega/original/2X/a/af91e9a2c696e6982e5825bb795fb6fd5f6161d5.png)

1. Truncate the table.

 ![Truncate Table.png](https://pega1.discourse-cdn.com/pega/original/2X/8/8ce27e6dfa1d2331e7f068058238e8d6670aa52a.png)

1. Open the class form from Dev Studio. The key field should be now editable because table has no records any more. Be noted that Class is a special type of rule instance, and there is no check in or check out concept here - even when RSV is locked, this rule is always savable like Data instance. Replace MacAddress with SerialNo and Save the changes.

 ![Serial No Key in Class.png](https://pega1.discourse-cdn.com/pega/original/2X/6/6b5c512e4985fb23731e2fd3506a1ee52274bd35.png)

1. Per my experiment, this operation does not automatically incur Primary key change in the DBMS level. Try hitting “Test Connection” button in the class form, and you will find an error message saying, "_There is a key mismatch: the keys defined for class MyCo-MyApp-Data-Computer do not match the primary key columns for external data table data.pr\_myco\_myapp\_data\_computer in database CustomerData; the class keys are [SerialNo], but the table’s primary keys are [data.macaddress]"_ as below\*.\* This implies that key change is still an irregular operation and change has to be done manually from DBMS.

 ![Key mismatch detected.png](https://pega1.discourse-cdn.com/pega/original/2X/f/f5d82e8b09766ade137595e05d8c3ad7dc504080.png)

1. From DBMS, drop the current Primary key by DDL. You can also use pgAdmin tool to do so.

 ![Drop Primary Key in DBMS.png](https://pega1.discourse-cdn.com/pega/original/2X/1/114e58fd1a4ef560d26289f3a58190722d1c0791.png)

1. Recreate a Primary key.

 ![Create Primary Key.png](https://pega1.discourse-cdn.com/pega/original/2X/6/60044ceeab026b57cae7e440b4f4b7cd50ab3cad.png)

1. Name the Primary key. It can be the same name as the original one. In this example, “pr\_myco\_myapp\_data\_computer\_pk”.

 ![Name Primary key.png](https://pega1.discourse-cdn.com/pega/original/2X/1/1069dac8489d4233bde25395d22f00153220a360.png)

1. Specify serialno for Columns.

 ![Columns SerialNo.png](https://pega1.discourse-cdn.com/pega/original/2X/4/46aac13fe7ba0e1eb543aa3bc095cd67d74678e0.png)

1. Make sure serialno is now Primary key in the DBMS.

 ![SerialNo Key in Table.png](https://pega1.discourse-cdn.com/pega/original/2X/b/b4bfa7c8143b72c9ec0489f2d99b7237e536b8df.png)

1. Try “Test Connection” again and find the mismatch error message is now gone. This means key is SerialNo at both Pega level and DBMS level in sync.

 ![Successful Test Connection.png](https://pega1.discourse-cdn.com/pega/original/2X/5/50809d31a83d6374f8cc9980133b67791ef839df.png)

1. Final work left is to restore the backup records into the table. Select Import from pgAdmin tool.

 ![Import records.png](https://pega1.discourse-cdn.com/pega/original/2X/e/eb474c7f8a13b47146b76d665e5b4b2cd9d1e6f3.png)

1. Switch to Import and specify the CSV file. Click OK.

 ![Specify Filename and Format for Import.png](https://pega1.discourse-cdn.com/pega/original/2X/e/eabed2d9811315a664af3a9959ff87885db98764.png)

1. Make sure all the records are restored.

 ![Records are all restored.png](https://pega1.discourse-cdn.com/pega/original/2X/f/fed3b47f46ef4ec6e725194f9f82bc35b00c0428.png)

This concludes my explanation. Hope this helps.

Thanks,

---

<div class="post-metadata">

**Author:** ![Dev.Pega007](https://yyz2.discourse-cdn.com/pega/user_avatar/forums.pega.com/dev.pega007/32/20950_2.png) [@Dev.Pega007](https://forums.pega.com/u/Dev.Pega007)\
**Post date:** [May 10, 2023, 9:37am UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438/2 "2023-05-10T09:37:43Z")

</div>

Reply has been moved here.

---

<div class="post-metadata">

**Author:** ![KVPVishnu](https://yyz2.discourse-cdn.com/pega/user_avatar/forums.pega.com/kvpvishnu/32/11519_2.png) [@KVPVishnu](https://forums.pega.com/u/KVPVishnu)\
**Post date:** [November 19, 2024, 5:23pm UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438/3 "2024-11-19T17:23:27Z")

</div>

@KenshoTsuchihashi I currently have the pyGUID (Automatically generated key) as the PK. Now, I’m adding another new column which is a sequentially generated number to the table & make both the pyGUID, new field together as primary keys. While doing that, I cannot get the automatic value for pyGUID while inserting a new record. Any workarounds?

---

<div class="post-metadata">

**Author:** ![duraisankar](https://yyz2.discourse-cdn.com/pega/user_avatar/forums.pega.com/duraisankar/32/23517_2.png) [@duraisankar](https://forums.pega.com/u/duraisankar)\
**Post date:** [November 24, 2024, 10:47pm UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438/4 "2024-11-24T22:47:58Z")

</div>

@KVPVishnu I am not sure about the exact issue but one quick workaround is to **set** the value of ` **pyGUID** ` to `@Utilities.pxGetNewGUID ()` in the class’s `pyDefault` data transform. This will generate unique id.

---

<div class="post-metadata">

**Author:** ![PRUTHVIRAJ](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@PRUTHVIRAJ](https://forums.pega.com/u/PRUTHVIRAJ)\
**Post date:** [January 25, 2026, 6:48am UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438/5 "2026-01-25T06:48:31Z")

</div>

@KenshoTsuchihashi Is there a way to add/delete key in database table directly from Pega and without using a DB tool?

---

<div class="post-metadata">

**Author:** ![shanthosh\_18](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@shanthosh\_18](https://forums.pega.com/u/shanthosh_18)\
**Post date:** [January 25, 2026, 8:24am UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438/6 "2026-01-25T08:24:10Z")

</div>

@PRUTHVIRAJ Yes, Using Connect sql we can do it

---

<div class="post-metadata">

**Author:** ![MohamedAkeel](https://yyz2.discourse-cdn.com/pega/user_avatar/forums.pega.com/mohamedakeel/32/10001_2.png) [@MohamedAkeel](https://forums.pega.com/u/MohamedAkeel)\
**Post date:** [February 16, 2026, 3:08pm UTC](https://forums.pega.com/t/how-to-change-the-key-in-data-type/1438/7 "2026-02-16T15:08:15Z")

</div>

@PRUTHVIRAJ Hi, check this article: [How to Change the Primary Key of a Data Type in Pega Cloud | Support Center](https://support.pega.com/discussion/how-change-primary-key-data-type-pega-cloud)
