CREATETABLE–SQLCommand
2007-11-11 04:48:49 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁诡垎鍐f寖闂佺娅曢幑鍥灳閺冨牆绀冩い蹇庣娴滈箖鏌ㄥ┑鍡欏嚬缂併劎绮妵鍕箳鐎n亞浠鹃梺闈涙搐鐎氫即鐛崶顒夋晬婵絾瀵ч幑鍥蓟閻斿摜鐟归柛顭戝枛椤牆顪冮妶搴′簼缂侇喗鎸搁悾鐑藉础閻愬秵妫冮崺鈧い鎺戝瀹撲礁鈹戦悩鎻掝伀缁惧彞绮欓弻娑氫沪閹规劕顥濋梺閫炲苯澧伴柟铏崌閿濈偛鈹戠€n€晠鏌嶆潪鎷屽厡闁汇倕鎳愮槐鎾存媴閸撴彃鍓卞銈嗗灦閻熲晛鐣烽妷褉鍋撻敐搴℃灍闁绘挻娲橀妵鍕箛闂堟稐绨肩紓浣藉煐濮樸劎妲愰幘璇茬闁冲搫鍊婚ˇ鏉库攽椤旂》宸ユい顓炲槻閻g兘骞掗幋鏃€鐎婚梺瑙勬儗閸樺€熲叺婵犵數濮烽弫鍛婃叏椤撱垹纾婚柟鍓х帛閳锋垶銇勯幒鍡椾壕缂備礁顦遍弫濠氱嵁閸℃稒鍊烽柛婵嗗椤旀劕鈹戦悜鍥╃У闁告挻鐟︽穱濠囨嚃閳哄啰锛滈梺褰掑亰閸欏骸鈻撳⿰鍫熺厸閻忕偟纭堕崑鎾诲箛娴e憡鍊梺纭呭亹鐞涖儵鍩€椤掑啫鐨洪柡浣圭墪閳规垿鎮欓弶鎴犱桓闂佸湱枪閹芥粎鍒掗弮鍫熷仺缂佸顕抽敃鍌涚厱闁哄洢鍔岄悘鐘绘煕閹般劌浜惧┑锛勫亼閸婃牠宕濋敃鈧…鍧楀焵椤掍胶绠剧€光偓婵犱線鍋楀┑顔硷龚濞咃絿妲愰幒鎳崇喓鎷犻懠鑸垫毐闂傚倷鑳舵灙婵炲鍏樺顐ゆ嫚瀹割喖娈ㄦ繝鐢靛У绾板秹寮查幓鎺濈唵閻犺櫣灏ㄥ銉р偓瑙勬尭濡繂顫忛搹鍦<婵☆垰鎼~宥囩磽娴i鍔嶉柟绋垮暱閻g兘骞嬮敃鈧粻濠氭偣閸パ冪骇鐎规挸绉撮—鍐Χ閸℃ê闉嶇紓浣割儐閸ㄥ墎绮嬪澶嬪€锋い鎺嶇瀵灝鈹戦埥鍡楃仯闁告鍕洸濡わ絽鍟崐鍨叏濡厧浜鹃悗姘炬嫹

