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

Primary Keys

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 an existing username. I have a
database full of the same usernames!
Jul 16 '05 #1
6 3555
On Sun, 17 Aug 2003 15:43:00 +0000 (UTC), "John Simmons"
<Jo**********@hisPC.net> wrote:
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 an existing username. I have a
database full of the same usernames!


Which database? Can you post your table definition?

Without more information the only likely reasons are (a) you haven't actually
added the primary key correctly or (b) your database is severely broken.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2

"John Simmons" <Jo**********@hisPC.net> wrote in message
news:bh**********@hercules.btinternet.com...
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 an existing username. I have a database full of the same usernames!

PHP allows duplicate Primary Keys though it is not recommended at all. You
must specify that your primary key be "UNIQUE" when you add the primary key
field.

At your MySQL command prompt Try:

ALTER TABLE `thetable` ADD UNIQUE (`username`) ;

-JD
Jul 16 '05 #3
On Sun, 17 Aug 2003 16:00:46 GMT, "Jamie Davison" <ja***@bardavon.org> wrote:
"John Simmons" <Jo**********@hisPC.net> wrote in message
news:bh**********@hercules.btinternet.com...
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 an existing username. I have
a database full of the same usernames!

PHP allows duplicate Primary Keys though it is not recommended at all. You
must specify that your primary key be "UNIQUE" when you add the primary key
field.

At your MySQL command prompt Try:

ALTER TABLE `thetable` ADD UNIQUE (`username`) ;


Would you mind backing up this statement with some evidence please? PHP has
_nothing_ to do with primary keys, it's entirely the database's responsibility.
You can never have duplicate primary keys in a table; it's a contradiction.

A unique key constraint enforces uniqueness of a set of fields, which may
contain NULL values. You can have more than one unique key constraint on a
table.

A primary key constraint enforces uniqueness of a set of fields, which may NOT
contain NULL values. You can have only one primary key constraint on a table. A
primary key is also a unique identity of the entity represented by the row.

Since a primary key is more restrictive than a unique key, there's no point
adding a unique key on top of a primary key constraint (and you can't, anyway).

mysql> create table pktest (id int not null);
Query OK, 0 rows affected (0.04 sec)

mysql> alter table pktest add primary key (id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table pktest add unique (id);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc pktest;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | | PRI | 0 | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

The 'unique' specification gets ignored as it's redundant. Other databases may
reject it with an error (e.g. Oracle would say 'such column list already
indexed').

mysql> insert into pktest values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into pktest values (1);
ERROR 1062: Duplicate entry '1' for key 1

Duplicates not allowed; it's a primary key.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4
Andy Hassall wrote:

mysql> insert into pktest values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into pktest values (1);
ERROR 1062: Duplicate entry '1' for key 1

Duplicates not allowed; it's a primary key.


I reckon he's not checking in his code to see if MySQL
raised an error; i.e., just executing the INSERT
and carrying on regardless...
Another case of "Please check mysql_error()"...

Still working on the faq/wiki thing; just a bit worried
about potential bandwidth, although I may be able to get
it free on tuxfamily.org, given the nature of the site

Matt
Jul 16 '05 #5
On Sun, 17 Aug 2003 21:04:13 +0000 (UTC), "John Simmons"
<Jo**********@hisPC.net> wrote:
Ok, see attached zipped html file for details of my layout. And yes, we all
don't
like attachments in posts but it's far easier to read in this format than
trying to line up columns in a post. Please also bare in mind, I've only
been at this for a few weeks so I'm not asking for you to "Please check
mysql_error()"..., I'm simply asking for some advice as I am sure you have
at times.

My PHP code to update the table goes as follows:

mysql_select_db($DB);
$sql = "INSERT INTO " . $Table . " VALUES('','" . $_POST['username'] . "','"
. md5($_POST['password']) . "','" . $_POST['firstname'] . "','" .
$_POST['lastname'] . "','" . $_POST['email'] . "','" . $_POST['address1'] .
"','" . $_POST['address2'] . "','" . $_POST['county'] . "','" .
$_POST['postcode'] . "','" . $_POST['country'] . "')";
if(!($result = mysql_query($sql)))
{
die(mysql_error());
}

With this layout I would have expected to get some sort of error report back
about a duplicate key(?)


From the attached file:

Fields

Field Type Null Key Default Extra
ID
smallint(3)

PRI
(NULL)
auto_increment

username
varchar(15)

PRI
i.e. you have a composite primary key of (ID, username), meaning you're only
limiting unique usernames per value of ID. Since ID is auto_increment, you get
a new one each time.

What you want is primary key (ID), unique (username).

alter table member drop primary key;
alter table member add primary key (ID);
alter table member add unique (username);

You'll have to sort out the duplicates first, though.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #6
Excellent, just what I needed. Thanks for this Andy.

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:tq********************************@4ax.com...
On Sun, 17 Aug 2003 21:04:13 +0000 (UTC), "John Simmons"
<Jo**********@hisPC.net> wrote:
Ok, see attached zipped html file for details of my layout. And yes, we alldon't
like attachments in posts but it's far easier to read in this format than
trying to line up columns in a post. Please also bare in mind, I've only
been at this for a few weeks so I'm not asking for you to "Please check
mysql_error()"..., I'm simply asking for some advice as I am sure you haveat times.

My PHP code to update the table goes as follows:

mysql_select_db($DB);
$sql = "INSERT INTO " . $Table . " VALUES('','" . $_POST['username'] . "','". md5($_POST['password']) . "','" . $_POST['firstname'] . "','" .
$_POST['lastname'] . "','" . $_POST['email'] . "','" . $_POST['address1'] .."','" . $_POST['address2'] . "','" . $_POST['county'] . "','" .
$_POST['postcode'] . "','" . $_POST['country'] . "')";
if(!($result = mysql_query($sql)))
{
die(mysql_error());
}

With this layout I would have expected to get some sort of error report backabout a duplicate key(?)
From the attached file:

Fields

Field Type Null Key Default Extra
ID
smallint(3)

PRI
(NULL)
auto_increment

username
varchar(15)

PRI
i.e. you have a composite primary key of (ID, username), meaning you're

only limiting unique usernames per value of ID. Since ID is auto_increment, you get a new one each time.

What you want is primary key (ID), unique (username).

alter table member drop primary key;
alter table member add primary key (ID);
alter table member add unique (username);

You'll have to sort out the duplicates first, though.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

Jul 16 '05 #7

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

Similar topics

17
by: Philip Yale | last post by:
I'm probably going to get shot down with thousands of reasons for this, but I've never really heard or read a convincing explanation, so here goes ... Clustered indexes are more efficient at...
7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
5
by: Geoff Cayzer | last post by:
At http://www.blueclaw-db.com/tips_tricks.htm I came across a section which is included below and was hoping for some comment on the article. -------------- Almost never use this auto-number...
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...
18
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
7
by: Dave | last post by:
Hi, Maybe I'm missing something with the DataKeyField attribute of a datagrid but it seems that it's somewhat limiting since this only allows you to specify one field as the key. I have a...
1
by: GGerard | last post by:
Hello I'm trying to find the best way to set indexes and primary keys on MSAccess tables What are the advantages and disadvantages of indexes and primary keys? What fields should be indexed?...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
2
by: Danny | last post by:
Hello, We imported a bunch of tables from a database and realized that the primary keys weren't copied to the destination db. In order to re- create the keys, we need to know which tables have...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.