0

I have data I am inserting into an SQL database from config files, structured like so:

[GROUP1]
Attribute1
Attribute2

[GROUP2]
Attribute3
Attribute4

I am new to SQL. Naively, each attribute can represent a column in my table, but I also want to preserve the Group # information. Is the best way to preserve that information to prepend the group name to the column name or is there a better way? E.g. instead of the column name as Attribute1, we have the column name as Group1_Attribute1

user3180
  • 1,369
  • 1
  • 21
  • 38
  • Your question is quite weird. Nevertheless, you could also create n “group” tables, each one with the related attributes. But you would need a key in all of them to join them later – James Jan 03 '19 at 07:25
  • 1
    The most natural way to represent an hierarchical structure (with no practical limit to its nesting depth) is to use a table with the following columns: `Entry_ID`, `Parent_ID`,`Name`,...(whatever you need to include). The first column (`Entry_ID`) **MUST** be unique, and you may declare it as `NOT NULL IDENTITY(1,1)` to get unique integer values for it. – FDavidov Jan 03 '19 at 07:32
  • @FDavidov I see. So in my case, I still insert all the attributes agnostic of the groups as columns. Column names must be unique anyway so this is OK. Then I have a second table that associates the attribute with group information, and this table can really just be two columns: Attribute_name, Group_name. And voila? – user3180 Jan 03 '19 at 07:40
  • 1
    @user3180, Indeed. – FDavidov Jan 03 '19 at 08:30
  • Which [DBMS](https://en.wikipedia.org/wiki/DBMS) product are you using? "SQL" is just a query language, not the name of a specific database product (every relational database is a "SQL database"). Please add the tag for the database product you are using `postgresql`, `oracle`, `db2`, `sql-server`, ... –  Jan 03 '19 at 09:50

0 Answers0