Question

In my code, I have a routine does some database maintenance upon startup and I use the following code to drop a primary key from a table.

USE Students EXCLUSIVE
ALTER TABLE Students DROP PRIMARY KEY

Life is good if this runs only once and the key is there. However, if it has already been removed, it generates the error code 1879 which I test for and just do a RETURN and things work.

However, I'd like to be able to test for the existence of the key prior to issuing the ALTER TABLE command.

I've searched the help file and MSDN to no avail,I can't imagine that there isn't code to check for the existence of the primary key,but I sure can't find it.

Thanks

Was it helpful?

Solution

You can use the ATAGINFO(), TAG() and PRIMARY() functions to determinate what's your Primary Key index. From the Help of Primary():

CLOSE DATABASES
SET PATH TO (HOME(2) + 'Data\')   && Sets path to database
OPEN DATABASE testdata  && Open testdata database
USE Customer     && Open customer table

FOR nCount = 1 TO 254
   IF !EMPTY(TAG(nCount))  && Checks for tags in the index
   ? TAG(nCount)  && Display tag name
   ? PRIMARY(nCount)     && Display primary status
   ELSE
      EXIT  && Exit the loop when no more tags are found
   ENDIF
ENDFOR

HTH

OTHER TIPS

You can check its existence first:

If !Empty(  DBGetProp("Students","TABLE","PrimaryKey") )
  Alter table students drop primary key
endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top