CREATE TABLE – SQL Command
Creates a table having the specified fields.
CREATE TABLE | DBF TableName1 [NAME LongTableName] [FREE] (FieldName1 FieldType [(nFieldWidth [, nPrecision])] [NULL | NOT NULL] [CHECK lExpression1 [ERROR cMessageText1]] [DEFAULT eExpression1] [Prima(最完善的虚拟主机管理系统)RY KEY | UNIQUE] [REFERENCES TableName2 [TAG TagName1]] [NOCPTRANS] [, FieldName2 ...] [, Prima(最完善的虚拟主机管理系统)RY KEY eExpression2 TAG TagName2 |, UNIQUE eExpression3 TAG TagName3] [, FOREIGN KEY eExpression4 TAG TagName4 [NODUP] REFERENCES TableName3 [TAG TagName5]] [, CHECK lExpression2 [ERROR cMessageText2]])| FROM ARRAY ArrayName
Parameters
- TableName1
- Specifies the name of the table to create. The TABLE and DBF options are identical.
- NAME LongTableName
- Specifies a long name for the table. A long table name can be specified only when a database is open because long table names are stored in databases.
Long names can contain up to 128 characters and can be used in place of short file names in the database.
- FREE
- Specifies that the table will not be added to an open database. FREE isn't required if a database isn't open.
- (FieldName1 FieldType [(nFieldWidth [, nPrecision])]
- Specifies the field name, field type, field width, and field precision (number of decimal places), respectively.
A single table can contain up to 255 fields. If one or more fields allow null values, the limit is reduced by one to 254 fields.
FieldType is a single letter indicating the field's data type. Some field data types require that you specify nFieldWidth or nPrecision, or both.
The following table lists the values for FieldType and whether nFieldWidth and nPrecision are required.
FieldType nFieldWidth nPrecision Description C n – Character field of width n D – – Date T – – DateTime N n d Numeric field of width n with d decimal places F n d Floating numeric field of width n with d decimal places I – – Integer B – d Double Y – – Currency L – – Logical M – – Memo G – – General nFieldWidth and nPrecision are ignored for D, T, I, Y, L, M, G, and P types. nPrecision defaults to zero (no decimal places) if nPrecision isn't included for the N or F types. nPrecision defaults to the number of decimal places specified by the SET DECIMAL setting if nPrecision isn't included for the B type.
- NULL
- Allows null values in the field. If one or more fields can contain null values, the maximum number of fields the table can contain is reduced by one, from 255 to 254.
- NOT NULL
- Prevents null values in the field.
If you omit NULL and NOT NULL, the current setting of SET NULL determines if null values are allowed in the field. However, if you omit NULL and NOT NULL and include the Prima(最完善的虚拟主机管理系统)RY KEY or UNIQUE clause, the current setting of SET NULL is ignored and the field defaults to NOT NULL.
- CHECK lExpression1
- Specifies a validation rule for the field. lExpression1 can be a user-defined function. Note that when a blank record is appended, the validation rule is checked. An error is generated if the validation rule doesn't allow for a blank field value in an appended record.
- ERROR cMessageText1
- Specifies the error message Visual FoxPro displays when the validation rule specified with CHECK generates an error. The message is displayed only when data is changed within a Browse window or Edit window.
- DEFAULT eExpression1
- Specifies a default value for the field. The data type of eExpression1must be the same as the field's data type.
- Prima(最完善的虚拟主机管理系统)RY KEY
- Creates a Prima(最完善的虚拟主机管理系统)ry index for the field. The Prima(最完善的虚拟主机管理系统)ry index tag has the same name as the field.
- UNIQUE
- Creates a candidate index for the field. The candidate index tag has the same name as the field. For more information about candidate indexes, see asp">Setting a Prima(最完善的虚拟主机管理系统)ry or Candidate Index.
Note Candidate indexes (created by including the UNIQUE option in CREATE TABLE or ALTER TABLE – SQL) are not the same as indexes created with the UNIQUE option in the INDEX command. An index created with the UNIQUE option in the INDEX command allows duplicate index keys; candidate indexes do not allow duplicate index keys. See asp">INDEX for additional information on its UNIQUE option.
Null values and duplicate records are not permitted in a field used for a Prima(最完善的虚拟主机管理系统)ry or candidate index. However, Visual FoxPro will not generate an error if you create a Prima(最完善的虚拟主机管理系统)ry or candidate index for a field that supports null values. Visual FoxPro will generate an error if you attempt to enter a null or duplicate value into a field used for a Prima(最完善的虚拟主机管理系统)ry or candidate index.
- REFERENCES TableName2 [TAG TagName1]
- Specifies the parent table to which a persistent relationship is established. If you omit TAG TagName1, the relationship is established using the Prima(最完善的虚拟主机管理系统)ry index key of the parent table. If the parent table does not have a Prima(最完善的虚拟主机管理系统)ry index, Visual FoxPro generates an error.
Include TAG TagName1 to establish a relation based on an existing index tag for the parent table. Index tag names can contain up to 10 characters.
The parent table cannot be a free table.
- NOCPTRANS
- Prevents translation to a different code page for character and memo fields. If the table is converted to another code page, the fields for which NOCPTRANS has been specified are not translated. NOCPTRANS can only be specified for character and memo fields. This will create what appears in the Table Designer as Character (binary) and Memo (binary) data types.
The following example creates a table named MYTABLE containing two character fields and two memo fields. The second character field CHAR2 and the second memo field MEMO2 include NOCPTRANS to prevent translation.
CREATE TABLE mytable (char1 C(10), char2 C(10) NOCPTRANS,; memo1 M, memo2 M NOCPTRANS)
- Prima(最完善的虚拟主机管理系统)RY KEY eExpression2 TAG TagName2
- Specifies a Prima(最完善的虚拟主机管理系统)ry index to create. eExpression2 specifies any field or combination of fields in the table. TAG TagName2 specifies the name for the Prima(最完善的虚拟主机管理系统)ry index tag that is created. Index tag names can contain up to 10 characters.
Because a table can have only one Prima(最完善的虚拟主机管理系统)ry index, you cannot include this clause if you have already created a Prima(最完善的虚拟主机管理系统)ry index for a field. Visual FoxPro generates an error if you include more than one Prima(最完善的虚拟主机管理系统)RY KEY clause in CREATE TABLE.
- UNIQUE eExpression3 TAG TagName3
- Creates a candidate index. eExpression3 specifies any field or combination of fields in the table. However, if you have created a Prima(最完善的虚拟主机管理系统)ry index with one of the Prima(最完善的虚拟主机管理系统)RY KEY options, you cannot include the field that was specified for the Prima(最完善的虚拟主机管理系统)ry index. TAG TagName3 specifies a tag name for the candidate index tag that is created. Index tag names can contain up to 10 characters.
A table can have multiple candidate indexes.
- FOREIGN KEY eExpression4 TAG TagName4 [NODUP]
- Creates a foreign (non-Prima(最完善的虚拟主机管理系统)ry) index, and establishes a relationship to a parent table. eExpression4 specifies the foreign index key expression and TagName4 specifies the name of the foreign index key tag that is created. Index tag names can contain up to 10 characters. Include NODUP to create a candidate foreign index.
You can create multiple foreign indexes for the table, but the foreign index expressions must specify different fields in the table.
- REFERENCES TableName3 [TAG TagName5]
- Specifies the parent table to which a persistent relationship is established. Include TAG TagName5 to establish a relation based on an index tag for the parent table. Index tag names can contain up to 10 characters. If you omit TAG TagName5, the relationship is established using the parent table's Prima(最完善的虚拟主机管理系统)ry index key by default.
- CHECK eExpression2 [ERROR cMessageText2]
- Specifies the table validation rule. ERROR cMessageText2 specifies the error message Visual FoxPro displays when the table validation rule is executed. The message is displayed only when data is changed within a Browse window or Edit window.
- FROM ARRAY ArrayName
- Specifies the name of an existing array whose contents are the name, type, precision, and scale for each field in the table. The contents of the array can be defined with the AFIELDS( ) function.
Remarks
The new table is opened in the lowest available work area, and can be accessed by its alias. The new table is opened exclusively, regardless of the current setting of SET EXCLUSIVE.
If a database is open and you don't include the FREE clause, the new table is added to the database. You cannot create a new table with the same name as a table in the database.
If a database isn't open when you create the new table, including the NAME, CHECK, DEFAULT, FOREIGN KEY, Prima(最完善的虚拟主机管理系统)RY KEY, or REFERENCES clauses generates an error.
Note that the CREATE TABLE syntax uses commas to separate certain CREATE TABLE options. Also, the NULL, NOT NULL, CHECK, DEFAULT, Prima(最完善的虚拟主机管理系统)RY KEY and UNIQUE clause must be placed within the parentheses containing the column definitions.
Example
The following example creates a new database named Mydata1
. CREATE TABLE is used to create three tables (Salesman, Customer,
and Orders
). The FOREIGN KEY and REFERENCES clauses in the second CREATE TABLE command create a persistent one-to-many relationship between the Salesman
and Customer
tables. The DEFAULT clauses in the third CREATE TABLE command establish default values, and the CHECK and ERROR clauses establish business rules for entering data into specific fields. The MODIFY DATABASE is used to display the relationship between the three tables.
CLOSE DATABASESCLEAR* Create mydata database in the current directory or folderCREATE DATABASE mydata1* Create a salesman table with a Prima(最完善的虚拟主机管理系统)ry keyCREATE TABLE
salesman ; (SalesID c(6) Prima(最完善的虚拟主机管理系统)RY KEY, ; SaleName C(20))* Create a customer table and relate it to the salesman table.CREATE TABLE
customer ; (SalesID c(6), ; CustId i Prima(最完善的虚拟主机管理系统)RY KEY, ; CustName c(20) UNIQUE, ; SalesBranch c(3), ;FOREIGN KEY
SalesId TAG SalesIdREFERENCES
salesman)* Create an orders table related to customer with its own Prima(最完善的虚拟主机管理系统)ry* key and some business rules such as defaults & checks.CREATE TABLE
orders ; (OrderId i Prima(最完善的虚拟主机管理系统)RY KEY, ; CustId iREFERENCES
customer TAG CustId, ; OrderAmt y(4), ; OrderQty i ;DEFAULT
10 ;CHECK
(OrderQty > 9) ;ERROR
"Order Quantity must be at least 10", ; DiscPercent n(6,2) NULL ;DEFAULT
.NULL., ;CHECK
(OrderAmt > 0)ERROR
"Order Amount Must be > 0" )* Display new database, tables, and relationshipsMODIFY DATABASE* Delete example filesSET SAFETY OFF && To suppress verification messageCLOSE DATABASES && Close database before deletingDELETE DATABASE mydata1 DELETETABLES
Tags:CREATETABLE SQLCommand
编辑录入:coldstar [复制链接] [打 印]更多精彩
赞助商链接