Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect prefix key MySQL [duplicate]

I have a problem creating a table with phpmyadmin, which gives me the following error:

#1089 - Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys

This is the query that I do:

CREATE TABLE `b2b`.`users` ( `id` BIGINT NOT NULL AUTO_INCREMENT ,
 `name` VARCHAR(30) NOT NULL ,
 `surnames` VARCHAR(80) NOT NULL ,
 `birthdate` DATE NOT NULL ,
 `drivingdoc` VARCHAR(20) NOT NULL ,
 `acdate` DATE NOT NULL ,
 `countrydoc` VARCHAR(20) NOT NULL ,
 `province` VARCHAR(20) NOT NULL ,
 `locality` VARCHAR(35) NOT NULL ,
 `address` VARCHAR(150) NOT NULL ,
 `number` VARCHAR(20) NOT NULL ,
 `flat` VARCHAR(20) NOT NULL ,
 `door` VARCHAR(20) NOT NULL ,
 `description` VARCHAR(2000) NOT NULL ,
 PRIMARY KEY (`id`(7))) ENGINE = InnoDB;

Using MariaDB in ubuntu minimal.

like image 422
Erik Neller Avatar asked Dec 04 '15 18:12

Erik Neller


People also ask

What is incorrect prefix key?

#1089 - Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys.

What is prefix key in MySQL?

Introduction to MySQL Prefix IndexMySQL allows you to optionally create column prefix key parts for CHAR , VARCHAR , BINARY , and VARBINARY columns. If you create indexes for BLOB and TEXT columns, you must specify the column prefix key parts.


2 Answers

The problem is:

PRIMARY KEY (`id`(7))

You cannot use part of a number as a key, you have to use the whole thing. Also, specifying lengths for numeric types is useless at best, and damaging at worst.

Change to:

PRIMARY KEY (`id`)
like image 60
Sammitch Avatar answered Oct 20 '22 00:10

Sammitch


That Primary Key syntax is nothing I've ever seen before. Try this:

CREATE TABLE `b2b`.`users` (
 `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ....
) ENGINE = InnoDB;

Or if you want

CREATE TABLE `b2b`.`users` (
 `id` BIGINT NOT NULL AUTO_INCREMENT,
   ....
 PRIMARY KEY (id) 
) ENGINE = InnoDB;
like image 37
Kenney Avatar answered Oct 20 '22 01:10

Kenney