CREATE TABLE – SQL Command

command

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] [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
ParametersTableName1 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.
FieldTypenFieldWidth nPrecisionDescriptionCn–Character field of width nD––DateT––DateTimeNndNumeric field of width n with d decimal placesFndFloating numeric field of width n with d decimal placesI––IntegerB–dDouble Y––CurrencyL––LogicalM––MemoG––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 PRIMARY 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. PRIMARY KEY Creates a primary index for the field. The primary 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 Setting a Primary 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 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.
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 primary index key of the parent table. If the parent table does not have a primary 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)
PRIMARY KEY eExpression2 TAG TagName2 Specifies a primary index to create. eExpression2 specifies any field or combination of fields in the table. TAG TagName2 specifies the name for the primary index tag that is created. Index tag names can contain up to 10 characters.
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.
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 primary index with one of the PRIMARY KEY options, you cannot include the field that was specified for the primary 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-primary) 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 primary 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, 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.
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 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 SalesId REFERENCES 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 i REFERENCES 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

时间: 2024-09-17 04:42:18

CREATE TABLE – SQL Command的相关文章

Create Database using Create Database SQL Command

创建ORACLE数据库有几种比较常用的方法: 1. 图形界面下使用DBCA,可以选择使用模板或者自定义(自定义需要执行创建数据字典和存储过程的过程,而模板的话是使用DBMS包作恢复的安装.所以速度上差别比较大) 2. 命令行的DBCA与responseFile 3. Create Database SQL命令,然后执行创建数据字典和存储过程,所以速度会略微慢点. 据David说考OCM的话考的是CREATE DATABASE SQL建库的过程.一般情况下DBA可能为了图快使用的都是DBCA和模板

SQL CREATE TABLE 语句用法

SQL CREATE TABLE 语句用法 CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库教程中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据类型, 列名称3 数据类型, .... ) 数据类型(data_type)规定了列可容纳何种数据类型.下面的表格包含了SQL中最常用的数据类型: 数据类型 描述 integer(size) int(size) smallint(size) tinyin

sql: Oracle 11g create table, function,trigger, sequence

--书藉位置Place目录 drop table BookPlaceList; create table BookPlaceList ( BookPlaceID INT PRIMARY KEY, --NUMBER BookPlaceName nvarchar2(500) not null, BookPlaceCode varchar(100) null, --位置編碼 BookPlaceParent INT null --BookPlaceKindId nvarchar(500) null --

sql入门教程:sql 创建表create table 与数据库[create databse]

sql入门教程:sql 创建表create table 与数据库[create databse] 在创建数据库的声明 在CREATE DATABASE语句是用来建立一个数据库. 创建数据库的SQL语法 CREATE DATABASE database_name 创建数据库的例子现在,我们要建立一个数据库,所谓的" my_db " . 我们使用下面的CREATE DATABASE语句:CREATE DATABASE my_db   下面来看看sql 创建表create table 在创建

sql CREATE TABLE 创建表语句

CREATE TABLE语句的基本语法是:  代码如下 复制代码 CREATE TABLE table_name ( column1 datatype null/not null,   column2 datatype null/not null,   ... ); 每列必须有一个数据类型.列应被定义为"null"或"not null",如果这个值是空,默认的"null". 实例  代码如下 复制代码 CREATE TABLE suppliers

ERROR 1005 (HY000): Can't create table 'zcxt.tb_asset' (errno: 150)解决

环境 MySQL 5.1 + 命令行工具 问题 建表出现如下错误: ERROR 1005 (HY000): Can't create table 'zcxt.tb_asset' (errno: 150) 解决 1. 是否存 在SQL语法错误: 2. 外键引用的字段是否存在.

MySQL Meta 信息与 CREATE TABLE 的对应关系

前情提要 下面四篇文章是数月之前对length的理解 MySQL Meta中的length字段 -- (1) 初始值的length计算 MySQL Meta中的length字段 -- (2) length的推导MySQL Meta中的length字段 -- (3) length的推导举例MySQL Meta中的length字段 -- (4) 玩儿MySQL代码 温故而知新,几个月后,对 length 等 meta 信息的理解又进一步,本文再记之. 混乱的MySQL定义 用"混乱"这个词

当心 CREATE TABLE AS

    对 DBA 而言,CREATE TABLE AS 可谓是家常便饭,顺手拈来.需不知该方式虽然简单,但疏忽也容易导致意想不到的问题.笔者前阵子就碰上了这样的事情.由于是对原表进行克隆,且数据存储在不同的表空间,因此毫不犹豫地使用了CREATE TABLE AS,结果在运行package时,error... --1.非空约束遗失 -->使用create table as 来创建对象 scott@CNMMBO> create table tb_dept as select * from de

13.1.17 CREATE TABLE Syntax

13.1.17 CREATE TABLE Syntax   13.1.17.1 CREATE TABLE ... LIKE Syntax 13.1.17.2 CREATE TABLE ... SELECT Syntax 13.1.17.3 Using FOREIGN KEY Constraints 13.1.17.4 Silent Column Specification Changes CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (cre