Home > Archive > Programming with dBASE > March 2006 > PRMARY KEY









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PRMARY KEY
Igor

2006-03-09, 8:23 pm

Hi,

How I can create primary key using the code. I tried using the code below, but unsuccessfuly. However, if I omit the creation of the primary key then it works fine (creates table with desired fields).

function createMonthlyTable(c
Table, aFields)
//
local fullPath, i
private cMacro

i = 0
fullPath = ""

try
fullPath = cTable

if _app.databases[1]. tableExists(fullPath
)
_app.databases[1].dropTable(fullPath)
endif

cMacro = [create table "] + fullPath + [" (]

for i = 1 to aFields.size
cMacro += aFields[i]
endfor

cMacro += ",CONSTRAINT AACODE PRIMARY KEY (DC, MERGEDDATE) )"
&cMacro.

catch(exception e)
local cMsg
if i = 0
cMsg = "Expecting at Least One Field Definition!"
else
cMsg = e.message
endif

msgbox(cMsg, "Cannot Create Table", 16)
endtry
return

Thanks

Igor
Ken B

2006-03-09, 8:23 pm

From Online Help:
Constraints are limited to PRIMARY KEY. For DBF7 tables, only single-field
primary keys are supported through the CREATE TABLE command. (Use the Table
Designer or the Xbase INDEX command to create complex primary keys.) Primary
keys are not supported for earlier versions of DBF.


Michael Nuwer [dBVIPS]

2006-03-10, 7:23 am

Igor wrote:
> Hi,
>
> How I can create primary key using the code. I tried using the code below, but unsuccessfuly. However, if I omit the creation of the primary key then it works fine (creates table with desired fields).


The following might work for you.

close data
if file("test.dbf")
drop table test
endif
create table test ( f1 char(10),f2 char(10) )

set procedure to :dUFLP:BDEIndex.cc additive
oIDX = new BDEIndex()
oIDX.indexName := "Primary"
oIDX.expression := "F1+F2"
oIDX.primary := true
// Add an index tag to a table -- for this the
// table should not be opened or in a query:
oIDX.AddTag("test.dbf"," ")
//oIDX.AddTag( cTablename, oDatabase )
// where;
// 'cTableName' is the name of the table
// you wish to add the tag to
// 'oDatabase' is a database object that
// already exists for the BDE Alias -- pass
// an empty string if not using this
*Lysander*

2006-03-10, 8:23 pm

Igor schrieb:

> cMacro += ",CONSTRAINT AACODE PRIMARY KEY (DC, MERGEDDATE) )"


This syntax is for most SQL-servers, but I think for dBase-tables it
is not allowed to use the word "constraint" and also you can NOT
give a name/tag to this index.

The following should work with dBase-7-tables:

cMacro += " PRIMARY KEY (DC, MERGEDDATE) )"

hope this helps.

ciao,
André
Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com