I am using Visual Studios 2010 for this C# program I am making where I am generating a cache that I want to store in a local database made by the SqlCeEngine class. I want to use the SqlCeEngine class to make this database in a 100% programmatic manner, but I am having problems finding CREATE TABLE examples using MS SQL data types while I am still fairly new to databases and what experience I have had before was with sqlite3.
I want to make a table with a UniqueID column as the PRIMARY KEY, but ideally using a smallint as the datatype for it as this generated cache shouldn’t be exceeding 2^15 entries, or even 1500 entries for that matter. But let’s say I also want to make a second column that holds some text, but I have a hard time determining how long that will be ahead of time. On average, I am expecting 8 characters or less, but I might have the occasional entry that is over 260 characters long. I could just make the string size for the entries set to the maximum length the string is allowed to be (4000 characters I believe), but for this project, space consumption by the cache needs to be minimized and I think that would waste a lot of space. I noticed there were nchar and nvarchar data types, but I didn’t really understand the difference as they both needed a length to be declared. It just seemed like one was hinting towards that it might be more friendly with strings of different lengths under the hood of the SqlCeEngine than the other.
TL;DR
Is there a way to declare a SQL database in C# using the SqlCeEngine class that uses a smallint data type for the primary key column and variable length text in the second column without wasting space?
You want to use a varchar, unless you’re storing unicode characters, in that case use nvarchar.
The difference between
char(50)andvarchar(50)is thatchar(50)will grab and hold onto 50 characters, whether they’re used or not.varchar(50)will allow up to 50 characters, but if 0 are used, it doesn’t reserve any space.So if you had a field char(20) and the value was ‘Name’ the size of that field would be 20 characters, the 4 you specified and an additional 16 to fill it out. If you had a varchar(20) and the value was ‘Name’ the size would be 4.