CBSE BOARD X, asked by pankajtandon8732, 11 months ago

List the data types available in binary types

Answers

Answered by Sanjayjaiswal2230
0

Explanation:

Binary Operators and Functions

Binary operators &, ~, | and # have special behavior for binary data types, as described in Bitwise Operators.

The following aggregate functions are supported for binary data types:

BIT_AND

BIT_OR

BIT_XOR

MAX

MIN

BIT_AND, BIT_OR, and BIT_XOR are bit-wise operations that are applied to each non-null value in a group, while MAX and MIN are byte-wise comparisons of binary values.

Like their binary operator counterparts, if the values in a group vary in length, the aggregate functions treat the values as though they are all equal in length by extending shorter values with zero bytes to the full width of the column. For example, given a group containing the values 'ff', null, and 'f', a binary aggregate ignores the null value and treats the value 'f' as 'f0'. Also, like their binary operator counterparts, these aggregate functions operate on VARBINARY types explicitly and operate on BINARY types implicitly through casts. See Data Type Coercion Operators (CAST).

Binary Versus Character Data Types

Binary data types BINARY and VARBINARY are similar to character data types CHAR and VARCHAR, respectively. They differ as follows:

Binary data types contain byte strings—a sequence of octets or bytes.

Character data types contain character strings (text).

The lengths of binary data types are measured in bytes, while character data types are measured in characters.

Examples

The following example shows VARBINARY HEX_TO_BINARY(VARCHAR) and VARCHAR TO_HEX(VARBINARY) usage.

Table t and its projection are created with binary columns:

=> CREATE TABLE t (c BINARY(1));

=> CREATE PROJECTION t_p (c) AS SELECT c FROM t;

Insert minimum byte and maximum byte values:

=> INSERT INTO t values(HEX_TO_BINARY('0x00'));

=> INSERT INTO t values(HEX_TO_BINARY('0xFF'));

Binary values can then be formatted in hex on output using the TO_HEX function:

=> SELECT TO_HEX(c) FROM t;

to_hex

--------

00

ff

(2 rows)

The BIT_AND, BIT_OR, and BIT_XORfunctions are interesting when operating on a group of values. For example, create a sample table and projections with binary columns:

The example that follows uses table t with a single column of VARBINARY data type:

=> CREATE TABLE t ( c VARBINARY(2) );

=> INSERT INTO t values(HEX_TO_BINARY('0xFF00'));

=> INSERT INTO t values(HEX_TO_BINARY('0xFFFF'));

=> INSERT INTO t values(HEX_TO_BINARY('0xF00F'));

Query table t to see column c output:

=> SELECT TO_HEX(c) FROM t;

TO_HEX

--------

ff00

ffff

f00f

(3 rows)

Now issue the bitwise AND operation. Because these are aggregate functions, an implicit GROUP BY operation is performed on results using (ff00&(ffff)&f00f):

=> SELECT TO_HEX(BIT_AND(c)) FROM t;

TO_HEX

--------

f000

(1 row)

Issue the bitwise OR operation on (ff00|(ffff)|f00f):

=> SELECT TO_HEX(BIT_OR(c)) FROM t;

TO_HEX

--------

ffff

(1 row)

Issue the bitwise XOR operation on (ff00#(ffff)#f00f):

=> SELECT TO_HEX(BIT_XOR(c)) FROM t;

TO_HEX

--------

f0f0

(1 row)

Similar questions