I have a view over three tables. It has 6 columns as follows:
ID | NAME | PRINCIPAL_ID | DESCRIPTION | GROUP_ID | TYPE
As I'm using hibernate
to retrieve data from database, I had to put the ID
column in this view. The problem is, I cannot generate a unique ID
which seems to be necessary for hibernate
to correctly load the data.
How can I have a unique ID for every row in this view?
Maybe this can help, I'm not sure: The combination of PRINCIPAL_ID
and GROUP_ID
can make a unique ID. All these three are Long
.
UPDATE: When you insist to have it as a number, create your view like this:
SELECT
(@rownum:=@rownum + 1) AS rownumber,
yourTable.*
FROM
yourTable
, (SELECT @rownum:=0) r
But that's really it - no more other possibilities. Cast rownumber as long
like I said in comments, if it really, really has to be long
.
Alternatively in a procedure:
DELIMITER $$
CREATE PROCEDURE selectFromWhatever()
BEGIN
SELECT
(@rownum:=@rownum + 1) AS rownumber,
yourTable.*
FROM
yourTable
, (SELECT @rownum:=0) r;
END $$
DELIMITER ;
Then get result with
CALL selectFromWhatever()
Original answer:
From the MySQL manual:
UUID()
Returns a Universal Unique Identifier (UUID) generated according to “DCE 1.1: Remote Procedure Call” (Appendix A) CAE (Common Applications Environment) Specifications published by The Open Group in October 1997 (Document Number C706, http://www.opengroup.org/public/pubs/catalog/c706.htm).
A UUID is designed as a number that is globally unique in space and time. Two calls to UUID() are expected to generate two different values, even if these calls are performed on two separate computers that are not connected to each other.
A UUID is a 128-bit number represented by a utf8 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format:
The first three numbers are generated from a timestamp.
The fourth number preserves temporal uniqueness in case the timestamp value loses monotonicity (for example, due to daylight saving time).
The fifth number is an IEEE 802 node number that provides spatial uniqueness. A random number is substituted if the latter is not available (for example, because the host computer has no Ethernet card, or we do not know how to find the hardware address of an interface on your operating system). In this case, spatial uniqueness cannot be guaranteed. Nevertheless, a collision should have very low probability.
Currently, the MAC address of an interface is taken into account only on FreeBSD and Linux. On other operating systems, MySQL uses a randomly generated 48-bit number.
mysql> SELECT UUID(); -> '6ccd780c-baba-1026-9564-0040f4311e29'
Warning
Although UUID() values are intended to be unique, they are not necessarily unguessable or unpredictable. If unpredictability is required, UUID values should be generated some other way. Note
UUID() does not work with statement-based replication.
Another way would be to use CONCAT()
to build your unique ID.
SELECT CONCAT(PRINCIPAL_ID, '-', GROUP_ID) AS myUniqueID
FROM yourTable
Alternative is use ROW_NUMBER() OVER ()
SELECT ROW_NUMBER() OVER () AS ROW_NUM ... COLUMN... FROM TABLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With