473,385 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Enums in a database

Jay
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?
Oct 27 '06 #1
10 3810

Jay wrote:
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?
You can override the default numeric values of an Enum like this:
(examples from
http://www.c-sharpcorner.com/Languag...nCsharpRVS.asp)

enum Months
{
jan = 10, feb = 20, mar = 30, apr=40
}
According to the article, more than one Enumerator may have the same
value:

enum Months
{
jan = 1, feb = 1, mar, apr
}

To get the integer value associated with the enum, cast it like so:

int x = (int) Months.jan ; is a valid statement.
Hope this helps...

Joel

Oct 27 '06 #2
Jay,
Unless sombody a lot smarter than I can come up with something I've never
seen, I am afraid you will have to use the lookup table as a surrogate for
your enums.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Jay" wrote:
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?
Oct 27 '06 #3

Using a lookup table is the standard approach. If you also have the
enum values in a c# class then a lot of times you can avoid using the
lookup table and look at the numeric data directly.

One thing we've found useful is to write a unit test for each enum
that maps to a lookup table to verify that the values in the enum
match the values in the lookup table. Also if the lookup tables use a
consistent naming convention you can generate the enum classes from
the lookup table data automatically.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 27 Oct 2006 20:40:05 +0100, "Jay" <-wrote:
>In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?
Oct 27 '06 #4
Jay,

I assume that you've exhausted the possibility of an int key
corresponding to your enum value. There are many solutions floating
around the internet for using attributes to decorate particular enums
for extended data access, including integration with databases.

That's entirely more efficient for data storage than string/varchar
value. I also assume that you're worried about storage space
efficiency, not ease-of-programming efficiency. Ask a RAD developer and
you might get a "who cares how efficient it is to store" response.

You'll need to be more specific about "a database." This is a C# board,
so many of the people here are probably defaulting to MSSQL. Some other
databases have native enum data types and a well-written data access
driver could map DB enums to CLR enums. Better yet, use an OR database,
where storing your object is a single undecorated call.

If you are looking at MSSQL, it's important to understand that its data
model doesn't support the concept of an enum. Asking that sort of DB
for an enum is like asking an auto mechanic how he feeds the horses. It
has a similar-but-different approach: normalization. Full circle
complete, you're back to your lookup tables. Any way you look at this,
if you want a robust app, you'll need to deal with keeping your data
clean. You can change a string in a database much easier than you can
change the name in an enum.
Stephan

Jay wrote:
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?
Oct 27 '06 #5
Jay
Thanks Joel + Peter + Samuel for your fast responses - very helpful.

Thinking about it, I will have to use a table since I will want the end user of the database to be
able to add keywords to the database, effectively adding more "enums". I guess to improve
efficiency, I could read all of the table in my C# programme so that I don't have to make database
query everytime I want to find a corresponding number for a keyword. Does that make sense? I'm a
beginner to database and C# programming, so this might sound a bit basic.

Jay
"Jay" <-wrote in message news:uj******************@TK2MSFTNGP02.phx.gbl...
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?

Oct 27 '06 #6

Jay wrote:
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?
You can override the default numeric values of an Enum like this:
(examples from
http://www.c-sharpcorner.com/Languag...nCsharpRVS.asp)

enum Months
{
jan = 10, feb = 20, mar = 30, apr=40
}
According to the article, more than one Enumerator may have the same
value:

enum Months
{
jan = 1, feb = 1, mar, apr
}

To get the integer value associated with the enum, cast it like so:

int x = (int) Months.jan ; is a valid statement.
Hope this helps...

Joel

Oct 28 '06 #7
Jay wrote:
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?

A user-defined function may do the trick. The following function returns
the number for the month entered into the function. Otherwise it returns
null, if the string you enter is not valid.
CREATE FUNCTION [GetMonthNo](@month varchar(10))
RETURNS int
AS
BEGIN
DECLARE @monthNo int
SET @monthNo =
CASE @month
WHEN 'January' THEN 1
WHEN 'February' THEN 2
WHEN 'March' THEN 3
WHEN 'April' THEN 4
WHEN 'May' THEN 5
WHEN 'June' THEN 6
WHEN 'July' THEN 7
WHEN 'August' THEN 8
WHEN 'September' THEN 9
WHEN 'October' THEN 10
WHEN 'November' THEN 11
WHEN 'December' THEN 12
END

RETURN @monthNo
END
GO

/* Calling the function */

SELECT dbo.GetMonthNo('January')
SELECT dbo.GetMonthNo('September')
SELECT dbo.GetMonthNo('Something invalid')
Oct 28 '06 #8
On Fri, 27 Oct 2006 22:01:56 +0100, "Jay" <-wrote:
>Thanks Joel + Peter + Samuel for your fast responses - very helpful.

Thinking about it, I will have to use a table since I will want the end user of the database to be
able to add keywords to the database, effectively adding more "enums". I guess to improve
efficiency, I could read all of the table in my C# programme so that I don't have to make database
query everytime I want to find a corresponding number for a keyword. Does that make sense? I'm a
beginner to database and C# programming, so this might sound a bit basic.

Jay
"Jay" <-wrote in message news:uj******************@TK2MSFTNGP02.phx.gbl...
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?

Jay,

Yes it does make sense. In fact you can load the values into a Dictionary
object and use the int as the key for the lookup value or the other way around.

using System;
using System.Collections.Generic;

namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
// using an int key and string value
Dictionary<int, stringtestDict = new Dictionary<int, string>();
testDict.Add(10, "DescriptionOfEnum");

// get the string value using the int key
string str = string.Empty;
bool exists = testDict.TryGetValue(10, out str);

// or

// using a string key and int value
Dictionary<string, inttestDict2 = new Dictionary<string, int>();
testDict2.Add("DescriptionOfEnum", 10);

// get the int value using a string key
int i = 0;
exists = testDict2.TryGetValue("DescriptionOfEnum", out i);
}
}
}
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Oct 28 '06 #9
Hi Jay,

