How to change a primary key in a record

Hi! I would like to change the primary key in my record and use another field instead (the created field would be a concat() from other two fields).

Even though I have been trying, I´m finding trouble when trying to change the condition of primary key and give it to another field. I don´t want this primary key to be AUTO-INCREMENT and, when I try to put another field as primary key, an error occurs. Is it possible to eliminate the primary key and convert the other one into a primary key?

Otherwise, how can I solve this?

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    Hi 

    -- This set of SQL queries is used to update the primary key from one column to another column in the database. 
    -- By executing these three queries, the database will be updated with a new column as the primary key, set to auto-increment.
    -- Additionally, in the record type, you can make corresponding changes as recommended by Appian.
    
    -- Step 1: Change the data type of the new column to INT(11) and mark it as NOT NULL.
    ALTER TABLE table_name
    CHANGE `column_name` `column_name` INT(11) NOT NULL;
    
    -- Step 2: Drop the existing primary key constraint from the table.
    ALTER TABLE table_name
    DROP PRIMARY KEY;
    
    -- Step 3: Add the new column as the primary key with auto-increment.
    ALTER TABLE table_name
    ADD COLUMN `new_column_name` INT AUTO_INCREMENT PRIMARY KEY;


    In these queries, we first change the data type of the new column, then drop the existing primary key, and finally, add the new column as the primary key with auto-increment. This sequence of steps ensures that your database table's primary key is updated correctly. Additionally, you can make corresponding changes in your Appian record type to align with this database update.

  • Thanks a lot! Only one thing, I want my NEW primary key to be a TEXT, and I don´t want it to be autoIncrement. In my case, I want to create it in the process model as a CONCAT() of two fields of the record, as my identifier 

    Can I use your code with the new primary key as a text, and without putting it as AutoIncrement?

  • 0
    Certified Senior Developer
    in reply to carlosp5114

    Primary keys mostly be Number (Integer) data types. They perform better than primary keys using text data types because integers are processed quicker and are easier to sort and index.

Reply Children
No Data