CREATETABLE–SQLCommand

来源:互联网 发布:湖北襄阳seo 编辑:程序博客网 时间:2024/05/04 06:40
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
CREATETABLE–SQLCommand

Createsatablehavingthespecifiedfields.

CREATETABLE|DBFTableName1[NAMELongTableName][FREE](FieldName1FieldType[(nFieldWidth[,nPrecision])][NULL|NOTNULL][CHECKlExpression1[ERRORcMessageText1]][DEFAULTeExpression1][PRIMARYKEY|UNIQUE][REFERENCESTableName2[TAGTagName1]][NOCPTRANS][,FieldName2...][,PRIMARYKEYeExpression2TAGTagName2|,UNIQUEeExpression3TAGTagName3][,FOREIGNKEYeExpression4TAGTagName4[NODUP]REFERENCESTableName3[TAGTagName5]][,CHECKlExpression2[ERRORcMessageText2]])|FROMARRAYArrayNameParameters
TableName1
Specifiesthenameofthetabletocreate.TheTABLEandDBFoptionsareidentical.
NAMELongTableName
Specifiesalongnameforthetable.Alongtablenamecanbespecifiedonlywhenadatabaseisopenbecauselongtablenamesarestoredindatabases.

Longnamescancontainupto128charactersandcanbeusedinplaceofshortfilenamesinthedatabase.

