Data Archiving with PHP and Oracle: Creating Archive Tables, Triggers, and PHP Scripts
This article explains how to improve Oracle database performance by creating archive tables and triggers, and demonstrates step‑by‑step PHP code for connecting to Oracle, moving old records, and querying archived data.
In large applications, archiving data in Oracle databases is essential for performance; moving older rows to an archive table reduces query response time. This guide shows how to use PHP together with Oracle to set up data archiving.
1. Preparing for Data Archiving
1.1 Create Archive Table
Use the CREATE TABLE statement in Oracle to define a table for archived records.
<code>CREATE TABLE archive_data (
id NUMBER,
name VARCHAR2(50),
created_date DATE
);
</code>1.2 Create Archive Trigger
A trigger monitors changes on the main table and inserts deleted or updated rows into the archive table.
<code>CREATE OR REPLACE TRIGGER archive_trigger
AFTER DELETE OR UPDATE OF created_date ON main_table
FOR EACH ROW
BEGIN
INSERT INTO archive_data (id, name, created_date)
VALUES (:old.id, :old.name, :old.created_date);
END;
</code>2. Using PHP for Archiving
2.1 Connect to Oracle Database
Establish a connection with oci_connect and handle connection errors.
<code><?php
$conn = oci_connect('username', 'password', 'database');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
?>
</code>2.2 Execute Archiving Operation
Run a DELETE statement to move records older than one year to the archive table.
<code><?php
$sql = 'DELETE FROM main_table WHERE created_date < SYSDATE - 365';
$stmt = oci_parse($conn, $sql);
if (!$stmt) {
$e = oci_error($conn);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$result = oci_execute($stmt);
if (!$result) {
$e = oci_error($stmt);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
oci_free_statement($stmt);
?>
</code>2.3 Query Archived Data
Retrieve and display rows from the archive table.
<code><?php
$sql = 'SELECT * FROM archive_data';
$stmt = oci_parse($conn, $sql);
if (!$stmt) {
$e = oci_error($conn);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$result = oci_execute($stmt);
if (!$result) {
$e = oci_error($stmt);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
while ($row = oci_fetch_array($stmt, OCI_ASSOC)) {
echo $row['ID'] . ", " . $row['NAME'] . ", " . $row['CREATED_DATE'] . "<br>";
}
oci_free_statement($stmt);
?>
</code>Conclusion
By combining PHP with Oracle, developers can easily implement data archiving to boost database performance and reduce query latency, tailoring archiving strategies to specific business needs such as timestamps or data importance.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.