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 Associate 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.

  • 0
    Certified Associate Developer
    in reply to carlosp5114

    Yes, You can use by below query, remaining all are same steps we needs to follow.

    -- Step 3: Add back the primary key constraint
    ALTER TABLE table_name
    ADD PRIMARY KEY (`new_column_name`);
    

  • Thanks! In the end I opted for creating a new primary key indexing to the first one (2 primary keys):

    • Primary key 1: "idUnico", integer and AutoIncrement
    • Primary key 2: "CONCAT_PEDIDO_MATERIAL", text and not autoincrement

    The fact is that, in my process, I have the following error: 

    I suppose I have to change the value of my second primary key ("CONCAT_...") to Null--> yes, but when I try to change it and save my edition doesn´t appear:

    1) Base Configuration of my second primary key

    2) Change I´m trying to do (it does not save)

    3) Changes not saved (still "None" and not null by default)

    I would like to know if this new primary key can be "null" as a default value and, if not, what can I do to fix the error! Thanks a lot!

  • 0
    Certified Associate Developer
    in reply to carlosp5114

    Hi 

    The primary key is a unique or non-null key that serves to uniquely identify every record in a table or relation. In every database, there is a need for a unique identifier for each row in a table, and the primary key plays a vital role in achieving this uniqueness. The primary key column cannot store duplicate values. It is also referred to as a minimal super key; as a result, we cannot specify more than one primary key in any relationship.

    For instance, consider a table named "customer" with attributes such as ID, Name, and City. Only the ID column can never contain duplicate or NULL values because each customer is assigned a unique identification number. This characteristic facilitates the unique identification of each record in the database. Consequently, we can designate the ID attribute as the primary key.

    So that is the reason Primary Key will not get update as default null instead of default none.

Reply
  • 0
    Certified Associate Developer
    in reply to carlosp5114

    Hi 

    The primary key is a unique or non-null key that serves to uniquely identify every record in a table or relation. In every database, there is a need for a unique identifier for each row in a table, and the primary key plays a vital role in achieving this uniqueness. The primary key column cannot store duplicate values. It is also referred to as a minimal super key; as a result, we cannot specify more than one primary key in any relationship.

    For instance, consider a table named "customer" with attributes such as ID, Name, and City. Only the ID column can never contain duplicate or NULL values because each customer is assigned a unique identification number. This characteristic facilitates the unique identification of each record in the database. Consequently, we can designate the ID attribute as the primary key.

    So that is the reason Primary Key will not get update as default null instead of default none.

Children
No Data