Results 1 to 4 of 4

Thread: Creating Primary Keys using SQL

  1. #1
    Join Date
    Jul 2005
    Posts
    5

    Creating Primary Keys using SQL

    I would like to use SQL to create a table that contains two fields that would be used as the primary key (not allowing duplicates). Is this possible and if so please provide an example. Thanks

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    create table test (a number primary key)

  3. #3
    Join Date
    Jul 2005
    Location
    Manchester, UK
    Posts
    7
    Yes, this is possible. What you need to do is create a multi-field primary key - also known as a composite key. In MS Access, you would do this by highlighting the fields that you wish to make up the composite and clicking the 'primary key' icon on the toolbar.

    In SQL Enterprise Manager you would do something similar.

    Search out the 'composite key' help topic in both Access help and SQL Server books online for more exhaustive information.

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    I didn't realize you wanted multiple column primary key. You can run this in query windows

    alter table test add constraint pk_test primary key (a,b)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •