locked
The best way to create a primary key? RRS feed

  • Question

  •  

    Hi ,

    I have an application in C#.

    I want to fill a table with data.

    However the primary key should be autogenerated and filled into the table.

    What is the best way of doing this ?

     

    Please explain..

     

     

    Regards

    Basim

    Thursday, December 13, 2007 6:28 AM

Answers

  •  

    If you are using Database:

    in most databases you can add auto number, and set it increament value

    so i think that the best way is to let the database do that for you and when inserting ignore that auto generated number.

     

    if  you are not using Database:

    you can store the last id number somewhere , and to generate a new id read that number and increase it value, and update it value to be the last generated ID

     

    Thursday, December 13, 2007 9:36 AM
  • if you use Access Database chose the column type: AutoNumber

    and it is better to make this column primary key, by right clicking on it.

     

    now when inserting rows from VB or C# or any other language ignore the primary key column:

     

    for example the primary key is col1 and you have fore text colmn(col2,col3,col4):

     

    use this SQL Statement:

     

    insert into tabl1 (col2,col3,col4) values('dfdf','dfdf','df')'

     

    Saturday, December 15, 2007 5:53 AM

All replies

  • It depends on what kind of "table" you are using... If you're talking about some sql dbms product it will likely have something that generates unique numbers.. (eg: autonumber, auto_increment, sequence are a couple of names for such a feature in sql server, mysql and oracle)
    Thursday, December 13, 2007 6:59 AM
  •  

    If you are using Database:

    in most databases you can add auto number, and set it increament value

    so i think that the best way is to let the database do that for you and when inserting ignore that auto generated number.

     

    if  you are not using Database:

    you can store the last id number somewhere , and to generate a new id read that number and increase it value, and update it value to be the last generated ID

     

    Thursday, December 13, 2007 9:36 AM
  •  

    Dear Fadi,

     

    Adding an autonumber to the database looks a good way.

    Can you explain it a little.

     

    Also i have a concern whether i will be able to do the same with both SQL and Access databases

    .

     

    Regards

    Basim

    Saturday, December 15, 2007 3:45 AM
  • if you use Access Database chose the column type: AutoNumber

    and it is better to make this column primary key, by right clicking on it.

     

    now when inserting rows from VB or C# or any other language ignore the primary key column:

     

    for example the primary key is col1 and you have fore text colmn(col2,col3,col4):

     

    use this SQL Statement:

     

    insert into tabl1 (col2,col3,col4) values('dfdf','dfdf','df')'

     

    Saturday, December 15, 2007 5:53 AM