How to design Database

I want to add  a field in db which should store data in array format . I will get this data via integration and i want it to be in an array. can someone help


Integration output :

{

email :Test,
addid:1193u,
remove id:123,
status:success,
statausmessage:
{
     {
          message :test1 success, 
          date:
       }
         {
             message : test2 failed,
             date:
          }
    
  }
}

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    A relational database cannot store multiple values in a single field. You will have to create a separate record/table and relate them.

    Before starting to build that in Appian, I suggest to look for some training material in the internet.

    When asked your question, ChatGPT told me this:

    Certainly! To store data in an array format in a database, you have a few options depending on the type of database you are using. Below are some common approaches for different types of databases:

    ### 1. **Relational Databases (e.g., MySQL, PostgreSQL)**

    In relational databases, you typically don't store arrays directly. Instead, you can use one of the following methods:

    - **JSON Data Type**: Many modern relational databases support a JSON data type, which allows you to store arrays as JSON.

    **Example (PostgreSQL)**:
    ```sql
    CREATE TABLE your_table (
    id SERIAL PRIMARY KEY,
    data JSONB
    );

    INSERT INTO your_table (data) VALUES ('[1, 2, 3, 4]');
    ```

    **Example (MySQL)**:
    ```sql
    CREATE TABLE your_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    data JSON
    );

    INSERT INTO your_table (data) VALUES ('[1, 2, 3, 4]');
    ```

    - **Separate Table**: Create a separate table to store the array elements, with a foreign key relationship to the main table.

    **Example**:
    ```sql
    CREATE TABLE your_table (
    id SERIAL PRIMARY KEY
    );

    CREATE TABLE your_array_table (
    id SERIAL PRIMARY KEY,
    your_table_id INT,
    value INT,
    FOREIGN KEY (your_table_id) REFERENCES your_table(id)
    );
    ```

    ### 2. **NoSQL Databases (e.g., MongoDB)**

    NoSQL databases like MongoDB are designed to handle unstructured data, including arrays, natively.

    **Example**:
    ```javascript
    db.your_collection.insertOne({
    data: [1, 2, 3, 4]
    });
    ```

    ### 3. **Using an Array Data Type (PostgreSQL)**

    If you are using PostgreSQL, you can also use the built-in array data type.

    **Example**:
    ```sql
    CREATE TABLE your_table (
    id SERIAL PRIMARY KEY,
    data INT[]
    );

    INSERT INTO your_table (data) VALUES (ARRAY[1, 2, 3, 4]);
    ```

    ### 4. **Using a Text Field (as a last resort)**

    If your database does not support JSON or array types, you can store the array as a delimited string (e.g., comma-separated values) in a text field. However, this approach is less efficient and more complex to query.

    **Example**:
    ```sql
    CREATE TABLE your_table (
    id SERIAL PRIMARY KEY,
    data TEXT
    );

    INSERT INTO your_table (data) VALUES ('1,2,3,4');
    ```

    ### Conclusion

    Choose the method that best fits your use case and the capabilities of your database. If you are using a relational database, the JSON data type or a separate table is often the best approach. If you are using a NoSQL database, you can store arrays directly.

  • 0
    Certified Lead Developer
    in reply to Stefan Helzle
    cannot store multiple values in a single field

    I mean, it can (as your chatGPT answer noted), but to your point, the conditions are far more limited / limiting than properly creating individual rows.  if we know up-front that the "multiple values" will be fairly limited and fairly consistent, my favorite method is to use a JSON string (with the DB just handling the string part, and Appian handling the JSON part).  I don't know if Appian is all that compatible with DB table columns of "json type", whatever that might imply (unless it's just renamed plaintext of some sort).

  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    I agree to everything you said.

    BTW, Appian can use JSON data fields in DB, just not all the JSON specific DB functions. For Appian, it is just text. In the DB, using stored procedures, you can do much more, and treat the JSON just like any other structure,

  • 0
    Certified Lead Developer
    in reply to Stefan Helzle

    That's cool - so out of curiosity, does declaring the column type as JSON add any particular value?  I've done the MariaDB-side JSON conversions before on simple plaintext (in stored procedures as well as views and/or just basic DB maintenance utilities), but not sure if the column typing somehow makes it easier.

  • Should I declare it as text for status message field then?

    I will get this data via integration and i want it to be in an array. 


    Integration output :



    {
    ID:1,
    CaseID: 12,
    email :Test,
    addid:1193u,
    remove id:123,
    status:success,
    statausmessage:
    {
         {
              message :test1 success, 
              date:
           }
             {
                 message : test2 failed,
                 date:
              }
        
      }

  • 0
    Certified Lead Developer
    in reply to ZAINAB

    If it were me, I'd declare the column as text, and wrap the "statusmessage" array in JSON before writing.  Be cautious, though, that the number of Status Messages doesn't potentially get problematically large, i.e. enough to break the column if the width grows too large.  If it could grow to hundreds of messages (or potential messages with very long text), then it might be time to consider an association table instead.

Reply
  • 0
    Certified Lead Developer
    in reply to ZAINAB

    If it were me, I'd declare the column as text, and wrap the "statusmessage" array in JSON before writing.  Be cautious, though, that the number of Status Messages doesn't potentially get problematically large, i.e. enough to break the column if the width grows too large.  If it could grow to hundreds of messages (or potential messages with very long text), then it might be time to consider an association table instead.

Children