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

Posted March 16, 2021 2.3k views
MySQLDigitalOcean Managed MySQL DatabaseSQL

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 sqlrequireprimary_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.

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.

Submit an Answer
1 answer

Hi @miramallows

You can use the SET SESSION sqlrequireprimary_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: