16

I am using MySQL database.

I have one table having column with datatype binary(16).

I need help with the insert statement for this table.

Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)

insert into assignedresource values ('9fad5e9e-efdf-b449');

Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1

How to resolve this issue?

1
  • You're trying to insert 18 bytes to a 16-byte field. What did you expect? How to fix - insert less bytes. Commented Jan 31, 2012 at 14:33

4 Answers 4

22

You should remove the hyphens to make the value match the length of the field...

Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)

insert into assignedresource values ('9fad5e9eefdfb449');

Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.

insert into assignedresource values (X'9fad5e9eefdfb449');
Sign up to request clarification or add additional context in comments.

1 Comment

Thought I should note that every two characters represents a binary byte. So I believe the string in question (after the dashes are removed) is a binary(8).
4

Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:

INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));

Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.

Comments

2

You can strip the hyphens and perpend 0x to the value unquoted, like this:

insert into assignedresource values (0x9fad5e9eefdfb449);

As well as, as this (mentioned in other answers):

insert into assignedresource values (X'9fad5e9eefdfb449');

Both are valid notation for a hexadecimal literal.

Comments

-1

Your string is 18 char long, change the database

CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)

2 Comments

Aren't strings in mysql null terminated (19 bytes required)?
AFAIK, MySQL doesn't care about null termination. So it's a matter of conversion at client side. But nothing prevents one from storing all 19 bytes and point to raw data on the client side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.