ALTER TABLE 语句 (Microsoft Access SQL)

适用于:Access 2013、Office 2013

修改用 CREATE TABLE 语句创建的表的设计。

注意

[!注释] Microsoft Access 数据库引擎不支持使用 ALTER TABLE 语句或任何数据定义语言 (DDL) 语句。 请改为使用 DAO Create 方法。

语法

ALTER TABLE {ADD {COLUMN field type[ (size) ] [NOT NULL] [CONSTRAINT index] |ALTER COLUMN 字段类型[ (大小) ] |CONSTRAINT multifieldindex} |DROP {COLUMN field I CONSTRAINT indexname} }

ALTER TABLE 语句包含以下部分:

部分

说明

table

要更改的表名称。

field

要添加到表或从 中删除的字段的名称。 或者,表中要更改的字段的名称。

type

字段的数据类型。

size

以字符数为单位的字段大小(仅限于“文本”字段和“二进制”字段)。

index

字段的索引。 有关如何构造此索引的详细信息,请参阅 CONSTRAINT 子句

multifieldindex

要添加到中的多字段索引的定义。 有关如何构造此索引的详细信息,请参阅 CONSTRAINT 子句

indexname

要删除的多字段索引的名称。

说明

使用 ALTER TABLE 语句能够以多种方式更改现有表。 您可以进行以下操作:

  • 使用 ADD COLUMN 向表中添加新字段。 可以指定字段名称、数据类型和可选大小(对于文本和二进制字段)。 例如,以下语句将具有 25 个字符的名为 Notes 的文本字段添加到 Employees 表中:

      ALTER TABLE Employees ADD COLUMN Notes TEXT(25)
    

    也可以在该字段上定义索引。 有关单字段索引的详细信息,请参阅 CONSTRAINT 子句

    如果为字段指定 NOT NULL,则需要新记录才能在该字段中包含有效数据。

  • 使用 ALTER COLUMN 可更改现有字段的数据类型。 可以指定文本和二进制字段的字段名称、新的数据类型以及可选大小。 例如,以下语句将 Employees 表中名为 ZipCode 字段的数据类型(原先定义为整型)更改为具有 10 个字符的文本字段:

      ALTER TABLE Employees ALTER COLUMN ZipCode TEXT(10)
    
  • 使用 ADD CONSTRAINT 添加多字段索引。 有关多字段索引的详细信息,请参阅 CONSTRAINT 子句

  • 使用 DROP COLUMN 可删除字段。 只需指定字段的名称。

  • 使用 DROP CONSTRAINT 可删除多字段索引。 只需在 CONSTRAINT 保留字后面指定索引名称。

注意

  • 无法一次添加或删除多个字段或索引。
  • 可以使用 CREATE INDEX 语句向表中添加单字段或多字段的索引,并且可以使用 ALTER TABLE 或者 DROP 语句删除使用 ALTER TABLE 或 CREATE INDEX 创建的索引。
  • 可以对单个字段或在应用于单个字段或多字段的命名 CONSTRAINT 的命名 CONSTRAINT 子句中使用 NOT NULL。 但是,仅可以对字段应用一次 NOT NULL 限制。 尝试多次应用此限制将造成运行时错误。

示例

以下示例向 Employees 表中添加数据类型为 Money 的 Salary 字段。

    Sub AlterTableX1() 
     
        Dim dbs As Database 
     
        ' Modify this line to include the path to Northwind 
        ' on your computer. 
        Set dbs = OpenDatabase("Northwind.mdb") 
     
        ' Add the Salary field to the Employees table  
        ' and make it a Money data type. 
        dbs.Execute "ALTER TABLE Employees " _ 
            & "ADD COLUMN Salary MONEY;" 
     
        dbs.Close 
     
    End Sub 

以下示例将 Salary 字段的数据类型从 Money 改为 Char

    Sub AlterTableX2() 
     
        Dim dbs As Database 
     
        ' Modify this line to include the path to Northwind 
        ' on your computer. 
        Set dbs = OpenDatabase("Northwind.mdb") 
     
        ' Modify the existing Salary field of the Employees table  
        ' by changing it to a CHAR data type. 
        dbs.Execute "ALTER TABLE Employees " _ 
            & "ALTER COLUMN Salary CHAR(20);" 
     
        dbs.Close 
     
    End Sub 

本例从 Employees 表中删除 Salary 字段。

    Sub AlterTableX3() 
     
        Dim dbs As Database 
     
        ' Modify this line to include the path to Northwind 
        ' on your computer. 
        Set dbs = OpenDatabase("Northwind.mdb") 
     
        ' Delete the Salary field from the Employees table. 
        dbs.Execute "ALTER TABLE Employees " _ 
            & "DROP COLUMN Salary;" 
     
        dbs.Close 
     
    End Sub

本例向 Orders 表中添加一个外键。 该外键基于 Employees 表的 EmployeeID 字段,并引用该字段。 在本例的 REFERENCES 子句中,您不必在 Employees 表后列出 EmployeeID 字段,因为 EmployeeID 是 Employees 表的主键。

    Sub AlterTableX4() 
     
        Dim dbs As Database 
     
        ' Modify this line to include the path to Northwind 
        ' on your computer. 
        Set dbs = OpenDatabase("Northwind.mdb") 
     
        ' Add a foreign key to the Orders table. 
        dbs.Execute "ALTER TABLE Orders " _ 
            & "ADD CONSTRAINT OrdersRelationship " _ 
            & "FOREIGN KEY (EmployeeID) " _ 
            & "REFERENCES Employees (EmployeeID);" 
     
        dbs.Close 
     
    End Sub 

本例从 Orders 表中删除外键。

    Sub AlterTableX5() 
     
        Dim dbs As Database 
     
        ' Modify this line to include the path to Northwind 
        ' on your computer. 
        Set dbs = OpenDatabase("Northwind.mdb") 
     
        ' Remove the OrdersRelationship foreign key from 
        ' the Orders table. 
        dbs.Execute "ALTER TABLE Orders " _ 
            & "DROP CONSTRAINT OrdersRelationship;" 
     
        dbs.Close 
     
    End Sub