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
//
static void Define()
{
Console.WriteLine("DEFINE");
// delete tables ...
Delete_Tables();
// ...and re-create them with constraints
Create_CustomerMaster_Table();
Create_CustomerOrders_Table();
Create_OrderItems_Table();
Create_ItemMaster_Table();
}
//
// Delete_Tables()
//
// This function removes all existing tables
//
static void Delete_Tables()
{
try
{
cmd.CommandText = "DROP TABLE ordritem";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
try
{
cmd.CommandText = "DROP TABLE custordr";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
try
{
cmd.CommandText = "DROP TABLE custmast";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
try
{
cmd.CommandText = "DROP TABLE itemmast";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
}
//
// Create_CustomerMaster_Table()
//
// Create the table CustomerMaster
//
static void Create_CustomerMaster_Table()
{
Console.WriteLine("\ttable CustomerMaster...");
try
{
cmd.CommandText = "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))";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
}
//
// Create_CustomerOrders_Table()
//
// Create the table CustomerOrders
//
static void Create_CustomerOrders_Table()
{
Console.WriteLine("\ttable CustomerOrders...");
try
{
cmd.CommandText = "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)";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
}
//
// Create_OrderItems_Table()
//
// Create the table OrderItems
//
static void Create_OrderItems_Table()
{
Console.WriteLine("\ttable OrderItems...");
try
{
cmd.CommandText = "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)";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
}
//
// Create_ItemMaster_Table()
//
// Create the table ItemMaster
//
static void Create_ItemMaster_Table()
{
Console.WriteLine("\ttable ItemMaster...");
try
{
cmd.CommandText = "CREATE TABLE itemmast (" +
"im_itemwght INTEGER, " +
"im_itempric MONEY, " +
"im_itemnumb CHAR(5) PRIMARY KEY, " +
"im_itemdesc VARCHAR(47))";
cmd.ExecuteNonQuery();
}
catch (CtreeSqlException e)
{
Handle_Exception(e);
}
catch (Exception e)
{
Handle_Exception(e);
}
}