How to hash auto increments in mysql


Problem :

Is there a way to make mysql hash the auto increment values for its tables? For example with md5?

    id - name
    1  - New York  
    2  - Chicago   
    3  - Sydney    
    4  - Berlin    

what I'm trying to get

    id - name 
    c4ca4238a0b923820dcc509a6f75849b - New York  
    c81e728d9d4c2f636f067f89cc14862c - Chicago   
    eccbc87e4b5ce2fe28308fd9f2a7baf3 - Sydney    
    a87ff679a2f3e71d9181a67b7542122c - Berlin    

Thanks in advance

EDIT:

I think I need to clarify the question a little more, what im trying to do is not Call the ID's but Insert them. In the moment the ID column is an int field which I will change to varchar.

I want to save the identifiers as Hashed in the DB not call them with mysql SELECT. Thanks



Solution :

If you really need this, for some reason, you can achieve it with a help of a separate table for sequencing and a BEFORE trigger

Table schemas:

CREATE TABLE table1_seq
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE table1
(id VARCHAR(32) NOT NULL DEFAULT 0, name VARCHAR(32));

The trigger

DELIMITER $$
CREATE TRIGGER tg_bi_table1
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
  INSERT INTO table1_seq () VALUES ();
  SET NEW.id = MD5(LAST_INSERT_ID());
END$$
DELIMITER ;

Now you can insert rows into your table1 table

INSERT INTO table1 (`name`) VALUES ('New York'),('Chicago'),('Sydney'),('Berlin');

or select

SELECT * FROM table1 WHERE id = MD5(2);

Here is SQLFiddle demo


Mysql Tutorials

Mysql Howto..

Fastest way to check how many posts are in a thread with MySQL and PHP

How to accept a floating point value in an html form and send it to MySQL DB

How to limit a join to one result per record, in order for MYSQL

MySql Geometry: How to populate table with a multipolygon from an multi-dimensional array?

How to echo print statements while executing a sql script

How to keep run Python Program in Background on Raspberry Pi after terminate ssh

How do I write this MySQL query to get the correct information? (Subquery, multiple subqueries)

How to send an array of Flex checkboxes to a mysql server?

How do you Change Array Index Keys to MySQL values

How to discard old entries in MySQL table

How to alert / see log in mysql

How to check offset is defined within MYSQL Query

How to have english and greek characters in MySQL WorkBench?

How to select the data from MySQL table only one record if there are same multiple records

How can I combine the LOCATE() and SUBSTR() functions in MySQL to create a new column?

How to escape for MYSQL queries from Ruby on Rails?

How do I decrypt and encrypt Joomla user password for Java program?

How to insert an html input array into mysql using PDO

How do I select only the most recent record based on a userid that can be in either of two columns?

How to show only mySQL records where a certain name appears in more than X rows?

Show “empty” mysql fields in form

How to get Django to work with MySQL in XAMPP?

How to display categories, subcategories, sub-subcategories in select list - php/mysql?

I need to know how i can write IF statements and CASE break statements that use and execute queries, etc in MySQL?

how to derived value of a filed from another field in MySql

How does this Switch statement know which case to execute? (PHP/MySQL)

How to connect mySql database using sequel pro? [closed]

How to do a regular expression replace in MySQL?

How to store 00 instead of 0 as DEFAULT to integer field in mysql?

How do I tell the MySQL Optimizer to use the index on a derived table?