How should I use UUID with JavaDB/Derby and JDBC? - jdbc

I currently use INT as type for primary key in JavaDB (Apache Derby), but since I'm implementing an distributed system I would like to change the type to java.util.UUID. A few questions about this:
What datatype in JavaDB/Derby should I use for UUID? I have seen CHAR(16) FOR BIT DATA been mentioned but I don't know much about it. Is VARCHAR(16) an alternative?
How should I use it with JDBC? E.g. in an PreparedStatement, how should I set and get an UUID?
If I later would likte to change database to SQL Server, is there a compatible datatype to java.util.UUID?
Simply, How should I use UUID with JavaDB/Derby and JDBC?

UUID is a 128 bit value. The CHAR(16) FOR BIT DATA type reflects that it is bit data stored in character form for conciseness. I don't think VARCHAR(16) would work because it doesn't have the bit flag. The database would have to be able to convert the binary data to character data which deals with encoding and is risky. More importantly, it wouldn't buy you anything. Since a UUID is always 128 bits, you don't get the space savings from using VARCHAR over CHAR. So you might as well use the intended CHAR(16) FOR BIT DATA.
With JDBC, I think you use the get/setBytes() method since it is dealing with small amounts of binary data. (Not positive, would have to try this)
And no idea about the SQL Server part.

If you still want to use the UUID object in your code you can use fromString to create UUID objects from the DB and toString to store them in the DB.

You could convert the UUID to a string and store it as VARCHAR. Most UUID string formats are similar to this one: 32 digits separated by hyphens: 00000000-0000-0000-0000-000000000000, so then you'd want a VARCHAR(36), or make it something like VARCHAR(64) if you please, since it doesn't hurt to have extra 'space' available in your VARCHAR -- only the actual digits are stored.
Once you've converted it to a string, just call Statement.SetString to include it in your INSERT statement.

Related

LaraAdmin how to use bigint field type

I'm building some application based on LaraAdmin crud generator. I need a field to be a BigInt since the possible values are bigger than a normal Integer. How should I achieve that in order to not make the system crash?
Because it is stored already as an integer, simply change the database type to biginteger and it'll be fine as they both store numerical values.

How does RethinkDB generate auto ids?

I'm writing a script which supposed to merge some data from sql-based db. Each row has a long-integer as a primary key (incremental). I was thinking about hashing these ids so that they'll somehow 'look' like the other ids already in my RethinkDB table. What I'm trying to achive here is to avoid dups in case of an attempt to merge the same data again, but keeping the original integers as ids along with the generated ids of the data saved directly to RethinkDB's table feels weird.
Can I do that?
How does RethinkDB generate auto ids anyways?
And am I approaching this correctly..?
RethinkDB uses a string-encoding of 128 bit UUIDs (basically hashed integers).
The string format looks like this: "HHHHHHHH-HHHH-HHHH-HHHH-HHHHHHHHHHHH" where every 'H' is a hexadecimal digit of the 128 bit integer. The characters 0-9 and a-f (lower case) are used.
If you want to generate such UUIDs from an existing integer, I recommend hashing the integer first. This will give you an even distribution over the whole key space (this makes sharding easier and avoids hotspots).
As a second step you have to format the hash value in a string of the format shown above. If you don't have enough digits, it's fine to leave some of the last 'H' as constant 0.
If you really want to go into the details of UUID generation, here are two links for further reading:
RFC 4122 "A Universally Unique IDentifier (UUID) URN Namespace" https://www.rfc-editor.org/rfc/rfc4122
RethinkDB's implementation of UUID generation and formatting https://github.com/rethinkdb/rethinkdb/blob/next/src/containers/uuid.cc

Specify Tinyint in MVC 3

I'm working on a project in MVC 3 (first time using!). One question I keep running into is how to specify datatypes. For example, why have a field of type Nvarchar that has a length of 4000 when you only need a length of 10. As far as I can tell, there is no definitive compilation of specifying server data types for your database. I've been fairly successful so far (Below are some of the ones I've found), and please correct me if I am wrong. My main question is if there is a way to specify a Tinyint (1 byte) in MVC 3.
Here's some of the popular ones I've found:
Smallint: Specify field as Int16
Bigint: Specify field as Int64
Nvarchar(n): Add [StringLength(n)] above your variable in your Model
bit: Seems simple, but for first-time programmers - specify your field as a bool.
DateTime: There are so many flavors of DateTime, and to specify, initialize your field in your model as DateTime, and specify the type by placing [DataType(DataType.YourPreferredFormat)] above the variable, where "YourPreferredFormat" is a date/time related option of the enum DataType (DateTime, Date, Time, Duration). I'm not 100% clear on this though, so if anyone knows how each correlates, that would be great to know.
Per this answer to c-sharp-equivalent-of-sql-server-2005-datatypes it appears that you need to use Byte.

DataMapper datatype

Just want to know what are the corresponding datatype that I should declare when using DataMapper.
Types in MySQL
smallint
bit
varchar
Can anyone tell me the corresponding type in DataMapper?
Thanks.
According to this site you are fairly limited with no specific equivalents. Varchar somewhat equals string and integer seems to take the place of bigint. Bit is not mentioned at all. The idea is born out further by this link (click on show source) where you will see the adapter for MySQL maps the data types similar to above. Again, bit is not mentioned.

Enterprise Architect Oracle long field column properties

I have a little problem with Enterprise Architect by Sparx System.
Im trying to model database schema for Oracle. I created table with primary key with data type long. But when im trying to modify column properties (set AutoNum = true) I see empty properties. I read documentation of EA and saw that I need to setup this property to generate sequence syntax.
When I change data type to number, or switch database to mysql (for example) everything is alright, there are properties so Im able to modify AutoNum value.
Did you had similar problem and found solution ? or maybe im doing something wrong.
regards
It's becouse Oracle use sequence instead of autoincrement option. I've checked it and I think you have to use NUMBER column type and then set AutoNum property (you have to select Generate Sequences in options to get proper DDL code too). Instead of LONG data type you can set PRECISION and SCALE options on NUMBER type ie NUMBER(8) mean you can have 8 digits number and it can be set up to 38, so if you don't want to store info about every star in the universe will be enought for your scenario :)

Resources