Click to See Complete Forum and Search --> : [RESOLVED] Mysql, text fields and primary keys


DJRobThaMan
05-28-2008, 04:49 PM
Hi,

Does anyone know if it's possible to create a table and make a text field its primary key in mysql?

Thanks

chazzy
05-28-2008, 09:34 PM
I can't seem to find anything saying one way or another, but my thought is no, they can't. My rationale is that any key on a TEXT/BLOB type requires a prefix, making a primary key from a prefix wouldn't be sensible.

Really your PK should be an immutable representation of the tuple that is this row.

DJRobThaMan
05-29-2008, 09:46 AM
So... when I went home me and my brother tried a few things and it seems that as long as you make a key no larger than 1024 k (I believe that's the size) a text field can be a key. So some rudimentary math came along and I think that's something close to 124 or 125 characters (can't remember exactly... divide 1024 by 8 and you'll get it). So as long as you limit it to that many characters when you create the field you're good to go. :)

chazzy
05-29-2008, 07:48 PM
right, like i was saying it would be a finite size. text has no actual restriction on how much can be stored in it, based on how mysql has handled it. of course, this all goes away if you come back and say that you meant VARCHAR instead of TEXT.