473,385 Members | 1,890 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,385 software developers and data experts.

Primary Key datatype = varchar

3
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:

[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
1 4676
sfh
3
Nevermind,

Completely forgot that the PK cant be edited, completely nullifying the point of an RDBMS....

Sorry....
Oct 2 '05 #2

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

Similar topics

5
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',...
0
by: Jeff | last post by:
Suppose that 'bigkey' is a column capable of representing the value of a 128-bit unsigned integer in some efficient fashion... maybe as a CHAR or VARCHAR field big enough to hold its decimal or hex...
3
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...
0
by: sfh | last post by:
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: CREATE TABLE color_id ( color_id int(10)...
6
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
by: shsandeep | last post by:
Hi, I have a column of datatype CHAR(20). When the DataStage job loads data of length 15 into it, it pads the remaining 5 spaces with blank space. This is happening with all CHAR datatype...
7
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.