What is causing MySQL error 1071 (key too long) in this case?










1















I have a following table encoded in utf8mb4:



CREATE TABLE IF NOT EXISTS `account` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NOT NULL,
`username` VARCHAR(254) NOT NULL,
`password` CHAR(60) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_table1_customer_idx` (`customer_id` ASC),
UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC),
CONSTRAINT `fk_table1_customer`
FOREIGN KEY (`customer_id`)
REFERENCES `customer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
ROW_FORMAT = DYNAMIC;


I need to add a boolean column to it, so here's what I do:



ALTER TABLE `account` 
ADD COLUMN `is_customer_admin`
INT(4) NOT NULL DEFAULT 0
AFTER `customer_id`;


I also tried to add specifically a BOOLEAN column instead of INT(4)



However, I get the error:




ERROR 1071 (42000): Specified key was too long; max key length is 767
bytes




It's the first time I encounter an error like that. I did find some questions about that specific error, however, I could not apply it to my situation myself.



From this question I understand that username might be too long, but then I don't understand how did they create that table in the first place. My query does not touch that field.










share|improve this question
























  • have you some index on username ??

    – scaisEdge
    Nov 15 '18 at 19:20











  • It's not my database, so pardon the lack of detail... I'll replace the table structure with the query they used to create it

    – JaffParker
    Nov 15 '18 at 19:21











  • What is your MySQL server version ?

    – Madhur Bhaiya
    Nov 15 '18 at 19:30






  • 2





    Possible duplicate of #1071 - Specified key was too long; max key length is 767 bytes

    – DanB
    Nov 15 '18 at 19:32











  • @MadhurBhaiya 10.1.31-MariaDB

    – JaffParker
    Nov 15 '18 at 19:32
















1















I have a following table encoded in utf8mb4:



CREATE TABLE IF NOT EXISTS `account` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NOT NULL,
`username` VARCHAR(254) NOT NULL,
`password` CHAR(60) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_table1_customer_idx` (`customer_id` ASC),
UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC),
CONSTRAINT `fk_table1_customer`
FOREIGN KEY (`customer_id`)
REFERENCES `customer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
ROW_FORMAT = DYNAMIC;


I need to add a boolean column to it, so here's what I do:



ALTER TABLE `account` 
ADD COLUMN `is_customer_admin`
INT(4) NOT NULL DEFAULT 0
AFTER `customer_id`;


I also tried to add specifically a BOOLEAN column instead of INT(4)



However, I get the error:




ERROR 1071 (42000): Specified key was too long; max key length is 767
bytes




It's the first time I encounter an error like that. I did find some questions about that specific error, however, I could not apply it to my situation myself.



From this question I understand that username might be too long, but then I don't understand how did they create that table in the first place. My query does not touch that field.










share|improve this question
























  • have you some index on username ??

    – scaisEdge
    Nov 15 '18 at 19:20











  • It's not my database, so pardon the lack of detail... I'll replace the table structure with the query they used to create it

    – JaffParker
    Nov 15 '18 at 19:21











  • What is your MySQL server version ?

    – Madhur Bhaiya
    Nov 15 '18 at 19:30






  • 2





    Possible duplicate of #1071 - Specified key was too long; max key length is 767 bytes

    – DanB
    Nov 15 '18 at 19:32











  • @MadhurBhaiya 10.1.31-MariaDB

    – JaffParker
    Nov 15 '18 at 19:32














1












1








1








I have a following table encoded in utf8mb4:



CREATE TABLE IF NOT EXISTS `account` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NOT NULL,
`username` VARCHAR(254) NOT NULL,
`password` CHAR(60) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_table1_customer_idx` (`customer_id` ASC),
UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC),
CONSTRAINT `fk_table1_customer`
FOREIGN KEY (`customer_id`)
REFERENCES `customer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
ROW_FORMAT = DYNAMIC;


I need to add a boolean column to it, so here's what I do:



ALTER TABLE `account` 
ADD COLUMN `is_customer_admin`
INT(4) NOT NULL DEFAULT 0
AFTER `customer_id`;


I also tried to add specifically a BOOLEAN column instead of INT(4)



