473,800 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Primary Key datatype = varchar

3 New Member
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 4696
sfh
3 New Member
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
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) );
0
3790
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 representation, or maybe some other datatype, and ${128bitvalue} is a representation of it acceptable to MySQL. Suppose 'smallkey' is an UNSIGNED INTEGER column. Which of the following two queries is likely to be faster and more efficient?
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...
0
2406
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) unsigned NOT NULL auto_increment primary key, color varchar(45) default NULL ) TYPE=INNODB DEFAULT CHARSET=latin1;
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
2055
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 fields. Is there any global setting which is causing this? Any other ideas? Thanks in advance.
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
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
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
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.