473,503 Members | 13,285 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Primary key and varchar

3 New Member
Greetings all,

I have a question concerning primary key types.

In the past, I have always created tables with a primary key as an "int" such as:

[COLOR=Blue][FONT=Courier New]CREATE TABLE color_id (
color_id int(10) unsigned NOT NULL auto_increment primary key,
color varchar(45) default NULL
) TYPE=INNODB DEFAULT CHARSET=latin1; [/FONT] [/COLOR]

But as of late I have been creating rather large databases that have many lookup tables.

Here is a quick example database I just created:

[COLOR=Blue][FONT=Courier New]mysql> select * from color_id;
+----------+--------+
| color_id | color |
+----------+--------+
| 1 | Red |
| 2 | Green |
| 3 | Blue |
| 4 | Purple |
| 5 | Yellow |
| 6 | Black |
| 7 | Grey |
+----------+--------+[/FONT][/COLOR]

That would be just a lookup field. and this would be a table with the foreign keys:

[COLOR=Blue][FONT=Courier New]mysql> select * from name_id;
+---------+----------+-------+-------+
| name_id | name | color | shape |
+---------+----------+-------+-------+
| 1 | Jim | 4 | 6 |
| 2 | Scott | 6 | 5 |
| 3 | Cory | 1 | 6 |
| 4 | Jim | 2 | 3 |
| 5 | Janet | 2 | 1 |
| 6 | Bobby | 1 | 6 |
| 7 | Carlos | 3 | 3 |
| 8 | Peter | 6 | 3 |
| 9 | Paul | 7 | 3 |
| 10 | Mary | 3 | 4 |
| 11 | Blair | NULL | NULL |
| 12 | Johnny | NULL | NULL |
| 13 | Mike | 2 | NULL |
| 14 | Morgan | NULL | 5 |
| 15 | Christos | 3 | 3 |
+---------+----------+-------+-------+ [/FONT] [/COLOR]

Ive found this difficult to work with.

Two examples programs are a csv insert/update program I have that is not lookup field aware so it doesent work on tables like this.

Another example is a a front end php program that has some nice sorting featres on the columns, but unfortunately it sorts on the field (key value) as opposed to the looked up value!?!?!

So for my question, What are the issues / drawbacks on creating (on lookup tables) my primary keys as varchar(45)? I know there might be index performance issues, but if every entry in these tables is unique, are there any other reasons for not doing this?

Here is an updated .sql file to represent what I am talking about:

[COLOR=Blue][FONT=Courier New]CREATE TABLE color (
color varchar(45) NOT NULL primary key
) TYPE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE shape (
shape varchar(45) NOT NULL primary key
) TYPE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE name (
name varchar(45) NOT NULL primary key,
color varchar(45) default NULL,
shape varchar(45) default NULL,
INDEX color_ind (color),
INDEX shape_ind (shape),
FOREIGN KEY (color) REFERENCES color(color) ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (shape) REFERENCES shape(shape) ON DELETE SET NULL ON UPDATE CASCADE
) TYPE=INNODB DEFAULT CHARSET=latin1;[/FONT][/COLOR]

Thanks all, take care!
Sep 30 '05 #1
0 2380

Sign in to post your reply or Sign up for a free account.

Similar topics

5
8119
by: Westcoast Sheri | last post by:
Which will be a faster lookup of an item by, "color" in the following mySQL tables: "unique key," "primary key," or just plain "key"?? CREATE TABLE myTable ( number int(11) NOT NULL default '0',...
2
2400
by: skidvd | last post by:
Hello: I have just recently converted to using the InnoDB table type so that I can enforce FOREIGN key constraints. I have been using MyISAM tables (accessed via JDBC) successfully for some...
3
2325
by: William D. Bartholomew | last post by:
I'm working on a system that is very address-centric and detection of duplicate addresses is very important. As a result we have broken addresses down into many parts (DDL below, but I've left out...
1
4685
by: sfh | last post by:
Greetings all, (I had posted this in MS SQL on accident, my apologies :( ) I have a question concerning primary key types. In the past, I have always created tables with a primary key as...
6
2472
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
1
1180
by: Kevin Murphy | last post by:
Using PG 7.4.5 on Mac OS X 10.3 ... I have a primary key that I can't destroy and can't create. One weird symptom is that when I use \d in psql to attempt to display the constraint, there is no...
7
20414
by: D. | last post by:
Hi, I'm planning the structure of a SqlServer 2005 database for a new application. The requirement is that primary keys must be "natural"; i.e. in the table Customers the primary key will be a...
4
3815
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the...
1
3347
by: Zamdrist | last post by:
Violation of PRIMARY KEY constraint 'PK_CUSTOM2'. Cannot insert duplicate key in object 'MHGROUP.Custom2' Is there ANY other reason this violation of the primary key would happen OTHER than a...
0
7212
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
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
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
7364
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...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.