Realize that if you are using an enumeration at all then you are constraining
your design to only use the constants that you initially define. As others
have mentioned, a lookup table is more flexible than an enum because you can
define new values at a later time without recompiling. However, lookups add
complexity that may be inappropriate when you are sure that you don't need the
flexibility it provides. For instance, if I have to code different business
logic based on a person's home state I'd much rather use enums than a lookup
table since the lookup data is probably not going to change anytime soon:

if (aPerson.State == USStates.NY)
...

The above is really easy to accomplish, allowing for database check
constraints to ensure data integrity just like you could have using a lookup
table. I like to use check constraints on an integer column to confine the
values to those within the enum and use a strong-typed dataset when
loading/saving the data. Some people like to use the string values so they
know exactly what data is stored when looking at raw data, however that means
you have to define the enum twice: once in code and once in the check
constraint. I prefer to avoid this at the sacrifice of looking at "ugly", raw
data. Implementing my example below using strings instead of integers is a
simple matter of defining a new strong-typed column, in code, that calls
Enum.Parse on the string column.

This approach (as opposed to a lookup table) also provides type-safety because
the compiler will catch if you try to use USStates.Panama, for example, and a
database constraint can be used to ensure that you'll never get a number in a
result set that doesn't correspond to a defined constant. Using a string
value in the database provides the same amount of type-safety as integer if a
check constraint is used.

Define the enum first:

public enum Specialty
{
ForeignCars, // auto-assigned 0
DomesticCars,
ClassicCars,
SpecialtyCars // auto-assigned 3
}

Create the table definition as follows (here I use T-SQL in Sql Server 2005,
which may or may not be similar to the RDBMS that you are using):

CREATE TABLE AutoMechanics (
[Name] varchar(50) NOT NULL,
[Specialty] int NOT NULL
CHECK (Specialty BETWEEN 0 AND 3) -- very important!
)

Fill in some test data:

INSERT AutoMechanics SELECT 'Fred', 0 -- ForeignCars
INSERT AutoMechanics SELECT 'Joe', 3 -- SpecialtyCars
INSERT AutoMechanics SELECT 'Ann', 3 -- SpecialtyCars
INSERT AutoMechanics SELECT 'Susie', 2 -- ClassicCars
INSERT AutoMechanics SELECT 'Tom', 1 -- DomesticCars
Define the strong-typed dataset as normal. After the DataTable is defined in
the XSD designer, simply change the column type from System.Int32 to the Type
name of your enum and you're all set. The name for this example would just be
"Specialty", since I never declared any namespace.

