dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Is a good idea using a string field as PK instead of an int in this case?

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Resolved [RESOLVED] Is a good idea using a string field as PK instead of an int in this case?

    Hi to all,

    I'm creating the database of a proyect and an idea has come to my mind: why using an additional int field if the only important field that is string must be unique??

    I've read opinions in both senses, some says it's good, some is wrong, but since my case is different to all I've read I want to ask here:

    I've a companyes table, but every company can be called by several names, I mean, user will define the name of the company, but also alternative names for it (alternative = 1 to n)

    So, I created the Company table and the CompanyNames:

    public class CompanyNames
    {
    public CompanyNames() { }

    public int CompanyNamesId { get; set; }
    [Required]
    [MaxLength(100)]
    public string Name { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }

    //Here I define the foreign key, so I can know what company does this name belongs
    public CompanyId { get; set; }
    public int CompanyId { get; set; }

    }
    In this way, when I have an string that can be a companyname I look for this name in the CompanyNames table, if found, I get the CompanyID so I can access to the company data that is on Company table.

    My idea, since the field Name must be unique (it has no sense havind duplicated names), is doing something like this:

    public class CompanyNames
    {
    public CompanyNames() { }

    [MaxLength(100)]
    public string CompanyNamesId { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }

    //Here I define the foreign key, so I can know what company does this name belongs
    public CompanyId { get; set; }
    public int CompanyId { get; set; }

    }
    In this way, when I look for a Company Name in the table Company Names i do it throroght the primary key, not another field.

    Is this a good or bad practise?

    Regards and many thanks
    Last edited by TassadarNET; May 19th, 2019 at 01:20 PM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width