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

primary key vs unique key

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)
);

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',
KEY (color)
);

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',
PRIMARY KEY (color)
);

Jul 17 '05 #1
5 8113
Westcoast Sheri wrote:
Which will be a faster lookup of an item by, "color" in the following
mySQL tables: "unique key," "primary key," or just plain "key"??
Hi, this is not a PHP related question, is it?

I don't know the answer to your question, and don't know mySQL good, but in
general:
Once a column is indexed, the retrieval based on that column (color here)
will be fast.
If you put a contraint like UNIQUE KEY or PRIMARY KEY on it, it will be
indexed.
(I don't know what KEY means...)

The speed of retrieval of an indexed column is based on the number of
records (doooh) and the storagelogic. I *think/expect* mySQL uses B-tree to
create indexes.

The best way to answer your question is: test it.
Just fill that table with 10.000.000 records and do some timetesting.
(I expect that an UNIQUE or PRIMARY constraint give the same time.)

Sorry I cannot be of more help.
Good luck,

Regards,
Erwin Moller

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)
);

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',
KEY (color)
);

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',
PRIMARY KEY (color)
);


Jul 17 '05 #2
Westcoast Sheri wrote:
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)
);

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',
KEY (color)
);

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',
PRIMARY KEY (color)
);

Primary Key: column(s) that is/are used to reference data in other
tables - Parent-child relationship and there may be duplicates - see
docs for restrictions and usage

Unique Key: column or columns that make a record UNIQUE.
Firstname+Lastname is not necessarily unique, SSN is Unique. NO
Duplicates allowed - this is why it is called UNIQUE.

Foreign Key constraint is used to enforce data integrity such that this
key field must also exist in a "primary" table.

and with MySQL I have no clue what KEY is, I guess I will have to go
read the docs too...

from:
http://dev.mysql.com/doc/mysql/en/CREATE_TABLE.html

KEY is normally a synonym for INDEX. From MySQL 4.1, the key attribute
PRIMARY KEY can also be specified as just KEY when given in a column
definition. This was implemented for compatibility with other database
systems.

In MySQL, a UNIQUE index is one in which all values in the index must be
distinct. An error occurs if you try to add a new row with a key that
matches an existing row. The exception to this is that if a column in
the index is allowed to contain NULL values, it can contain multiple
NULL values. This exception does not apply to BDB tables, for which
indexed columns allow only a single NULL.

Michael Austin.
OracleRdb and Oracle7/8i/9i DBA.
Jul 17 '05 #3
On Tue, 06 Jul 2004 18:08:59 GMT, Michael Austin <ma*****@firstdbasource.com>
wrote:
Primary Key: column(s) that is/are used to reference data in other
tables - Parent-child relationship and there may be duplicates - see
docs for restrictions and usage


Maybe I'm just reading your sentence incorrectly, but it sounds like you're
saying you can have duplicates in the primary key itself. You can't have
duplicates in a primary key.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4
Obviously if you put a unique restraint on the column you can't have
two "orange" or whatever.

Westcoast Sheri <sh*********@nospamun8nospam.com> wrote in message news:<40***************@nospamun8nospam.com>...
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)
);

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',
KEY (color)
);

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',
PRIMARY KEY (color)
);

Jul 17 '05 #5
Andy Hassall wrote:
On Tue, 06 Jul 2004 18:08:59 GMT, Michael Austin <ma*****@firstdbasource.com>
wrote:

Primary Key: column(s) that is/are used to reference data in other
tables - Parent-child relationship and there may be duplicates - see
docs for restrictions and usage

Maybe I'm just reading your sentence incorrectly, but it sounds like you're
saying you can have duplicates in the primary key itself. You can't have
duplicates in a primary key.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

My mistake, a primary key constraint will only allow a unique entry,
even if there are multiple columns in the PK. I am not sure what I had
in mind when I wrote that... my brain went on vacation and has not yet
returned... now where did it go this time ???

Michael Austin.

Jul 17 '05 #6

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

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
1
by: Raquel | last post by:
Here is the scenario: 1. I create a unique index on a table. 2. I create a primary key on that table with same columns as the ones in the unique index by Alter table add primary key statement....
6
by: Bob Stearns | last post by:
I was under the impression that the primary key had to be a unique index. Since I usually create my primary indices before my primary keys, in order to get the indices in the same schema as their...
4
by: deko | last post by:
I have a Make Table query that creates a fairly large table. The Make Table query populates the new table with one AutoNumber field (which is taken form another unrelated table as part of the...
7
by: Philip | last post by:
Hey all, (Access 2000) I've been having a horror story with this design problem. My Database is Structured like This: AUTHORS, BOOKS, PAGES. Those are the Tables and each Item in each table...
3
by: vj_dba | last post by:
Hi Group, I have a Primary key in my table. It's clear Primary key wont allow duplicates, this primary key creates one index for retrival. Suppose if my table is having a Unique index also....
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...
6
by: sachin | last post by:
Hi, I am facing some strange issue in DB2 UDB 9.5.1 I have created a database on DPF implemented environment and I tried to execute following commands Db2 create table test ( name char(10)...
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?
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...
0
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...

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.