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] [PRIMARY KEY | UNIQUE] [REFERENCES TableName2 [TAG TagName1]] [NOCPTRANS] [, FieldName2 ...] [, PRIMARY 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
Long names can contain up to 128 characters and can be used in place of short file names in the database.
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.
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 PRIMARY KEY or UNIQUE clause, the current setting of SET NULL is ignored and the field defaults to NOT NULL.
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 INDEX for additional information on its UNIQUE option.
Null values and duplicate records are not permitted in a field used for a primary or candidate index. However, Visual FoxPro will not generate an error if you create a primary 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 primary or candidate index.
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.
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)
Because a table can have only one primary index, you cannot include this clause if you have already created a primary index for a field. Visual FoxPro generates an error if you include more than one PRIMARY KEY clause in CREATE TABLE.
A table can have multiple candidate indexes.
You can create multiple foreign indexes for the table, but the foreign index expressions must specify different fields in the table.
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, PRIMARY 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, PRIMARY KEY and UNIQUE clause must be placed within the parentheses containing the column definitions.
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 DATABASES CLEAR * Create mydata database in the current directory or folder CREATE DATABASE mydata1 * Create a salesman table with a primary keyCREATE TABLE
salesman ; (SalesID c(6) PRIMARY KEY, ; SaleName C(20)) * Create a customer table and relate it to the salesman table.CREATE TABLE
customer ; (SalesID c(6), ; CustId i PRIMARY KEY, ; CustName c(20) UNIQUE, ; SalesBranch c(3), ;FOREIGN KEY
SalesId TAG SalesIdREFERENCES
salesman) * Create an orders table related to customer with its own primary * key and some business rules such as defaults & checks.CREATE TABLE
orders ; (OrderId i PRIMARY 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 relationships MODIFY DATABASE * Delete example files SET SAFETY OFF && To suppress verification message CLOSE DATABASES && Close database before deleting DELETE DATABASE mydata1 DELETETABLES