How to Alter a Primary Key

Knowing how to alter a primary key can save you time when working with a table that needs a primary key removed. Microsoft Office Access is a relational database management system often used to design local databases. A primary key is used as a unique identifier for each record in a database table. Visual Basic for Applications, also known as VBA, is a computer programming language used to automate tasks otherwise done manually in Microsoft Office applications.

Things You'll Need

  • Microsoft Office Access
Show More

Instructions

    • 1

      Start Microsoft Office Access and select "Blank Database," then click "Create." Select the "Database Tools" menu, then click "Visual Basic." Select the "Insert" menu, then click "Module."

    • 2

      Type the following to create a new subroutine:

      Private Sub alterPrimaryKey()

      Press "Enter."

    • 3

      Type the following to create a new String variable:

      Dim SQLstr As String

    • 4

      Type the following to create a new table with two columns, using "ID_Field" as the primary key and "Column1" as a text column:

      SQLstr = "CREATE TABLE myTbl "

      SQLstr = SQLstr & "(ID_Field INTEGER CONSTRAINT PK_ID_Field PRIMARY KEY, "

      SQLstr = SQLstr & "Column1 TEXT(25)) "

      DoCmd.RunSQL (SQLstr)

    • 5

      Type the following to remove the primary key setting from the "ID_Field":

      SQLstr = "ALTER TABLE myTbl "

      SQLstr = SQLstr & "DROP CONSTRAINT PK_ID_Field;"

      DoCmd.RunSQL (SQLstr)

    • 6

      Press "F5" to run the subroutine.

Related Searches:

References

Comments

You May Also Like

  • How to Check Registry Entries

    The Windows Registry contains a comprehensive hierarchical database of configuration data. The configuration data applies to various aspects of the operating system...

  • What Is a Microsoft Access Primary Key?

    Microsoft Access is part of the Microsoft Office productivity suite. Access is one of the few programs that allows a user to...

  • How to Remove Primary Key SQL

    Primary keys are an important part of a table design. When redesigning tables, there may come a time when a primary key...

  • The Importance of a Primary Key

    Modern relational database systems are used to enter, organize and report on all sorts of data. Before considering what sort of software...

  • How to Remove Primary Key from MySql

    Set up your "alter" statement. The basic statement is the "alter" keyword with the table name you want to change. The following...

  • How to Add Gridlines to an Excel Chart

    Increase the readability of your Microsoft Excel 2007 chart by adding gridlines. Gridlines can be added horizontally and vertically and be displayed...

  • Foreign Key MySQL Tutorial

    A foreign key is a column in one table that points to the primary key column of another table. Using a foreign...

  • How to Change Windows 7 Product Key

    Within 30 days of installing Windows 7 on your computer, you must supply a valid product key to activate the installation. If...

Related Ads

Featured