Results 1 to 6 of 6

Thread: Set Primary Key constraint while creating table through code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    135

    Set Primary Key constraint while creating table through code

    I am creating a table in access database (.mdb) through VB
    But I am not able to set the primary key constraint through code.

    I searched on the forum regarding this...but no luck.

    Here's the code I am using to create the table

    Code:
    Dim cn as New ADODB.Connection
    
    cn.Open = "DSN=MyDSN"
    
    sql = "create table Master(Id Number, Name Text)
    cn.Execute sql
    With the above code I am creating a table. But I want to set Id as my primary key.

    How do I do this?

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Set Primary Key constraint while creating table through code

    Does this help?
    Tengo mas preguntas que contestas

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Set Primary Key constraint while creating table through code

    Here is some full syntax on CREATE TABLE

    Code:
    CREATE TABLE Event_T
    (Bldg		int		NOT NULL
    ,Yr		int		NOT NULL
    ,ApptType	int		NOT NULL
    ,StartTime	varchar(4)	NOT NULL
    ,EndTime	varchar(4)	NOT NULL
    ,Teacher	int		NOT NULL
    ,Room		varchar(10)	NOT NULL
    ,ApptNotes	varchar(50)	NOT NULL
    ,TDate		datetime	NULL
    ,constraint	    PKEvent
    		    PRIMARY KEY CLUSTERED (Bldg, Yr, ApptType, StartTime)
    )
    GO
    
    ALTER TABLE Event_T
    	ADD CONSTRAINT FKApptTypeEvent
    	FOREIGN KEY (ApptType) REFERENCES ApptType_T(ApptType)
    GO
    And altering for a foreign key as well.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    135

    Re: Set Primary Key constraint while creating table through code

    Ok thanks, will give it a try...
    But will this work for MS Access.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Set Primary Key constraint while creating table through code

    The syntax might be slightly different - but the HELP FILE for ACCESS should tell you all that.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Set Primary Key constraint while creating table through code

    mcnn is the ADODB connection for access database

    mcnn.execute "create table temp (id varchar(10) primary key)"

    or

    mcnn.execute "create table temp (id autoincrement primary key)"
    Rahul Yadav

    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

Posting Permissions

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



Click Here to Expand Forum to Full Width