databasespring-bootspring-data

UUID vs Long primary key


In database design, when considering a primary key, what are the advantages and disadvantages of using UUID compared to a Long data type?

I came across two different tutorials that implemented each of them, and I am just curious about which one serves better.


Solution

  • Compare UUID and Long as the type of primary key in database:

    1. Long:

    This code will generate an increasing-from-0 key. For example: 0, 1, 2, .... Of course, an increasing-from-0 key won't be safe against something like XSS (because of its easy-to-predict property).

    2. UUID:

    In summary, both are good to use as the primary key in database. I think UUID is better, but I prefer to use Long because it's easier to implement in the program.