WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Creating Auto-incrementing ID Fields with PHP and Oracle
Categories : PHP, PHP options/info, Databases, Oracle Picture not available
Kevin Porter
Date : 2000-05-17
Grade : 0 of 5 (graded 0 times)
Viewed : 9293
Search : More Articles by Kevin Porter
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


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.



We'll use the tried and tested emp table as an example, created with:


create table emp
(
id number(5),
name varchar2(20),
surname varchar2(30)
);




It's a bit simple, but we're only interested in one field in this case, the id field.



The next step is to create a sequence:


create sequence seq_emp_id
start with 1
increment by 1;
/


Next, the trigger:


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;
/


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.



Now, when you insert a couple of rows from PHP:


$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 )


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:


SQL> select * from emp order by id;

ID NAME SURNAME
---------- -------------------- ------------------------------
1 Ali G
2 Keith Chegwin








Abstracting Oracle Connectivity with PHP and OCI8
Categories : PHP, Databases, OCI8, Oracle
Descriptions of Common Data Types
Categories : MySQL, Databases, PHP, PHP options/info, General
Simple Connection to Oracle with PHP
Categories : PHP, Oracle, Databases
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
Honey, I Shrunk My Website
Categories : PHP, PHP options/info, Site Planning, Other
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Custom MySQL-functions
Categories : Databases, MySQL, PHP, PHP Functions
Referer Statistics
Categories : PHP, MySQL, HTTP, Databases
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
Simple Connection to Sybase with PHP
Categories : PHP, Sybase, Databases
User identification using cookies in PHP and MySQL
Categories : PHP, Databases, MySQL, Cookies
Simple ODBC Connections with PHP
Categories : PHP, ODBC, Databases
Multicolumn Output from a Database with PHP
Categories : PHP, Databases, HTML and PHP, MySQL
Simple Connection to Informix with PHP
Categories : PHP, Informix, Databases