Numeric values as string values

naprile's profile image naprile posted 3 years ago in General Permalink

Hello. I have a table with several column, all formatted as INT values. I'd like to know if there is a possibility to use in this table, CONSTANT STRING values instead of numeric value. Each CONSTANT STRING value is defined (like C language "#define" syntax) in another table. For example:

table1:

  • CONSTANT1 = 2
  • CONSTANT2 = 23
  • CONSTANT3 = 12
  • CONSTANT4 = 7

table2

  • PARAMETER1 = CONSTANT1
  • PARAMETER2 = CONSTANT2
  • PARAMETER3 = CONSTANT3
  • PARAMETER4 = CONSTANT4

Is there any way to do this? Many thanks

ansgar's profile image ansgar posted 3 years ago Permalink

That sounds as if you want to create a relationship between a column in table1 and one in table2. If you're on MySQL or MariaDB, you can create a foreign key for that purpose. Both tables must have a column with the same definition (the name of it may differ). Then, in table2 you create a foreign key which points to the column in table1.

In HeidiSQL you use the foreign key editor in the table tab:

Description

naprile's profile image naprile posted 3 years ago Permalink

Maybe I haven't well explained what I want to do. In the generic cell (MY_CELL) of a particular column (MY_COLUMN), instead of insert an integer value, for example 5, I want to insert a string (MY_STRING), that only visually is a string but internally is treated as integer value 5. I need something similar to ENUM management. I tried to declare MY_COLUMN as ENUM type and assign to the ENUM list the values 'CONSTANT1', 'CONSTANT2', 'CONSTANT3','CONSTANT4' but if I read the MY_CELL value the returned value is not an integer but a string value ('CONSTANT1' or 'CONSTANT2' or 'CONSTANT3' or 'CONSTANT4'). I need to read an integer value.

ansgar's profile image ansgar posted 3 years ago Permalink

Well, then you should define the column as ENUM('CONST1', 'CONST2'):

Description

naprile's profile image naprile posted 3 years ago Permalink

Ok, but there is a problem for me. Reading the cell with 'CONST1' or 'CONST2' value return the string 'CONST1' or 'CONST2'. I need to read an integer value not a string value.

ansgar's profile image ansgar posted 3 years ago Permalink

You can't expect the result of an ENUM value to be aware of your integer values. Store these integer constants in your application instead.

Please login to leave a reply, or register at first.