About primary key in the table? RRS feed

  • Question

  • I would like to have some explaination about the characteristics of a primary key in the table of such database.This is for vb 2008 express edition,since im new to this language,if this is true, as far as i understand about the characteristic in setting the primary key on each field for true.My question is if you are doing an updates/edit records in your table using the DataContext,if you setup the primary key for true of one of your field in the table it would edit all records in one datarow but if you put the primary key for true in all fields except one of them,all the records in the data column of that field which primary key is false could be edited.Basically its impossible to edit all records in the datarow and all the records in the datacolumn of the table in such one event.

    Is there any further explaination about the characteristics of primary key in the table?

    Saturday, October 24, 2009 6:45 AM


  • Mitkram,

    take a look at this http://msdn.microsoft.com/en-us/library/aa933112(SQL.80).aspx

    typically primary keys are used to keep integrity of the data by having unique identifiers for each record in a table.  a column or combination of columns which sets each record apart from the others.  in the link above it shows an example of how to use 2 columns where the combination of the data in both columns for one record will be unique.  no other record in the table will have the same combination of both column values together.

    col1             col2
    jeff              spacek

    no other record in the database can have that combination or jeff in col1 and spacek in col2.

    now as far as i understand it, you can have the first column set as unique but the second column can have duplicates.  you just can have a duplicate of the combination of the values in the 2 columns.  in other words, you could not have another record with jeff in col1 but you could have spacek in another record in co2.  and i imagine it could be set the other way as well.  depends on the primary key constraints you have set.

    setting all columns as primary key may have some issues for you if you have data that will be similar in several records.  but specifically for editing records, setting a primary key won't necessarily prevent you from editing the values.  an identity column will control whether or not you can edit the values in it.

    typically i use a single primary key column as a unique identifier set to identity with an auto increment for each record and i use that single columns values as foregn key ids in related tables.  i may not use an auto identity column if in a table where i identify records based on an email address.  for example on my website, for my member information table i identify each record using an email address which should be unique to all members.  this is a primary key column which is unique.  all the other columns in the record could match if needed.

    well hope this helps clear some things up for you.  i don't know what the rest of your knowledge about database is as far as working with the data and relationships but you might find my tutorial helpful.  it is there if you want to take a look.
    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial
    Saturday, October 24, 2009 1:56 PM