Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - Using tinyint with enum

Suppose that I have a SQL Server database that, amongst other tables, has a table named Mail, which has the expected recipient, sender, message body, etc. associated with it, one of these being the property Priority, which basically indicates if a mail has a high, normal, or low priority of being sent. Since I'll be only using these small amount of values (and if more were to be added it wouldn't be that many), I decided to make Priority a tinyint type, while in my code I'd represent is an enum type.

My question is, what is the right way to insert and retrieve this property when calling the database? In the case of retrieval (using a DataRow) I have:

mail.Priority = (MailPriority)(byte)row["Priority"];

And for inserting:

parameter = new SqlParameter("@Priority", SqlDbType.TinyInt);
parameter.Value = (byte)mail.Priority;
statement.Parameters.Add(parameter);

(Where statement is a SQLCommand).

So would this work, and is it the right way to do it? I figured that using tinyint would be okay, since the possible values can't be that many, but I don't know if int would be fine, better in terms of performance, or more commonly used, etc.

like image 411
Ana Ameer Avatar asked Jul 28 '26 05:07

Ana Ameer


1 Answers

Performance and space are not the same.
From a performance perspective .NET can actually access an Int32 faster.
What you get is space savings in SQL.
And you should also define you enum as byte.

public enum enumSVMV : byte { SV = 0, MV = 1, none = 2 };
like image 120
paparazzo Avatar answered Jul 30 '26 18:07

paparazzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!