However, I get the error:




ERROR 1071 (42000): Specified key was too long; max key length is 767
bytes




It's the first time I encounter an error like that. I did find some questions about that specific error, however, I could not apply it to my situation myself.



From this question I understand that username might be too long, but then I don't understand how did they create that table in the first place. My query does not touch that field.










share|improve this question
















I have a following table encoded in utf8mb4:



CREATE TABLE IF NOT EXISTS `account` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NOT NULL,
`username` VARCHAR(254) NOT NULL,
`password` CHAR(60) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_table1_customer_idx` (`customer_id` ASC),
UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC),
CONSTRAINT `fk_table1_customer`
FOREIGN KEY (`customer_id`)
REFERENCES `customer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
ROW_FORMAT = DYNAMIC;


I need to add a boolean column to it, so here's what I do:



ALTER TABLE `account` 
ADD COLUMN `is_customer_admin`
INT(4) NOT NULL DEFAULT 0
AFTER `customer_id`;


I also tried to add specifically a BOOLEAN column instead of INT(4)



However, I get the error:




ERROR 1071 (42000): Specified key was too long; max key length is 767
bytes




It's the first time I encounter an error like that. I did find some questions about that specific error, however, I could not apply it to my situation myself.



From this question I understand that username might be too long, but then I don't understand how did they create that table in the first place. My query does not touch that field.







mysql mariadb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 19:36







JaffParker

















asked Nov 15 '18 at 19:18









JaffParkerJaffParker

163114




163114












  • have you some index on username ??

    – scaisEdge
    Nov 15 '18 at 19:20











  • It's not my database, so pardon the lack of detail... I'll replace the table structure with the query they used to create it

    – JaffParker
    Nov 15 '18 at 19:21











  • What is your MySQL server version ?

    – Madhur Bhaiya
    Nov 15 '18 at 19:30






  • 2





    Possible duplicate of #1071 - Specified key was too long; max key length is 767 bytes

    – DanB
    Nov 15 '18 at 19:32











  • @MadhurBhaiya 10.1.31-MariaDB

    – JaffParker
    Nov 15 '18 at 19:32


















  • have you some index on username ??

    – scaisEdge
    Nov 15 '18 at 19:20











  • It's not my database, so pardon the lack of detail... I'll replace the table structure with the query they used to create it

    – JaffParker
    Nov 15 '18 at 19:21











  • What is your MySQL server version ?

    – Madhur Bhaiya
    Nov 15 '18 at 19:30






  • 2





    Possible duplicate of #1071 - Specified key was too long; max key length is 767 bytes

    – DanB
    Nov 15 '18 at 19:32











  • @MadhurBhaiya 10.1.31-MariaDB

    – JaffParker
    Nov 15 '18 at 19:32

















have you some index on username ??

– scaisEdge
Nov 15 '18 at 19:20





have you some index on username ??

– scaisEdge
Nov 15 '18 at 19:20













It's not my database, so pardon the lack of detail... I'll replace the table structure with the query they used to create it

– JaffParker
Nov 15 '18 at 19:21





It's not my database, so pardon the lack of detail... I'll replace the table structure with the query they used to create it

– JaffParker
Nov 15 '18 at 19:21













What is your MySQL server version ?

– Madhur Bhaiya
Nov 15 '18 at 19:30





What is your MySQL server version ?

– Madhur Bhaiya
Nov 15 '18 at 19:30




2




2





Possible duplicate of #1071 - Specified key was too long; max key length is 767 bytes

– DanB
Nov 15 '18 at 19:32





Possible duplicate of #1071 - Specified key was too long; max key length is 767 bytes

– DanB
Nov 15 '18 at 19:32













@MadhurBhaiya 10.1.31-MariaDB

– JaffParker
Nov 15 '18 at 19:32






@MadhurBhaiya 10.1.31-MariaDB

– JaffParker
Nov 15 '18 at 19:32













1 Answer
1






active

oldest

votes


















0














In older MySQL / MariaDB versions, maximum key (index) length allowed is 767 bytes only. From Docs:




By default, the index key prefix length limit is 767 bytes. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character. When the innodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.




