Page 254 - Open Soource Technologies 304.indd
P. 254

Web Technologies-I



                   Notes         10.4.5 Sequences
                                 Not every RDBMS  has  the ability to  assign  unique row  IDs,  and  those  that do  have  wildly
                                 differing ways of returning that information. PEAR DB sequences are an alternative to database-
                                 specific ID assignment (for instance, MySQL’sAUTO_INCREMENT).
                                 The nextID( ) method returns the next ID for the given sequence:
                                 $id = $db->nextID(sequence);
                                 Normally you will have one sequence per table for which you want unique IDs. This example
                                 inserts values into the books table, giving a unique identifier to each row:
                                 $books = array(array(‘Foundation’, 1951),

                                                 array(‘Second Foundation’, 1953),
                                                 array(‘Foundation and Empire’, 1952));



                                 foreach ($books as $book) {
                                   $id = $db->nextID(‘books’);
                                   splice($book, 0, 0, $id);

                                   $db->query(‘INSERT INTO books (bookid,title,pub_year) VALUES (?,?,?)’, $book);
                                 }



                                 A sequence is really a table in the database that keeps track of the last-assigned ID. You can
                                 explicitly create and destroy sequences with the createSequence( ) and dropSequence( ) methods:

                                 $res = $db->createSequence(sequence);
                                 $res = $db->dropSequence(sequence);


                                 The result will be the result object from the create or drop query or DB_ERROR if an error
                                 occurred.




                                               Create a database and a sequence. Write the command to delete sequence from
                                             the database.

                                 10.4.6 Metadata
                                 The getListOf( ) method lets you query the database for information on available databases,
                                 users, views, and functions:
                                 $data = $db->getListOf(what);



                                 The what parameter is a string identifying the database feature to list. Most databases support
                                 “databases;” some support “users,” “views,” and “functions.”






        248                               LOVELY PROFESSIONAL UNIVERSITY
   249   250   251   252   253   254   255   256   257   258   259