I am trying to get a deep understanding about RDBMS, I am learning MariaDB.
Struggling to get the Binary data type and how its collation works.
I understand the binary(n) data type takes n bytes. This means the smallest memory that could be allocated is 1 byte?
This is taken from the MariaDB documentation:
The BINARY type is similar to the CHAR type, but stores binary byte strings rather than non-binary character strings. M represents the column length in bytes. It contains no character set, and comparison and sorting are based on the numeric value of the bytes.
I have the following sub questions:
- What exactly is a numeric value of a byte ? Is it the same as an ASCII character getting a number? If not, how is a numeric value determined ?
- If I insert a character 'A' inside a binary data type, how is this encoded and stored ? In the Table plus client, I can't see this being stored in binary.
- In the same data type i could add chars and also a binary image for example, how is the numeric value of the byte string determined?
I have created a few tables to test this,
First I create a table to test this with Binary(1) this doesn't allow me to add more than 1 digit 0-9 or 1 character A-Z or a-z
And then I have created a table with Binary(10) to see how the sorting order works
CREATE TABLE BinaryEg1(
b BINARY(1)
);
-- Adding two binary digits
INSERT INTO BinaryEg1 VALUES (0), (1);
-- Throws error, too long
INSERT INTO BinaryEg1 VALUES (10);
-- Adding basic chars to compare sort
INSERT INTO BinaryEg1 VALUES ('A'), ('a'), ('B'), ('b');
Here if I see the default sort order it places numbers before capital letters and then small case letters:
SELECT * FROM BinaryEg1 ORDER BY b;
-- Creating a table of 10 binary bytes to test how sorting works
CREATE TABLE BinaryEg10(
b BINARY(10)
);
-- Inserting values into 10 byte binary column
INSERT INTO BinaryEg10 VALUES (HEX('A'));
-- Adding some numbers of varying lengths to test sort order
INSERT INTO BinaryEg10 VALUES
(110),
(44),
(999999),
(1111111111),
(7876);
SELECT * FROM BinaryEg10 ORDER BY b;
Now, I see the sort order:
Please can someone explain how the sorting works, it seems like the first byte is the most important in the sequence, it ignores the rest of the bytes,
I understand, this might be an irrelevant and pointless question as normally i assume i should not use binary for storing chars, but i would still like to get a solid understanding about how this data type works