473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trying to make TEXT column PRIMARY KEY

I created a table that has a column in that needs to contain a full
Unix file path. Since 2048 was too long for a VARCHAR, I made it
TEXT. I since populated the table. Now I want to make the path
column a primary key, and I can't figure out how. (I googled the
web and groups without luck, looked over the reference manual also,
especially reading the entry on BLOBs.)

I was able to make a fulltext index with:

create fulltext index path_idx on the_table (path);

But my attempts to make a primary key have failed:

SQL> alter table the_table add primary key (path);
BLOB column 'path' used in key specification without a key length

SQL> alter table the_table add primary key (path(2048));
Incorrect sub part key. The used key part isn't a string, the used length
is longer than the key part or the table handler doesn't support unique sub keys

SQL> alter table the_table add constraint unique (path);
BLOB column 'path' used in key specification without a key length

Actually, having made the index, all I care about is the unique constraint.
How do I do that?

--
Peter Scott

Jul 23 '05 #1
4 25039
Peter Scott wrote:
I created a table that has a column in that needs to contain a full
Unix file path. Since 2048 was too long for a VARCHAR, I made it
TEXT. I since populated the table. Now I want to make the path
column a primary key, and I can't figure out how. (I googled the
web and groups without luck, looked over the reference manual also,
especially reading the entry on BLOBs.)

I was able to make a fulltext index with:

create fulltext index path_idx on the_table (path);

But my attempts to make a primary key have failed:

SQL> alter table the_table add primary key (path);
BLOB column 'path' used in key specification without a key length

SQL> alter table the_table add primary key (path(2048));
Incorrect sub part key. The used key part isn't a string, the used
length is longer than the key part or the table handler doesn't
support unique sub keys

SQL> alter table the_table add constraint unique (path);
BLOB column 'path' used in key specification without a key length

Actually, having made the index, all I care about is the unique
constraint. How do I do that?


Pretty simple answer. You can't make a text column a primary key. The
only sort of index you can put on a text column is full text.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 23 '05 #2
Chris Hope wrote:
Peter Scott wrote:
I created a table that has a column in that needs to contain a full
Unix file path. Since 2048 was too long for a VARCHAR, I made it
TEXT. I since populated the table. Now I want to make the path
column a primary key, and I can't figure out how. (I googled the
web and groups without luck, looked over the reference manual also,
especially reading the entry on BLOBs.)

I was able to make a fulltext index with:

create fulltext index path_idx on the_table (path);

But my attempts to make a primary key have failed:

SQL> alter table the_table add primary key (path);
BLOB column 'path' used in key specification without a key length

SQL> alter table the_table add primary key (path(2048));
Incorrect sub part key. The used key part isn't a string, the used
length is longer than the key part or the table handler doesn't
support unique sub keys

SQL> alter table the_table add constraint unique (path);
BLOB column 'path' used in key specification without a key length

Actually, having made the index, all I care about is the unique
constraint. How do I do that?


Pretty simple answer. You can't make a text column a primary key. The
only sort of index you can put on a text column is full text.


A quick look in the manual and I stand corrected. However it doesn't
specify whether or not you can make them unique or primary, but you an
always try. Just make sure the maximum prefix length is 255 or 1000
characters depending on your version and table type as specified below.

From http://dev.mysql.com/doc/mysql/en/indexes.html :

The MyISAM and (as of MySQL 4.0.14) InnoDB storage engines also support
indexing on BLOB and TEXT columns. When indexing a BLOB or TEXT column,
you must specify a prefix length for the index. For example:

CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));

Prefixes can be up to 255 bytes long (or 1000 bytes for MyISAM and
InnoDB tables as of MySQL 4.1.2). Note that prefix limits are measured
in bytes, whereas the prefix length in CREATE TABLE statements is
interpreted as number of characters. Take this into account when
specifying a prefix length for a column that uses a multi-byte
character set.

As of MySQL 3.23.23, you can also create FULLTEXT indexes. They are used
for full-text searches. Only the MyISAM table type supports FULLTEXT
indexes and only for CHAR, VARCHAR, and TEXT columns. Indexing always
happens over the entire column and partial (prefix) indexing is not
supported. See Section 12.6, ?Full-Text Search Functions? for details.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 23 '05 #3
Peter Scott wrote:
I created a table that has a column in that needs to contain a full
Unix file path. Since 2048 was too long for a VARCHAR, I made it
TEXT. I since populated the table. Now I want to make the path
column a primary key, and I can't figure out how.
BLOB and TEXT columns can be indexed, but only by using an index prefix
length. That is, only a leading part of uniform length of a BLOB or
TEXT can be used for the index. In MySQL 4.1.2, the prefix can be up to
1000 characters; in earlier versions, the limit is 255 characters.

Read this page for more information:
http://dev.mysql.com/doc/mysql/en/indexes.html

Since you are using this column to store pathnames, it's likely that
there will be a lot of duplication in the leading portion of the
strings. This probably makes it impractical to use for enforcing
uniqueness.
Actually, having made the index, all I care about is the unique constraint.
How do I do that?


One solution might be to make a digest of the long string, using the
MD5() or SHA() functions. The digest is a string of 32 or 40 characters
respectively, regardless of the length of the input string. The value
is very likely to be unique and reproducable based on the input string.
Such a digest value is suitable for use in a unique index, and can be
used to enforce uniqueness of the longer string input as well, to the
extent that each input string results in a unique digest value.

Regards,
Bill K.
Jul 23 '05 #4
In article <ct*********@enews4.newsguy.com>,
Bill Karwin <bi**@karwin.com> writes:
Peter Scott wrote:
I created a table that has a column in that needs to contain a full
Unix file path. Since 2048 was too long for a VARCHAR, I made it
TEXT. I since populated the table. Now I want to make the path
column a primary key, and I can't figure out how. [snip] Actually, having made the index, all I care about is the unique constraint.
How do I do that?


One solution might be to make a digest of the long string, using the
MD5() or SHA() functions.

[snip]

Dang, I was afraid of that. Thanks.

--
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ken1 | last post by:
I am going to drop a primary key from one column and create a new column to be used as primary key in an existing database. The old column was a date column which someone earlier though was a good...
4
by: Mavis Tilden | last post by:
Hi all, So I've been reading the newsgroups, and reading a few books trying to learn SQL and SQL Server 2000. The books tell me I need a Primary Key, and that every table should have one. I know...
9
by: kojim | last post by:
Test page: http://www.key-horse.com/linkt.html I have a td that's styled to look something like a button, and on that button is anchor text. So far, I can't make the entire "surface" of the...
3
by: Danny | last post by:
Hi again I am trying to import from an excel spreadsheet into access 2002, but the order that is in the spreadsheet is not preserved when I import. using DoCmd.TransferSpreadsheet in code. ...
3
by: google | last post by:
This is something I've done plenty of times in '97, but I can't seem to get it to work correctly in Access 2003. Say, for example, I have a form with an unbound combobox, the data source is a...
8
by: Mike Wertheim | last post by:
Hi, I'm using PostgreSQL 8. I have two tables that I am doing a join on, and the join executes very slowly. The table called Notification has a text field called NotificationID, which is...
0
by: Henry | last post by:
I am trying to create a TreeView control that works with an ADO Dataset DataTable or the new BindingSource stuff in .NET 2.0 to build a Treeview that is populated. This is what I came up with...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
6
by: pbd22 | last post by:
Hi. I am trying to insert a zero into the below table and I am being told that: Cannot insert the value NULL into column 'UsageToday', table DB.dbo.Pub_Count'; column does not allow nulls....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.