FREE
Specifiesthatthetablewillnotbeaddedtoanopendatabase.FREEisn'trequiredifadatabaseisn'topen.
(FieldName1FieldType[(nFieldWidth[,nPrecision])]
Specifiesthefieldname,fieldtype,fieldwidth,andfieldprecision(numberofdecimalplaces),respectively.

Asingletablecancontainupto255fields.Ifoneormorefieldsallownullvalues,thelimitisreducedbyoneto254fields.

FieldTypeisasingleletterindicatingthefield'sdatatype.SomefielddatatypesrequirethatyouspecifynFieldWidthornPrecision,orboth.

ThefollowingtableliststhevaluesforFieldTypeandwhethernFieldWidthandnPrecisionarerequired.

FieldTypenFieldWidthnPrecisionDescriptionCharacterfieldofwidthnNumericfieldofwidthnwithddecimalplacesFloatingnumericfieldofwidthnwithddecimalplaces

nFieldWidthandnPrecisionareignoredforD,T,I,Y,L,M,G,andPtypes.nPrecisiondefaultstozero(nodecimalplaces)ifnPrecisionisn'tincludedfortheNorFtypes.nPrecisiondefaultstothenumberofdecimalplacesspecifiedbytheSETDECIMALsettingifnPrecisionisn'tincludedfortheBtype.

NULL
Allowsnullvaluesinthefield.Ifoneormorefieldscancontainnullvalues,themaximumnumberoffieldsthetablecancontainisreducedbyone,from255to254.
NOTNULL
Preventsnullvaluesinthefield.

IfyouomitNULLandNOTNULL,thecurrentsettingofSETNULLdeterminesifnullvaluesareallowedinthefield.However,ifyouomitNULLandNOTNULLandincludethePRIMARYKEYorUNIQUEclause,thecurrentsettingofSETNULLisignoredandthefielddefaultstoNOTNULL.

CHECKlExpression1
Specifiesavalidationruleforthefield.lExpression1canbeauser-definedfunction.Notethatwhenablankrecordisappended,thevalidationruleischecked.Anerrorisgeneratedifthevalidationruledoesn'tallowforablankfieldvalueinanappendedrecord.
ERRORcMessageText1
SpecifiestheerrormessageVisualdisplayswhenthevalidationrulespecifiedwithCHECKgeneratesanerror.ThemessageisdisplayedonlywhendataischangedwithinaBrowsewindoworEditwindow.
DEFAULTeExpression1
Specifiesadefaultvalueforthefield.ThedatatypeofeExpression1mustbethesameasthefield'sdatatype.
PRIMARYKEY
Createsaprimaryindexforthefield.Theprimaryindextaghasthesamenameasthefield.
UNIQUE
Createsacandidateindexforthefield.Thecandidateindextaghasthesamenameasthefield.Formoreinformationaboutcandidateindexes,seeSettingaPrimaryorCandidateIndex.Note   Candidateindexes(createdbyincludingtheUNIQUEoptioninCREATETABLEorALTERTABLE–SQL)arenotthesameasindexescreatedwiththeUNIQUEoptionintheINDEXcommand.AnindexcreatedwiththeUNIQUEoptionintheINDEXcommandallowsduplicateindexkeys;candidateindexesdonotallowduplicateindexkeys.SeeINDEXforadditionalinformationonitsUNIQUEoption.

Nullvaluesandduplicaterecordsarenotpermittedinafieldusedforaprimaryorcandidateindex.However,VisualFoxProwillnotgenerateanerrorifyoucreateaprimaryorcandidateindexforafieldthatsupportsnullvalues.VisualFoxProwillgenerateanerrorifyouattempttoenteranullorduplicatevalueintoafieldusedforaprimaryorcandidateindex.

REFERENCESTableName2[TAGTagName1]
Specifiestheparenttabletowhichapersistentrelationshipisestablished.IfyouomitTAGTagName1,therelationshipisestablishedusingtheprimaryindexkeyoftheparenttable.Iftheparenttabledoesnothaveaprimaryindex,VisualFoxProgeneratesanerror.

IncludeTAGTagName1toestablisharelationbasedonanexistingindextagfortheparenttable.Indextagnamescancontainupto10characters.

Theparenttablecannotbeafreetable.

NOCPTRANS
Preventstranslationtoadifferentcodepageforcharacterandmemofields.Ifthetableisconvertedtoanothercodepage,thefieldsforwhichNOCPTRANShasbeenspecifiedarenottranslated.NOCPTRANScanonlybespecifiedforcharacterandmemofields.ThiswillcreatewhatappearsintheTableDesignerasCharacter(binary)andMemo(binary)datatypes.

ThefollowingexamplecreatesatablenamedMYTABLEcontainingtwocharacterfieldsandtwomemofields.ThesecondcharacterfieldCHAR2andthesecondmemofieldMEMO2includeNOCPTRANStopreventtranslation.CREATETABLEmytable(char1C(10),char2C(10)NOCPTRANS,;memo1M,memo2MNOCPTRANS)

PRIMARYKEYeExpression2TAGTagName2
Specifiesaprimaryindextocreate.eExpression2specifiesanyfieldorcombinationoffieldsinthetable.TAGTagName2specifiesthenamefortheprimaryindextagthatiscreated.Indextagnamescancontainupto10characters.

Becauseatablecanhaveonlyoneprimaryindex,youcannotincludethisclauseifyouhavealreadycreatedaprimaryindexforafield.VisualFoxProgeneratesanerrorifyouincludemorethanonePRIMARYKEYclauseinCREATETABLE.

UNIQUEeExpression3TAGTagName3
Createsacandidateindex.eExpression3specifiesanyfieldorcombinationoffieldsinthetable.However,ifyouhavecreatedaprimaryindexwithoneofthePRIMARYKEYoptions,youcannotincludethefieldthatwasspecifiedfortheprimaryindex.TAGTagName3specifiesatagnameforthecandidateindextagthatiscreated.Indextagnamescancontainupto10characters.

Atablecanhavemultiplecandidateindexes.

FOREIGNKEYeExpression4TAGTagName4[NODUP]
Createsaforeign(non-primary)index,andestablishesarelationshiptoaparenttable.eExpression4specifiestheforeignindexkeyexpressionandTagName4specifiesthenameoftheforeignindexkeytagthatiscreated.Indextagnamescancontainupto10characters.IncludeNODUPtocreateacandidateforeignindex.

Youcancreatemultipleforeignindexesforthetable,buttheforeignindexexpressionsmustspecifydifferentfieldsinthetable.

REFERENCESTableName3[TAGTagName5]
Specifiestheparenttabletowhichapersistentrelationshipisestablished.IncludeTAGTagName5toestablisharelationbasedonanindextagfortheparenttable.Indextagnamescancontainupto10characters.IfyouomitTAGTagName5,therelationshipisestablishedusingtheparenttable'sprimaryindexkeybydefault.
CHECKeExpression2[ERRORcMessageText2]
Specifiesthetablevalidationrule.ERRORcMessageText2specifiestheerrormessageVisualFoxProdisplayswhenthetablevalidationruleisexecuted.ThemessageisdisplayedonlywhendataischangedwithinaBrowsewindoworEditwindow.
FROMARRAYArrayName
Specifiesthenameofanexistingarraywhosecontentsarethename,type,precision,andscaleforeachfieldinthetable.ThecontentsofthearraycanbedefinedwiththeAFIELDS( )function.
Remarks

Thenewtableisopenedinthelowestavailableworkarea,andcanbeaccessedbyitsalias.Thenewtableisopenedexclusively,regardlessofthecurrentsettingofSETEXCLUSIVE.

Ifadatabaseisopenandyoudon'tincludetheFREEclause,thenewtableisaddedtothedatabase.Youcannotcreateanewtablewiththesamenameasatableinthedatabase.

Ifadatabaseisn'topenwhenyoucreatethenewtable,includingtheNAME,CHECK,DEFAULT,FOREIGNKEY,PRIMARYKEY,orREFERENCESclausesgeneratesanerror.

NotethattheCREATETABLEsyntaxusescommastoseparatecertainCREATETABLEoptions.Also,theNULL,NOTNULL,CHECK,DEFAULT,PRIMARYKEYandUNIQUEclausemustbeplacedwithintheparenthesescontainingthecolumndefinitions.

Example

ThefollowingexamplecreatesanewdatabasenamedMydata1.CREATETABLEisusedtocreatethreetables(Salesman,Customer,andOrders).TheFOREIGNKEYandREFERENCESclausesinthesecondCREATETABLEcommandcreateapersistentone-to-manyrelationshipbetweentheSalesmanandCustomertables.TheDEFAULTclausesinthethirdCREATETABLEcommandestablishdefaultvalues,andtheCHECKandERRORclausesestablishbusinessrulesforenteringdataintospecificfields.TheMODIFYDATABASEisusedtodisplaytherelationshipbetweenthethreetables.

CLOSEDATABASESCLEAR*CreatemydatadatabaseinthecurrentdirectoryorfolderCREATEDATABASEmydata1*CreateasalesmantablewithaprimarykeyCREATETABLEsalesman;(SalesIDc(6)PRIMARYKEY,;SaleNameC(20))*Createacustomertableandrelateittothesalesmantable.CREATETABLEcustomer;(SalesIDc(6),;CustIdiPRIMARYKEY,;CustNamec(20)UNIQUE,;SalesBranchc(3),;FOREIGNKEYSalesIdTAGSalesIdREFERENCESsalesman)*Createanorderstablerelatedtocustomerwithitsownprimary*keyandsomebusinessrulessuchasdefaults&checks.CREATETABLEorders;(OrderIdiPRIMARYKEY,;CustIdiREFERENCEScustomerTAGCustId,;OrderAmty(4),;OrderQtyi;DEFAULT10;CHECK(OrderQty>9);ERROR"OrderQuantitymustbeatleast10",;DiscPercentn(6,2)NULL;DEFAULT.NULL.,;CHECK(OrderAmt>0)ERROR"OrderAmountMustbe>0")*Displaynewdatabase,tables,andrelationshipsMODIFYDATABASE*DeleteexamplefilesSETSAFETYOFF&&TosuppressverificationmessageCLOSEDATABASES&&ClosedatabasebeforedeletingDELETEDATABASEmydata1DELETETABLES

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击