|
|
|
<H2>Creating Auto-incrementing ID Fields with PHP/Oracle</H2>
<HR>
You will often need to create auto-incrementing fields in your
Oracle tables, most usually in order to generate a unique number for
a table's primary key.
Although there is no inbuilt support for this in Oracle, it is a
relatively simple process to make this behaviour happen. You need to
use Oracle
sequences to generate the numbers, and triggers to automagically
give the ID field a value whenever you
insert a row into the database.
<P>
We'll use the tried and tested emp table as an example, created with:
<PRE>
create table emp
(
id number(5),
name varchar2(20),
surname varchar2(30)
);
</PRE>
It's a bit simple, but we're only interested in one field in this case,
the id field.
<P>
The next step is to create a sequence:
<PRE>
create sequence seq_emp_id
start with 1
increment by 1;
/
</PRE>
Next, the trigger:
<PRE>
create or replace trigger trig_emp
before insert on emp
for each row
begin
select seq_emp_id.nextval into :new.id from dual
end;
/
</PRE>
Once created, this trigger will be activated whenever a row is inserted
into the table.
The select statement may look daunting to everyone but Oracle gurus,
but you should be able to understand the
gist of what is going on. The next value is pulled out of the
sequence and this value is entered into the id field.
<P>
Now, when you insert a couple of rows from PHP:
<PRE>
$sql1 = "insert into emp (name, surname) values ('Ali', 'G' )";
$sql2 = "insert into emp (name, surname) values ('Keith', 'Chegwin' )";
$stmt = OCIParse( $conn, $sql1 );
OCIExecute( $stmt )
$stmt = OCIParse( $conn, $sql2 );
OCIExecute( $stmt )
</PRE>
and then issue a select statement, you will see that the id fields
have been assigned a unique number without you having to
explicitly ask it to in your PHP code:
<PRE>
SQL> select * from emp order by id;
ID NAME SURNAME
---------- -------------------- ------------------------------
1 Ali G
2 Keith Chegwin
</PRE>
|
|
| normalize fields and strings used in where (command's Sql) Categories : PHP, Databases, Oracle, Functions | | | Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0. Categories : Databases, PHP, mSQL, Databases | | | StoredProcedure, Stored Procedure, Oracle, OCI8, OCI8i Categories : OCI8, Oracle, Databases, PHP | | | Connecting to Oracle with php3 Categories : Oracle, PHP, Databases | | | Simple class for accessing databases like MSSql Server, Oracle etc by Raju Categories : PHP, MS SQL Server, Databases, PHP Classes, Oracle | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | PHP CLASS for ORACLE (database connectivity) Categories : PHP, PHP Classes, Classes and Objects, Databases, Oracle | | | A database abstraction layer for the PHP Oracle 8 module (available from PHP 3.0.5). It supports persistent connections, fetching rows into arrays, prepare/execute (variable binding) and has a new and improved error interface. Categories : Databases, Oracle, PHP, Arrays, Variables | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | |
|
|
|