I'm trying to get the most optimized method for storing a GUID as the primary key, which is Binary(16) BUT be able to look at the binary value as text in a MySQL Workbench query. MSSQL makes this easy, why not MySQL?
I have created a table called "hosts" where the primary key is a CHAR(16) called "SystemID". Looking at others example not sure whats wrong.
Why am I getting the error:
Data too long for column 'SystemID' at row 1
My test ASP.NET method looks like:
[HttpGet("add")]
public ActionResult<string> Add([FromQuery(Name = "systemid")] Guid systemID)
{
//https://localhost:44344/api/hostresolver/add?systemid=D3DDCAC9-7DAD-44DF-AD5E-7F9F10AA0EF3
using (var connection = new MySqlConnection(DBSettings.connectionString))
{
try
{
connection.Open();
var data = new byte[255];
string sql = "INSERT INTO hosts VALUES(?SystemID, ?TrackingID)";
using (var cmd = new MySqlCommand(sql, connection))
{
cmd.Parameters.Add("?SystemID", MySqlDbType.Guid).Value = systemID;
cmd.Parameters.Add("?TrackingID", MySqlDbType.Binary, 255).Value = data;
return rowsEffected != 0 ? "Success" : "Failed";
}
}
catch (Exception e)
{
return e.Message;
}
connection.Close();
}
return "Failed";
}
Since Guid.NewGuid().ToByteArray()
produces 16 bytes I expect it to fit to CHAR(16).
Changing the key to MySqlDbType.Binary
and setting its value to systemID.ToByteArray()
works but I can't see what the SystemID value is when doing a query in MySQL Workbench. I should be able to use CHAR(16) but its not working.
Maybe I should to use: "BIN_TO_UUID"?