Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8596165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:36:41+00:00 2026-06-12T00:36:41+00:00

Let’s say I am programming a MMORPG. I modeled a Entity character that can

  • 0

Let’s say I am programming a MMORPG. I modeled a Entity character that can have a multitude of attributes like coating, strength, color and so on. Because I do not know these attributes in advance (what and how many of them), I thought I create an extra table for it, like so:

CREATE TABLE character (INTEGER id, VARCHAR name, INTEGER player_id);

and

CREATE TABLE attributes (INTEGER character_id, VARCHAR key, VARCHAR value);

I would then be able to introduce a multitude of new attributes. However, how would I query this construct?
The query

SELECT * FROM character JOIN attributes ON character.id=attributes.character_id;

will obvioulsy only work for a single attribute. Do I have to JOIN the attributes table more than once or is there another solution?

Is there a way to have different types for the attribute.value Part? Doing it the way I am doing now would limit me to a VARCHAR representation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:36:42+00:00Added an answer on June 12, 2026 at 12:36 am

    Another possibility is to use hstore instead of EAV model.

    CREATE TABLE character (id INTEGER, name VARCHAR,
                            player_id INTEGER, attributes hstore);
    

    That way you can store the attributes as a map (key – value).

    insert into character (id, name, player_id, attributes)
    values (1, 'test', 1, '"attribute1"=>"value1","attribute2"=>"value2"')
          ,(2, 'test', 1, '"attribute1"=>"value1","attribute3"=>"value3"');
    
    select (each(attributes)).key, (each(attributes)).value 
    from character where id = 1;
    
      key text    value text
      --------------------------
       attribute1   value1
       attribute2   value2
    
    select id, attributes->'attribute3' as value 
    from character WHERE exist(attributes,'attribute3');
    
      id    value
      ---------------
       2   "value3"
    

    Hope this helps.

    UPDATE

    I made a small benchmark to compare hstore vs two tables.

    CREATE OR REPLACE FUNCTION create_dummy_data()
    RETURNS integer AS
    $BODY$
    DECLARE
       cont1       INTEGER;
       cont2       INTEGER;
       sqlInsert   VARCHAR;
    
    BEGIN
       CREATE TABLE character (id INTEGER PRIMARY KEY
                              ,name VARCHAR
                              ,player_id INTEGER);
    
       CREATE TABLE attributes (character_id INTEGER
                               ,key VARCHAR
                               ,value VARCHAR
                               ,FOREIGN KEY (character_id) REFERENCES character);
    
       cont1 := 1;
       WHILE cont1 < 10000 LOOP
          sqlInsert := 'INSERT INTO character (id, name, player_id) VALUES (' || cont1 || ', ''character' || cont1 || ''', ' || cont1 || ');';
          EXECUTE sqlInsert;
          cont1 := cont1 + 1;
       END LOOP;
    
       cont1 := 1;
       WHILE cont1 < 10000 LOOP
          cont2 := 1;
          WHILE cont2 < 10 LOOP   
             sqlInsert := 'INSERT INTO attributes (character_id, key, value) VALUES (' || cont1 || ', ''key' || cont2 || ''', ' || cont2 || ');';
             EXECUTE sqlInsert;
             cont2 := cont2 + 1;
          END LOOP;       
          cont1 := cont1 + 1;
        END LOOP;
    
        CREATE TABLE character_hstore (id INTEGER
                                      ,name VARCHAR
                                      ,player_id INTEGER
                                      ,attributes hstore);
        cont1 := 1;
        WHILE cont1 < 10000 LOOP
           sqlInsert := 'INSERT INTO character_hstore (id, name, player_id, attributes) VALUES (' || cont1 || ', ''character' || cont1 || ''', ' || cont1 || ', ''"key1"=>"1","key2"=>"2","key3"=>"3","key4"=>"4","key5"=>"5"'');';
           EXECUTE sqlInsert;
           cont1 := cont1 + 1;
        END LOOP;   
    
        RETURN 1;
     END;
     $BODY$
     LANGUAGE plpgsql;
    
     select * from create_dummy_data();
    
     DROP FUNCTION create_dummy_data();
    

    And I’ve got the following results:

    explain analyze
    SELECT ca.* 
    FROM character ca
    JOIN attributes at ON ca.id = at.character_id
    WHERE at.value = '1';
    "Hash Join  (cost=288.98..2152.77 rows=10076 width=21) (actual time=2.788..23.186 rows=9999 loops=1)"
    
    CREATE INDEX ON attributes (value);
    
    explain analyze
    SELECT ca.* 
    FROM character ca
    JOIN attributes at ON ca.id = at.character_id
    WHERE at.value = '1';
    "Hash Join  (cost=479.33..1344.18 rows=10076 width=21) (actual time=4.330..13.537 rows=9999 loops=1)"
    

    And using hstore:

    explain analyze
    SELECT * 
    FROM character_hstore
    WHERE attributes @> 'key1=>1';
    "Seq Scan on character_hstore  (cost=0.00..278.99 rows=10 width=91) (actual time=0.012..3.530 rows=9999 loops=1)"
    
    explain analyze
    SELECT * 
    FROM character_hstore
    WHERE attributes->'key1' = '1';
    "Seq Scan on character_hstore  (cost=0.00..303.99 rows=50 width=91) (actual time=0.016..4.806 rows=9999 loops=1)"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say you have a string that looks like this: token1 token2 tok3 And
Let's say that I have 2 lines like this. Some Label: First element Second
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I can call a method like this: core::get() . What is the
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have multiple requirements for a password. The first is that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.