The command that I use to CREATE
my TABLE
is:
CREATE TABLE carts(order_id TEXT(14), items TEXT, shipping INT, price INT
I'd like to set 'order_id' as my primary key. I've tried to ALTER
the TABLE
with:
ALTER TABLE `carts` ADD PRIMARY KEY(order_id)
But that returns the error:
#1170 - BLOB/TEXT column 'order_id' used in key specification without a key length
I understand that means that the length isn't being set correctly in the initial setup, so I tried:
ALTER TABLE `carts` ADD PRIMARY KEY(order_id(14))
Which returns the same error. The type defined in phpmyadmin is 'tinytext'; I was expecting to see TEXT(14)
.
I'm performing all of these commands via PDO in PHP. What's the correct way to set the column 'order_id' as my TABLE
's primary key?
You can define keys which allow duplicate values. However, do not allow duplicates on primary keys as the value of a record's primary key must be unique.
A primary key is a column of table which uniquely identifies each tuple (row) in that table. Primary key enforces integrity constraints to the table. Only one primary key is allowed to use in a table. The primary key does not accept the any duplicate and NULL values.
in MySQL BLOB/TEXT are not served as a part of table, rather they are referenced from other memory locations. So they cannot be treated as a part of KEY . You have to define it as VARCHAR(14).
in MySQL BLOB/TEXT
are not served as a part of table, rather they are referenced from other memory locations. So they cannot be treated as a part of KEY
.
You have to define it as VARCHAR(14).
ALTER TABLE `carts` modify order_id VARCHAR(14);
And then you can apply the PK.
ALTER TABLE `carts` ADD PRIMARY KEY(order_id);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With