User's Guide
PART 2. Relational Database Concepts
CHAPTER 15. Using Transactions and Locks
This section discusses the following particular concurrency issues:
You will encounter situations where the database should automatically generate a unique number. For example, if you are building a table to store sales invoices you might prefer that the database assign unique invoice numbers automatically, rather than require sales staff pick them.
There are many methods for generating such numbers.
For example, invoice numbers could be obtained by adding 1 to the previous invoice number. This method will not work when there is more than one person adding invoices to the database. Two people may decide to use the same invoice number.
There is more than one solution to the problem:
Assign a range of invoice numbers to each person who adds new invoices.
You could implement this scheme by creating a table with two columns user name and invoice number. The table would have one row for each user that adds invoices. Each time a user adds an invoice, the number in the table would be incremented and used for the new invoice. In order to handle all tables in the database, the table should have three columns: table name, user name, and last key value. You should periodically check that each person still has a sufficient supply of numbers.
Create a table with two columns: table name and last key value.
One row in this table would contain the last invoice number used. Each time someone adds an invoice, establish a new connection, increment the number in the table, and commit the change immediately. The incremented number can be used for the new invoice. Other users will be able to grab invoice numbers because you updated the row with a separate transaction that only lasted an instant.
Probably the best solution is to use a column with a default value of AUTOINCREMENT.
For example,
CREATE TABLE orders ( order_id INTEGER NOT NULL DEFAULT AUTOINCREMENT, order_date DATE, primary key( order_id ) )
On INSERTs into the table, if a value is not specified for the autoincrement column, a unique value is generated. If a value is specified, it will be used. If the value is larger than the current maximum value for the column, that value will be used as a starting point for subsequent INSERTs. The value of the most recently inserted row in an autoincrement column is available as the global variable @@identity.
Unique values in replicated databases |
Autoincrement using PowerBuilder |
The CREATE INDEX statement, ALTER TABLE statement, and DROP statement are prevented whenever the statement affects a table that is currently is used by another connection. These statements can be time consuming and the database server will not process requests referencing the same table while the command is being processed.
The CREATE TABLE statement does not cause any concurrency conflicts.
The GRANT statement, REVOKE statement, and SET OPTION statement also do not cause concurrency conflicts. These commands affect any new SQL statements sent to the database engine, but do not affect existing outstanding statements.
GRANT and REVOKE for a user are not allowed if that user is connected to the database.
Data definition statements and replicated databases |