(Note: You may have to build your project once with the enum defined before
assigning the new value in the designer. Also, this approach is much easier
in VS 2005. In earlier versions you have to change the XSD schema manually,
but it still works IIRC).

Here's an example of how all this can be used in your business logic code:

void RelocateMechanicsBySpecialty()
{
AppData data = new AppData();

// In VS 2005 a strong-typed SqlDataAdapter is created along with the
// DataSet, however you can use any code you'd like to fill "data".
using (AppDataTableAdapters.AppDataTableAdapter adapter =
new AppDataTableAdapters.AppDataTableAdapter())
{
adapter.Fill(data.AutoMechanics);
}

foreach (AppData.AutoMechanicsRow mechanic in data.AutoMechanics)
{
switch (mechanic.Specialty)
{
case Specialty.ClassicCars:
Console.WriteLine("{0} has a choice where to move.",
mechanic.Name);
break;
case Specialty.DomesticCars:
Console.WriteLine("{0} should be moved to Detroit.",
mechanic.Name);
break;
case Specialty.ForeignCars:
Console.WriteLine("{0} should be deported.", mechanic.Name);
break;
case Specialty.SpecialtyCars:
Console.WriteLine("{0} can stay put.", mechanic.Name);
break;
}
}
}

Here's the console output:

Fred should be deported.
Joe can stay put.
Ann can stay put.
Susie has a choice where to move.
Tom should be moved to Detroit.

--
Dave Sexton

"Jay" <-wrote in message news:uj******************@TK2MSFTNGP02.phx.gbl...
In C# I can set up an Enum so that number are represented as keywords, which
is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and
a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is
there a better way?


Oct 28 '06 #10
Jay
Hi,

I will be using MSSQL Express, which is free but has storage space limitations. If these limitations
do become a problem, I will probably move to PostgreSQL, but still using C#.net.

Actually, the int key would correspond to my enum value, so perhaps a lookup table is an efficient
method after all.

Jay

"ssamuel" <ss*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Jay,

I assume that you've exhausted the possibility of an int key
corresponding to your enum value. There are many solutions floating
around the internet for using attributes to decorate particular enums
for extended data access, including integration with databases.

That's entirely more efficient for data storage than string/varchar
value. I also assume that you're worried about storage space
efficiency, not ease-of-programming efficiency. Ask a RAD developer and
you might get a "who cares how efficient it is to store" response.

You'll need to be more specific about "a database." This is a C# board,
so many of the people here are probably defaulting to MSSQL. Some other
databases have native enum data types and a well-written data access
driver could map DB enums to CLR enums. Better yet, use an OR database,
where storing your object is a single undecorated call.

If you are looking at MSSQL, it's important to understand that its data
model doesn't support the concept of an enum. Asking that sort of DB
for an enum is like asking an auto mechanic how he feeds the horses. It
has a similar-but-different approach: normalization. Full circle
complete, you're back to your lookup tables. Any way you look at this,
if you want a robust app, you'll need to deal with keeping your data
clean. You can change a string in a database much easier than you can
change the name in an enum.
Stephan

Jay wrote:
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?

Oct 29 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
7
by: narshe | last post by:
I haven't come across an elegant way to do this yet, and would like to know what other people have come up with. There are two tables: CREATE TABLE EmployeeStatus ( pkId int not null primary...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
5
by: Hans Kesting | last post by:
Hi, In a database some code is stored as a string. This code can have a limited (known) set of values. What is the best way to use this in my (program-)code? I don't want to use "magic codes"...
2
by: Varangian | last post by:
Hi, This is a performance issue.... How about storing static data in integers in the database, querying it using integers as well, while displaying that static I'll use Enums - for example a...
3
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have a class as follows: class GamesConsole { public int iReference; public enum Maker {Nintendo, Sega, Sony, Panasonic} }
0
by: =?Utf-8?B?SGVucnlDQw==?= | last post by:
We are trying to develop a set of C# 2.0 web services that are data driven, loosely coupled, easy to maintain, and somewhat high preferment (I'm sure that's either an oxymoron or everyone else's...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.