On Sat, 11 Dec 2004 13:58:56 -0800, Bill Karwin <bi**@karwin.co m> wrote:
Paul Marcel wrote:
I added a new client and clients shows the new
client_id as 188 which makes sense. I added a new sale to the sales
table for this client.
But in sales the client_id is set to '1'. Why isn't it 188?
Each table has its own auto_increment counter. They don't share values.
Generally one doesn't define a foreign key as an auto_increment,
because you need to specify explicit values that actually exist in the
referenced table. Inserting foreign key values is not automatic or
implicit.
Does this help? It's hard to tell if this really addresses your
problem. Can you provide the insert statements that you used for both
tables?
Regards,
Bill K.
Hi,
I'm getting back to the mysql database problem that I was having.
I ran mysqldump and I get this:
DROP TABLE IF EXISTS `clients`;
CREATE TABLE `clients` (
`client_id` int(10) unsigned NOT NULL auto_increment,
`lastname` char(15) NOT NULL default '',
`firstname` char(15) NOT NULL default '',
`birthdate` date NOT NULL default '0000-00-00',
`username` char(20) NOT NULL default '',
`email` char(40) NOT NULL default '',
PRIMARY KEY (`client_id`),
KEY `lastname` (`lastname`),
KEY `firstname` (`firstname`),
KEY `birthdate` (`birthdate`),
KEY `username` (`username`),
KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `sales`;
CREATE TABLE `sales` (
`client_id` int(10) unsigned NOT NULL default '0',
`xname` char(80) NOT NULL default '',
`ms` int(10) unsigned NOT NULL default '0',
KEY `xname` (`xname`),
KEY `ms` (`ms`),
KEY `client_id` (`client_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I created the datbase and tables from a php script. The dump is a little different
from my original script. In the script I had this line in the sales table statement:
constraint foreign key (client_id) references clients (client_id)
instead of the dump's:
KEY `client_id` (`client_id`)
I am trying to have client_id as a primary key in clients and as a foreign key in sales.
Is that happening? I don't see any difference between the key statemens for xname, ms, or client_id
in the sales table.
In clients I added a few other keys because I think that I will need to search on fields like
'lastname'. Did I do that correctly? Does the key directive cause mysql to make an index for that
column? I've seen an explicit index command, but I'm not sure that is necessary.
Should I create these extra keys (lastname, firstname, birthdate, username, email, xname, ms) for
fast searching? Did I do it correctly?
thanks,
Paul