473,800 Members | 2,711 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 2406

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

Similar topics

5
8133
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', description varchar(50) NOT NULL default '', color varchar(30) NOT NULL default '', price decimal(3,2) NOT NULL default '0.00', UNIQUE KEY (color) );
2
2416
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 time. However, I have just come across a problem with the new configuration that boggles my mind.... First some configuration data:
3
2340
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 some reference tables for conciseness), these being state, locality, street, street number, and address. The breakdown is roughly consistent with Australian addressing standards, we're working on finalising this. Because we carry the primary...
1
4696
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 an "int" such as: CREATE TABLE color_id ( color_id int(10) unsigned NOT NULL auto_increment primary key,
6
2505
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
1205
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 output at all! Normally, psql either shows the constraint or reports that the constraint doesn't exist. (BTW, I know that the default clauses below are kind of weird, but I am using PG to process data for a database that doesn't use NULL and...
7
20475
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 max. 10 characters string (but the string may be filled i.e. with only 5 charachters). Should I define these primary keys as char or varchar? I'm interested in your opinion in particular about performace issue, because there will be tables with...
4
3837
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 rule of the business is that only band plays on the one night. The SQL statement prevents a Band name being repeated (as it is Unique). Similar statement for the Venues. CREATE TABLE Bands (BandID varchar(5) CONSTRAINT BandID PRIMARY KEY, Band...
1
3368
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 trying to insert a duplicate record? This sql statement false due to the primary key violation: Insert Into MHGROUP.Custom2 Select
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10279
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7582
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
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 we have to send another system

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.