Creating a MySQL table with a basic text key

The command I use for CREATEmy TABLE:

CREATE TABLE carts(order_id TEXT(14), items TEXT, shipping INT, price INT

I would like to set "order_id" as the primary key. I tried ALTER TABLEusing:

ALTER TABLE `carts` ADD PRIMARY KEY(order_id)

But this returns an error:

 #1170 - BLOB/TEXT column 'order_id' used in key specification without a key length

I understand that this means that the length is not set correctly in the initial setup, so I tried:

ALTER TABLE `carts` ADD PRIMARY KEY(order_id(14))

It returns the same error. The type defined in phpmyadmin is "tinytext"; I expected to see TEXT(14).

I execute all these commands through PDO in PHP. What is the correct way to set the column "order_id" as my primary key TABLE?

+3
source share
2 answers

MySQL BLOB/TEXT , . KEY.

VARCHAR (14).

ALTER TABLE `carts` modify order_id VARCHAR(14);

PK.

ALTER TABLE `carts` ADD PRIMARY KEY(order_id);
+3

- , MySQL N BLOB TEXT. , , / TEXT BLOB ( , TEXT BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB, TINYTEXT, MEDIUMTEXT LONGTEXT), .

BLOB TEXT MySQL . , BLOB TEXT N, MySQL . MySQL TEXT BLOB. (11) .

0

All Articles