CREATE TABLE Statement, CONSTRAINT Clause Example (DAO)

Microsoft Jet SQL Reference

CREATE TABLE Statement, CONSTRAINT Clause Example

This example creates a new table called ThisTable with two text fields.

Sub CreateTableX1()

    Dim dbs As Database

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

' Create a table with two text fields.

    dbs.Execute "CREATE TABLE ThisTable " _

        & "(FirstName CHAR, LastName CHAR);"

    dbs.Close

End Sub

This example creates a new table called MyTable with two text fields, a Date/Time field, and a unique index made up of all three fields.

Sub CreateTableX2()

    Dim dbs As Database

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    ' Create a table with three fields and a unique

    ' index made up of all three fields.

    dbs.Execute "CREATE TABLE MyTable " _

        & "(FirstName CHAR, LastName CHAR, " _

        & "DateOfBirth DATETIME, " _

        & "CONSTRAINT MyTableConstraint UNIQUE " _

        & "(FirstName, LastName, DateOfBirth));"

    dbs.Close

End Sub

This example creates a new table with two text fields and an Integer field. The SSN field is the primary key.

Sub CreateTableX3()

     Dim dbs As Database

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    ' Create a table with three fields and a primary

    ' key.

    dbs.Execute "CREATE TABLE NewTable " _

        & "(FirstName CHAR, LastName CHAR, " _

        & "SSN INTEGER CONSTRAINT MyFieldConstraint " _

        & "PRIMARY KEY);"

    dbs.Close

End Sub