[MDEV-5590] Primary key cannot be defined upon persistent virtual column Created: 2014-01-29  Updated: 2023-08-23

Status: Open
Project: MariaDB Server
Component/s: Virtual Columns
Fix Version/s: None

Type: Task Priority: Major
Reporter: VAROQUI Stephane Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: None

Issue Links:
Duplicate
duplicates MDEV-8135 Support primary keys on virtual columns Closed
PartOf
is part of MDEV-10137 Providing compatibility to other data... Open

 Description   

ERROR 1903 (HY000): Primary key cannot be defined upon a computed column .

Virtual column was a nice way to remove stuff like
PRIMARY KEY (`COD_INV`, `COD_FAM`, `COD_MAG`, `COD_FIL`, `COD_ID_INV`)

  • It's a bad practice to use triggers
  • It's a bad practice to use hidden primary key ,
  • It's a bad practice to use virtual column .

What could we advice that can be transparent to the application layer to fixe such bad design for InnoDB.

Rewrite the application is not always a political correct answer.



 Comments   
Comment by Manjot Singh (Inactive) [ 2019-04-08 ]

So as a workaround, we are able to remove the primary key and create a unique key with the required columns. InnoDB seems to treat this the same as the primary key (needs more testing).

Therefore, this feature should be easy to implement?

MariaDB [test]> alter table bad_with_key add primary key (id,id_len);
ERROR 1903 (HY000): Primary key cannot be defined upon a generated column
MariaDB [test]> alter table bad_with_key add unique key (id,id_len);
Query OK, 0 rows affected (0.035 sec)
Records: 0  Duplicates: 0  Warnings: 0

Comment by VAROQUI Stephane [ 2020-01-29 ]

The implementation should take care of :
https://blog.jcole.us/2013/05/02/how-does-innodb-behave-without-a-primary-key/

Comment by VAROQUI Stephane [ 2020-11-16 ]

The idea was to create an UNSIGNED LONG HASH for primary or unique key but this is blocked by 10.3+ -> https://jira.mariadb.org/browse/MDEV-18319

Comment by Rick James [ 2023-02-03 ]

I'm confused. https://mariadb.com/kb/en/generated-columns/ implies that "persistent" and "virtual" are mutually exclusive. So, is this bug report about a GENERATED column that is VIRTUAL? Or is PERSISTENT (aka STORED)?

(Also, Singh's example does not have enough info to reproduce it.)

Comment by VAROQUI Stephane [ 2023-02-03 ]

Less of on issue as of MDEV-19506

Comment by Geoff Montee (Inactive) [ 2023-02-03 ]

Hi rjasdfiii,

I'm confused. https://mariadb.com/kb/en/generated-columns/ implies that "persistent" and "virtual" are mutually exclusive. So, is this bug report about a GENERATED column that is VIRTUAL? Or is PERSISTENT (aka STORED)?

MariaDB implemented "virtual generated columns" before it implemented "persistent generated columns". At first, many folks just called the new feature "virtual columns". When MariaDB implemented "persistent generated columns", many folks were used to calling the old feature "virtual columns", so they started calling the new feature "persistent virtual columns", even though "virtual virtual columns" sounds kind of redundant and weird. Hopefully that confusing misnomer stops being used eventually.

Comment by Marko Mäkelä [ 2023-08-23 ]

When it comes to InnoDB, and presumably any other storage engine that uses index-organized tables, support for defining a primary key on a virtual column would have to be implemented at a very low level. It would also imply that any update of the base columns of that virtual column would be expensive, just like updating the primary key. In an index-organized table, updating the primary key is like a DELETE and INSERT of the entire row, in all indexes.

In a heap-organized table (which InnoDB does not support), every key can be updated independently of each other, so this would just be a DELETE and INSERT of the primary key index, not touching the data heap or other indexes.

Generated at Thu Feb 08 07:05:32 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.