I'm using Spring (Roo specifically) to develop an application and for one entity I have a drop down list that is based on a set of enums (i.e. enum(Blue,Pink,Red)). When stored in MYSQL database these enums are stored as numbers (obviously). I want these to be stored as the actual string values instead of numbers in the database (so in MYSQL I want "pinK" to be under "color" instead of simply "2".Is this possible? Thanks
edit:
Let me try asking it a different way. Say when I'm retrieving the data from MYSQL. Instead of the symbolic values I want the actual words is there any way to do this?
I'd try labeling your field with #Enumerated(EnumType.STRING) annotation like:
#Enumerated(EnumType.STRING)
private MyEnumType myEnumField;
You can store the values as text, then use an insert trigger to validate that the stored values are one of the allowed values (since MySQL doesn't support check constraints).
You can also index the minimum number of characters to provide uniqueness, to make your indexing overhead smaller. In your example, you'd only need to index the first character, since 'B', 'P', and 'R' are unique. If you add 'Brown' to the list, you'll need to index the first two characters ('Bl','Pi','Re','Br').
EDIT
My original suggestion was to store the values as their actual text representations, rather than as a number.
Another way to do what you're asking, though, is to add a 'colors' table:
id INT
name VARCHAR
Then have your 'enum' column be a foreign key to the colors table. Then when you do your select:
SELECT ...,colors.name AS color
FROM table t
JOIN colors c ON t.color = c.id
Related
I want to store a single column with just one value in MySQL DB, and this column is not related to any other table in the DB, so just to store a single value should I create an entire table or is there a better way to store single key-value pair in SQL DB.
For example, a boolean variable isActive needs to be stored and queried
It is not uncommon to do this, but don't create a new table for every singleton value. Make one table for this purpose, with two columns, where the first column identifies a name for the value, and the second column is the actual value. Let the data type of the value be string. This is a minor disadvantage when you actually need a boolean, but that way your table is more flexible.
For instance:
create table params(
paramName varchar(100) not null,
paramValue varchar(100)
);
insert into params values ('isActive', '1');
commit;
See also Variant data type in DB which touches on the need to store different data types in the same column. The consensus is to use the string data type unless the specific data type is really essential, in which case I would suggest to create a separate "parameterXXX" table per data type, so all booleans go in one table, all dates in another, ...etc.
I have essentially a table that contains a rowid, person_id (not importane here), key, and value. My problem is that my application needs to be able to support different types of values: some int's, some text fields, and some composite types which hold multiple pieces of information. The only approach I can think of to solve this is to change value into value_table and value_id, where I have different tables for each type of value, and value_id is the primary key in the table given by value_table of the desired value.
I am new to SQL though, so I want to know if this is a bad idea for some reason I'm not thinking of. It seems weird to have value_id be a foreign key into more than one table, and it also seems like it would be difficult to write the queries to enable data access (especially since I'm doing this from Django's ORM).
Edit with more details:
My application contains user-added fields where the user sets the name of the field (like a map key) and the value, as well as the type of the value. The value can be anything from a boolean value or an integer to a 10000+ character text field or a composite type made up of multiple SQL primitive types.
I could use JSON in a text field here, but then I can never implement any kind of sorting or filtering by values, which is a requirement.
In my table "Phones", I have a Boolean column 'memoryCard'.
If a phone doesn't admits an external memory card, so memoryCard = False, otherwise it is True.
But if memoryCard = True, so I have to put in my Database the Type of Memory Card.
How to model this ? should I create another column "memoryCardType" in the table "Phones" ? or I have to create a separate table that contains card mermory Types ?
Thank you very much.
First, you should have a separate table for memory cards. And you table for phones should have a foreign key reference to this table, with a column called something like MemoryCardId.
Next, you have a choice. You will have a field MemoryCardId that can be NULL. You can treat a NULL value as not having a memory card at all. If so, you can put a view on phones to get the boolean value:
create v_phones as
select p.*, (MemoryCardId is not null) as HasMemoryCard
from phones;
Otherwise, you can have a separate flag (as you are suggesting in the question). You would only use MemoryCardId when the value of the flag is true. In most databases, you could add a check constraint to ensure that the id and flag are aligned. Alas, in MySQL, you would need to do this with a trigger instead.
EDIT:
The advantage of an explicit table over an enum:
You are not constrained to arbitrary limits on the number of values in the enum (see here).
If the memory card names are actually numbers, the parse won't get confused.
You can store additional information about the memory card -- the manufacturer, date of release, color of packaging, if you like.
The solution is more portable to other databases.
Is there any way to issue a mysql statement to create a table without having to assign the number of columns? I am working with the MySQL C API for grabbing some variables and then storing them in a table. The issue that I am encountering is that I have to create the table (obviously) before inserting the variables into the table. These variables sometimes are structures (two, three or four variables into a single table), so I am looking for a way of not having to say:
CREATE TABLE Structures(ID varchar(10) primary key, name varchar(25))
but creating a table on where any number of columns can be inserted?
Let me know if I am being a bit vague in here.
No, you can't. You can however add columns at runtime using ALTER TABLE.
However, personally, I wouldn't recommend that. You should know what your database looks like, before you start implementing it.
The other way to code this is to use two tables and a one-to-many between them.
For instance, you might have a tables like this - pcode,
table experiment
experiment_id: long
experiment_header: varchar(50)
table experiemnt_data
experiemnt_data_id: long
experiment_id: long
key: varchar(20)
value: long
#id = insert into experiment (experiment_header) value("test run")
insert into experiment_data (experiment_id, key, value) value(#id, 'x', 1)
insert into experiment_data (experiment_id, key, value) value(#id, 'y', 20)
AS #Mark and #attis said:
You can't. You can however add columns at runtime using ALTER
TABLE.
However, personally, I wouldn't recommend that. You should know what
your database looks like, before you start implementing it.
I think the best solution could be:
Create two tables :
column with (id, name)
values with (id, column_id, value)
then you just have to join them to easily get you results, and you can easily add others "columns"
You can also store everything in values table, but your data may be inconsistent, and, in my mind, it's faster to look for a number than to compare strings (table lock, index etc...)
I wanted to comment #Mark post, but can't (reputation too low)
I'm sure this is either totally impossible or really easy:
If I'm creating a table and I want one of the columns to have limited options, it seems that I use either the ENUM or SET value type. But I have to define the possible values at that moment. What if I have another table which has two columns, a primary key column and a data column, and I want the ENUM for my new table to be set to the primary key of the already existing column?
I'm sure I can just write in the values long-hand, but ideally what I need is for new values to be entered into the list table and for the table with the enum column to just accept that the value choices will include anything new added to that list table.
Is this possible without needing to manipulate the structure of the new table each time something is added to the list?
i think this link help :
http://dev.mysql.com/doc/refman/5.0/en/enum.html
have a discussion of it
in the user comments
start :
"In MySQL 5.0, you can convert an enum's values into a dynamically-defined table of values, which then provides effectively a language-neutral method to handle this kind of conversion (rather than relying on PHP, Tcl, C, C++, Java, etc. specific code).
"
he do it with stored PROCEDURE
The easiest way is to use a regular column without contraints. If you're interested in all the current values, use DISTINCT to query them:
select distinct YourColumn from YourTable
That way, you don't have any maintenance and can store whatever you like in the table.
The foreign key table you mention is also a good option. The foreign key will limit the original column. Before you do the actual insert, you run a query to expand the "enum" table:
insert into EnumTable (name)
select 'NewEnumValue'
where not exists (select * from EnumTable where name = 'NewEnumValue')
Not sure what exactly you're trying to achieve btw; limit the column, but automatically expand the choices when someone breaks the limit?