Re: Table locking

From: Date: Sun, 11 Jul 2004 12:35:08 +0000
Subject: Re: Table locking
References: 1 2 3 4 5 6 7 8 9  Groups: php.general 
Request: Send a blank email to php-general+get-190179@lists.php.net to get a copy of this message
Rosen, You've got your insertion order backwards. Insert in table1 Grab the key with mysql_insert_id - assuming you have an autoincrement field as primary key on that table. Call it something like master_key. Insert the detail records in table2, with the key you just grabbed from table1 (master_key) as the foreign key for those records. Note that mysql_insert_id retrieves the last id on a per connection basis, so if 20 people do a near-simultaneous insert each will receive a unique key. You do NOT need to track the number of child records in table2, the key will do the work. So if you want all the information for a given invoice, fetch its master_key: Select master_key, invoice_no, another_field from table1 where invoice_no = '$invoice_no' then after extracting that information, Select * from table2 where table2.master_key = $master_key Let SQL do the work, keep it simple, don't get bogged down in housekeeping. I believe table locking is a bad idea on the web as dropped or slow connections, browsers closed without a logout, and a host of other reasons can leave a record locked and unavailable. Regards - Miles Thompson At 07:44 PM 7/10/2004, you wrote:
I have an orders with one main record in table1 ( client, date, e.t.c. ) and detail description in table2 ( all materials with quant, price, e.t.c. ) and I save data in table1 for positions (range of id - autoinc field of records in table2) for detailed data of order . And I don't want someone else to insert data in table2, because will be a problem with orders. Now I insert data first in table2 and then insert main record in table1 with the range of id's of detail order data. Could be some solution for this ? Thanks in advance. Rosen "John W. Holmes" <holmes072000@charter.net> wrote in message news:40F01718.5090100@charter.net... Rosen wrote:
I need to be sure, thath nobody else will can write on some tables until
I
don't append obout 4-500 records in these tables. But until now I never used table locking in MySQL and I didn't found information about this ( like examples ).
You could try a multi insert syntax such as INSERT INTO yourtable (a,b,c) VALUES (1,2,3),(4,5,6),(7,8,9); which will insert three rows into the table. Couldn't confirm in the manual, but this INSERT should run completely before anything else does. I still have to question _why_ another INSERT in the middle of your insertion will mess things up. Sounds like the problem is there.
Is there a problem with locking if PHP uses same user&pass for all users
in
database ?
No. The LOCK is on a per connection basis and is not tied to the username and/or password. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals ­ www.phparch.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#190179) next »