Home > Blockchain >  creating a table in a DB in SQL with age groups
creating a table in a DB in SQL with age groups

Time:08-21

I'm creating a DB in SQL and I need a column that that has age groups and the value to be inserted is 20-30, 31-40 ect. I have done a bit of research and cant dind an answer to what data type i should use for storing numbers with a dash in them. TIA

CodePudding user response:

If you consider an age group just to be a sequence of characters (a name), you can use a VARCHAR.

However, if you want to use the age group for sorting, you'll have to left-pad each number to the number of digits of the used maximum value. E. g. you'd store '05-09' instead of '5-9' because values will be compared character by character, that is '5-9' > '10-20' (because 5 > 1) but '05-09' < '10-20' (because 0 < 1).

If you need access to the boundaries (numbers) of the age-groups or if you want to store additional information about the groups, you'll want to introduce a table that contains the information for each age-group. In general, you also want to reference this table using a foreign key, but it depends a bit on the use case.

CodePudding user response:

I'd use varchar or nvarchar. For data integrity, you should make a foreign key reference to another table that stores all of the possible values that could be entered for the column.

  • Related