473,799 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two tables out of sync?



I'm new to databases and am experimenting with mysql.

I have a clients table and a sales table in a mysql database.
Client_id is the primary key in clients and a secondary key in sales.

The only client_id's that show up in the clients table are client_id's
starting at 28 (they autoincrement from there). 1-27 were old ones
that I used in testing. 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?
Probably '1' was the original test client_id and had the same data.
The database seems to have remnants left over from past deleted
records from the clients table.

Does anyone know what's going on? Is there something like a 'pack' or
're-sync' command in mysql?

thanks,
Paul

Jul 20 '05 #1
4 2646
Paul Marcel wrote:
But in sales the client_id is set to '1'. Why isn't it 188?


Sounds to me like you are doing something very wrong which will propably
cause you even more problems later.

You should have something like this:

--------------
| tableone
| ---------
| id
| something_else
--------------

--------------
| tabletwo
|-------------
| id
| tableone_id
| something_else
--------------

And when you insert stuff into tabletwo, that should be linked to
tableone, you need to make sure that the value in tabletwo.tableo ne_id
is the correct id value from tableone.id. Common way to do this is to
use the last_insert_id( ), to get the id for previously inserted row. (
You don't have to have the id-field in tabletwo, but it is commonly a
good idea to have a unique field in table which can be used to identify
rows. )

See this manual page for more info how to use it:
http://dev.mysql.com/doc/mysql/en/Ge...unique_ID.html
Jul 20 '05 #2
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.
Jul 20 '05 #3
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

Jul 20 '05 #4
Paul Marcel wrote:
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.
If you define your tables with the ENGINE=MyISAM clause, foreign keys
are not supported. MySQL will read the command and not complain, and it
does create an index for the column, but there it keeps no record of the
reference between the tables, nor does it enforce the referential
integrity. That is, you can put a value into sales.client_id that does
not exist in clients.client_ id.

To have foreign key referential integrity enforced, use the InnoDB table
type. See the documentation for more information.
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.


INDEX and KEY are synonymous, as far as I know. They are
interchangeable because they accomplish exactly the same thing.

Yes, creating an index on a field is likely to speed up some types of
queries that use that field for table joins, WHERE conditions, or
sorting. But actually this depends on the type of query, and the
variability of data in the field. MySQL tries to be smart about when it
can gain benefit from using an index or not.

For instance, if you sort by a field that has exactly the same value on
every row, there's no benefit from using an index, and in fact it's
unnecessary extra work. MySQL tries to detect conditions like those.

If your data and pattern of typical queries is likely to preclude
usefulness of the index, don't bother to create an index. It costs a
little bit of work for MySQL to maintain the index every time you insert
or update data, and if it never benefits your queries, that's just a
waste. You can always add indexes later if the usage patterns change.

Regards,
Bill K.
Jul 20 '05 #5

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

Similar topics

5
11535
by: Perttu Pulkkinen | last post by:
I want to copy certain tables from server to server b. How do I make it fastly and efficiently with PHP? I wrote below my current start, but it may not be the best approach. Perttu Pulkkinen, FINLAND ---------------------------- <? $host_A = "xxx"; $user_A = "xxx";
0
2126
by: Ferindo Middleton Jr | last post by:
I am trying to write a Perl Function for one of the databases I'm building a web application for. This function is triggered to occur BEFORE INSERT OR UPDATE. This function is complex in that it involves fields in two different tables which need to be updated, where the updates one receives depend on and must actually also reflect the same data entered into one of the tables explicitly by the user. I basically want to make sure that...
1
2433
by: kartik | last post by:
I open an fstream in read-only mode, read till the end, then try to sync() before seeking to position 0 & reading again. But the sync fails. I discovered that clear()ing the stream before the sync causes the sync to work. Seeking to position 0 before the sync doesn't seem to help. If you look at sync() as simply invalidating the read buffer (isn't that right?), isn't it illogical for it to fail in this situation? Besides sync()...
4
1297
by: Stefan Kowalski | last post by:
I recently posted a question which was answered by Allen Browne and gave me some tips to structure the tables. However, when it comes to searching the database, performance is unacceptably slow and I am now thinking of doing the unthinkable with my tables and wonder whether anyone has used (or still uses) this sort of approach. The database stores libraries and classifies the types of book they keep. So I have the classic 3-table...
2
2063
by: DickChristoph | last post by:
Hi I tried posting this query in microsoft.public.sqlserver.programming but got no response. I am new to replication but I am trying to setup up a transactional replication of tables from one database to another in MSSQL 2000 (SP2). My target tables have primary keys defined. Under publication properties I go to the snapshot tab and for each table I clear the check box that says
5
4104
by: rdemyan via AccessMonster.com | last post by:
I have a need to add another field to all of my tables (over 150). Not data, but an actual field. Can I code this somehow. So the code presumabley would loop through all the tables, open each table in design mode and then add the new field and set its properties. Thanks. --
2
1566
by: Kees de Winter | last post by:
Hi, I am thinking about using the personalization features of .net 2.0 but I can't figure out the following. If I use the standard features of personalization, I will have users in the auto-generated profile-tables. But I have my own user tables with many more fields. How do I sync these users with the profile tables? I have thousands of users in my tables but if I can't use these tables for personalization it seems quite useless... ...
0
949
by: sachjn | last post by:
Hi, I have three tables with columns as: Person_sys1 Id(pk) name telephoneNum userid ----- -------- ---------------------- --------- Person_sys2 Id(pk) name telephoneNum userid
7
5218
by: john | last post by:
I am reading TC++PL3 and on page 644 it is mentioned: "Flushing an istream is done using sync(). This cannot always be done right. For some kinds of streams, we would have to reread characters from the real source - and that is not always possible or desirable. Consequently, sync() returns 0 if it succeeded. If it failed, it sets ios_base::badbit (21.3.3) and returns -1. Again, setting badbit might trigger an exception (21.3.6). A sync()...
0
9687
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
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6808
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
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
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
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.