Search code examples
mysqlsqlsinglestore

MemSQL - Surrogate key as Primary and different unique keys at the same time in table creation


I have a situation that I need to have a surrogate key (id) in place of a composite key (4 field combined to be unique: project_id, dataset_id, table_id, view_name) to easily refer that in other tables.

So to do this I used id field as Primary key and other 4 fields mentioned above as unique keys. This is allowed in MySQL but not in MemSQL.

Error Code: 1895. The unique key named: 'project_id' must contain all columns specified in the primary key when no shard key is declared

So I added the id field as the Shard key but no use.

CREATE TABLE `table_access_details` (
  `id` integer primary key,
  `project_id` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `dataset_id` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
  `table_id` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
  `view_name` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL ,
  `upload_id` decimal (14,0) DEFAULT NULL,
  `modified_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  #SHARD KEY (`id`),
  unique(`project_id`,`dataset_id`,`table_id`,`view_name`)
);

How can I overcome this situation in MemSQL?


Solution

  • So, you want unique key (id) as well as unique key (project_id, dataset_id, table_id, view_name)? This is not possible in a sharded table in memsql - the unique key cannot be efficiently enforced across shards. Your options are: don't use both unique keys, or make the table a reference table.