tags:

views:

47

answers:

2

I am kinda new to database designing so i ask for some advices or some kind of a good pattern.

The situation is that, there is one database, few tables and many users. How should i design the database, or / and which types of queries should i use, to make it work, if users can interact with the database simultaneously? I mean, they have access to and can change the same set of data.

I was thinking about transactions, but I am not sure, if that is the right / good / the only solution.

I will appreciate some google keywords too.

UPDATE:

By many i mean hundreds, maybe thousands at all. Clients will be connecting to MySQL through WWW page in PHP. They will use operations such: insert, update, delete and select, sometimes join. It's a small database for 5-20 clients and one-two admins. Clients will be updating and selecting info. I am thinking about transactions with storing some info in $_SESSION.

+2  A: 

a simple approach that can be very effective is the row versioning.

  • add a version int field to the main table,
  • when insert, set it to 0
  • when update, increment it by one; in the where it should be the version field

EXAMPLE:

CREATE TABLE myTable (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
vs INT NOT NULL,
)

INSERT INTO myTable VALUES (1, 'Sebastian', 0)

-- first user reads, vs = 0
SELECT * FROM myTable WHERE id = 1

-- second user reads, vs = 0
SELECT * FROM myTable WHERE id = 1

-- first user writes, vs = 1
UPDATE myTable SET name = 'Juan Sebastian', vs = vs + 1 WHERE id = 1 AND vs = 0
(1 row affected) 

-- second user writes, no rows affected, because vs is different, show error to the user or do your logic
UPDATE myTable SET name = 'Julian', vs = vs + 1 WHERE id = 1 AND vs = 0
(0 rows affected) 
Jhonny D. Cano -Leftware-
By the way, this is known as *optimistic locking* or *optimistic concurrency control* : http://en.wikipedia.org/wiki/Optimistic_concurrency_control
markusk
A: 
  1. Use InnoDB as a engine type. In opposite to MyISAM it supports row-level blocking, so you wouldn't have to block entire table when someone is updating some record.
Crozin