Question

MySQL Managed Database can't modify database without setting primary key, but I can't set primary key without modifying the database.

Hi, so I currently have a table with the following datatypes: column1 -> TEXT column2 -> TEXT column3 -> TEXT

This table is huge, with over 500k+ rows. When I initially started using MySQL on DO, I hardly knew what a primary key was, so I never set one up.

However, I’ve encountered a loop of a problem: In order to set a primary key, I must have a column in the database which has a specific amount of characters, e.g. VARCHAR(10). If I don’t have it, I will get something similar to this:

Error: BLOB/TEXT column 'columnName' used in key specification without a key length

However, in order to even set my column to have a specific key length, I have to modify the table.

As a result, I try to modify the table, using something on the lines of MODIFY TABLE tableName ALTER COLUMN columnName VARCHAR(length). Which tells me that:

Error: Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset this variable to avoid this message. Note that tables without a primary key can cause performance problems in row-based replication, so please consult your DBA before changing this setting.

Which creates an infinite loop of me not even being able to set a primary key in the first place.

Now I’ve took a look about the possibility of perhaps setting the system variable sql_require_primary_key. However after taking a look at stackoverflow I’ve been suggested it’s not possible to do so.

Can someone help me in this? Thank you.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Priyanka Malik
DigitalOcean Employee
DigitalOcean Employee badge
March 19, 2021
Accepted Answer

Hi @miramallows

You can use the SET SESSION sql_require_primary_key = 0;

It is for a single session though. Once you log out, you would need to run this again before you could do modify the table without primary key.

The below URL has more information on how to create primary keys:

https://www.digitalocean.com/docs/databases/mysql/how-to/create-primary-keys/#how-digitalocean-uses-primary-keys<https://www.digitalocean.com/docs/databases/mysql/how-to/create-primary-keys/>

Regards Priyanka

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.