Product Documentation

FairCom SQL for PHP PDO

Previous Topic

Next Topic

Define

Define() establishes specific data definitions. This involves defining columns/fields and creating the tables/files with optional indexes.

Below is the code for Define():

//

// Define()

//

// Create the tables

//

function Defines($ses) {

print("\t<h4>DEFINE</h4>\n");

if (!$ses->beginTransaction())

Handle_Error($ses, "beginTransaction()");

// delete tables ...

Delete_Tables($ses);

// ...and re-create them with constraints

Create_CustomerMaster_Table($ses);

Create_CustomerOrders_Table($ses);

Create_OrderItems_Table($ses);

Create_ItemMaster_Table($ses);

if (!$ses->commit())

Handle_Error($ses, "commit");

}

//

// Delete_Tables()

//

// This function removes all existing tables

//

function Delete_Tables ($ses) {

$qry = $ses->exec("DROP TABLE ordritem");

if (!$qry)

Handle_Error($ses, "exec(DROP TABLE)");

$qry = $ses->exec("DROP TABLE custordr");

if (!$qry)

Handle_Error($ses, "exec(DROP TABLE)");

$qry = $ses->exec("DROP TABLE custmast");

if (!$qry)

Handle_Error($ses, "exec(DROP TABLE)");

$qry = $ses->exec("DROP TABLE itemmast");

if (!$qry)

Handle_Error($ses, "exec(DROP TABLE)");

}

//

// Create_CustomerMaster_Table()

//

// Create the table CustomerMaster

//

function Create_CustomerMaster_Table ($ses) {

print("\t\ttable CustomerMaster<br>\n");

$qry = $ses->exec(

"CREATE TABLE custmast (

cm_custnumb CHAR(4) PRIMARY KEY,

cm_custzipc CHAR(9),

cm_custstat CHAR(2),

cm_custrtng CHAR(1),

cm_custname VARCHAR(47),

cm_custaddr VARCHAR(47),

cm_custcity VARCHAR(47))");

if (!$qry)

Handle_Error($ses, "exec(CREATE TABLE)");

}

//

// Create_CustomerOrders_Table()

//

// Create the table CustomerOrders

//

function Create_CustomerOrders_Table ($ses) {

print("\t\ttable CustomerOrders<br>\n");

$qry = $ses->exec(

"CREATE TABLE custordr (

co_ordrdate DATE,

co_promdate DATE,

co_ordrnumb CHAR(6) PRIMARY KEY,

co_custnumb CHAR(4),

FOREIGN KEY (co_custnumb) REFERENCES custmast)");

if (!$qry)

Handle_Error($ses, "exec(CREATE TABLE)");

}

//

// Create_OrderItems_Table()

//

// Create the table OrderItems

//

function Create_OrderItems_Table ($ses) {

print("\t\ttable OrderItems<br>\n");

$qry = $ses->exec(

"CREATE TABLE ordritem (

oi_sequnumb SMALLINT,

oi_quantity SMALLINT,

oi_ordrnumb CHAR(6),

oi_itemnumb CHAR(5),

FOREIGN KEY (oi_itemnumb) REFERENCES itemmast,

FOREIGN KEY (oi_ordrnumb) REFERENCES custordr)");

if (!$qry)

Handle_Error($ses, "exec(CREATE TABLE)");

}

//

// Create_ItemMaster_Table()

//

// Create the table ItemMaster

//

function Create_ItemMaster_Table ($ses) {

print("\t\ttable ItemMaster<br>\n");

$qry = $ses->exec(

"CREATE TABLE itemmast (

im_itemwght INTEGER,

im_itempric MONEY,

im_itemnumb CHAR(5) PRIMARY KEY,

im_itemdesc VARCHAR(47))");

if (!$qry)

Handle_Error($ses, "exec(CREATE TABLE)");

}

TOCIndex