Now, the problem in your case is increased further, as you are using utf8mb4 character set instead. This means 4 bytes (not 3 bytes) per character.



`username` VARCHAR(254)


This would mean 254 * 4 = 1016 bytes, which is definitely going above the limit. You will need to reduce the maximum characters specified for username column.



Also, the following constraint does not make much sense, as customer_id is already a Primary key.



UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC)


You can get rid of it as well.






share|improve this answer


















  • 1





    Thanks. I didn't think that a column that was already there would block further alterations...

    – JaffParker
    Nov 15 '18 at 19:47










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326521%2fwhat-is-causing-mysql-error-1071-key-too-long-in-this-case%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In older MySQL / MariaDB versions, maximum key (index) length allowed is 767 bytes only. From Docs:




By default, the index key prefix length limit is 767 bytes. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character. When the innodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.




Now, the problem in your case is increased further, as you are using utf8mb4 character set instead. This means 4 bytes (not 3 bytes) per character.



`username` VARCHAR(254)


This would mean 254 * 4 = 1016 bytes, which is definitely going above the limit. You will need to reduce the maximum characters specified for username column.



Also, the following constraint does not make much sense, as customer_id is already a Primary key.



UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC)


You can get rid of it as well.






share|improve this answer


















  • 1





    Thanks. I didn't think that a column that was already there would block further alterations...

    – JaffParker
    Nov 15 '18 at 19:47















0














In older MySQL / MariaDB versions, maximum key (index) length allowed is 767 bytes only. From Docs:




By default, the index key prefix length limit is 767 bytes. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character. When the innodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.




Now, the problem in your case is increased further, as you are using utf8mb4 character set instead. This means 4 bytes (not 3 bytes) per character.



`username` VARCHAR(254)


This would mean 254 * 4 = 1016 bytes, which is definitely going above the limit. You will need to reduce the maximum characters specified for username column.



Also, the following constraint does not make much sense, as customer_id is already a Primary key.



UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC)


You can get rid of it as well.






share|improve this answer


















  • 1





    Thanks. I didn't think that a column that was already there would block further alterations...

    – JaffParker
    Nov 15 '18 at 19:47













0












0








0







In older MySQL / MariaDB versions, maximum key (index) length allowed is 767 bytes only. From Docs:




By default, the index key prefix length limit is 767 bytes. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character. When the innodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.




Now, the problem in your case is increased further, as you are using utf8mb4 character set instead. This means 4 bytes (not 3 bytes) per character.



`username` VARCHAR(254)


This would mean 254 * 4 = 1016 bytes, which is definitely going above the limit. You will need to reduce the maximum characters specified for username column.



Also, the following constraint does not make much sense, as customer_id is already a Primary key.



UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC)


You can get rid of it as well.






share|improve this answer













In older MySQL / MariaDB versions, maximum key (index) length allowed is 767 bytes only. From Docs:




By default, the index key prefix length limit is 767 bytes. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character. When the innodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.




Now, the problem in your case is increased further, as you are using utf8mb4 character set instead. This means 4 bytes (not 3 bytes) per character.



`username` VARCHAR(254)


This would mean 254 * 4 = 1016 bytes, which is definitely going above the limit. You will need to reduce the maximum characters specified for username column.



Also, the following constraint does not make much sense, as customer_id is already a Primary key.



UNIQUE INDEX `unique_account` (`customer_id` ASC, `username` ASC)


You can get rid of it as well.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 19:42









Madhur BhaiyaMadhur Bhaiya

19.6k62236




19.6k62236







  • 1





    Thanks. I didn't think that a column that was already there would block further alterations...

    – JaffParker
    Nov 15 '18 at 19:47












  • 1





    Thanks. I didn't think that a column that was already there would block further alterations...

    – JaffParker
    Nov 15 '18 at 19:47







1




1





Thanks. I didn't think that a column that was already there would block further alterations...

– JaffParker
Nov 15 '18 at 19:47





Thanks. I didn't think that a column that was already there would block further alterations...

– JaffParker
Nov 15 '18 at 19:47



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326521%2fwhat-is-causing-mysql-error-1071-key-too-long-in-this-case%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線