Automatically provide unique id in the database

后端 未结 3 553
無奈伤痛
無奈伤痛 2021-01-26 04:27

In my project i need to register a donor and I need the user to enter his information and the system registers him and generate a unique id to the donor.

相关标签:
3条回答
  • 2021-01-26 05:02

    If the donor id has to be used outside mysql, consider the additional usage of UUID() -- http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid (in addition to the integer primary key that is); external interfaces won't be as vulnerable to parameter-playing (e.g. "let's see if I cant't get the data for donor 'my-id + 1'") if the exposed id is a uuid.

    That said, as mentioned in the mysql docs, if you need the uuids to be unpredictable, don't use the UUID()-Function but use e.g. an hash-algorithm (e.g. sha1 ) over the users data and sufficient entropy from the system (urandom, ...)

    Edit: Have a read at wikipedia http://en.wikipedia.org/wiki/Universally_unique_identifier on how to build format a uuid.

    0 讨论(0)
  • 2021-01-26 05:06

    Make a table with a field ID that has an index and has auto increment on.

    CREATE TABLE Persons
    (
    ID int NOT NULL AUTO_INCREMENT,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    PRIMARY KEY (ID)
    )
    

    If you now add a new rule with MySQL you can leave the ID field empty, or just don't pass it like below

    INSERT INTO Persons(LastName, FirstName, Address, City) VALUES('Your firstname','lastname','adress','city')
    
    0 讨论(0)
  • 2021-01-26 05:09

    Use auto-incremented integer field. Or use an auto-generated GUID. (Both of these work in T-SQL, I did not try MySQL.)

    0 讨论(0)
提交回复
热议问题