0

Possible Duplicate:
How to set a MySQL row to READ-ONLY?

I need to temporarily protect custom rows of a table from editing by another users. for example when I'm inserting a row with this feature : tid = 51 ; should not let anybody to insert a row , or at least a row with tid = 51 at the same time. How can I do this?

Community
  • 1
  • 1
Amin Gholibeigian
  • 1,325
  • 2
  • 9
  • 11

3 Answers3

0

Usually, an id is assigned to a row only at the moment it is inserted into the database. It does not exist before this moment. After it, it is already insert, and nobody else will be able to insert another row with the same id (assuming it is a uniquely indexed column).

So, your example seems to be invalid. Anyway, the question still makes sense if we ignore the example and consider the opening phrase: "I need to temporarily protect custom rows of a table from editing by another users."

In this case, you need to store somewhere the value of the IDs being edited, and by which user. Then, you need to check this data to allow / disallow an edit action.

In fact, the main problem here is the UNLOCK phase, due to the stateless nature of web/HTTP applications. If the row is not properly UNLOCKED after its edition or some kind of timeout, it will be permanently locked (instead of temporarily).

J. Bruni
  • 20,322
  • 12
  • 75
  • 92
0

What you want goes agains everything a database was designed for. I you want a unique primary key, use a auto increment field. If you want to enforce a unique field value, use a field contraint, perhaps a unique field or a unique combination of fields.

Do NOT lock the table, row or database, in almost any case this is bad design!

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
0

Simply define the column (tid) as an AUTO_INCREMENT field in MYSQL (or any other DBMS, this feature is almost supported everywhere). You don't have to look after the locking, the Database will do it for you.

If you NEED this field's value before inserting a row in the database (wich is actually not always a good design), uou should use a random generated UUID. Thus avoiding collision by minimizing the probability of having two identical values.

ibtarek
  • 795
  • 4
  • 16