简体   繁体   中英

Can we have primary key as function of column or set of column

I am trying to define a table where I want to define primary key has reverse of column. I was wondering if its possible in innodb ?

I am creating a table

create table abc 
(
  `id` varchar(255) PRIMARY KEY , 
  `key` LONGTEXT NOT NULL,
  `value` LONGTEXT  NOT NULL ,
  `last_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `created_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) 

but instead I want PRIMARY KEY (reverse(id))

MySQL doesn't support indexes on functions at all. So it clearly can't have them for a primary key.

Nor does MySQL support materialized views nor indices on views. 1

Depending on what you're trying to accomplish, most likely you should just store your key the other way around. If your application depends on having it reverse of what's stored, create a view for the application to interact with. Unfortunately, updates will be difficult as MySQL doesn't do INSTEAD OF triggers or triggers on views at all .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM