0

My system consist of React client app + ASP.NET Core API + SQL Server database. I have a db table called 'Titles':

Id (Primary Key, nvarchar, not null) Name (nvarchar, not null)

When I create a new title entity, I use a random Guid as Id. However, I want the client user to be able to use a unique (but mutable), friendly name to reach the page, something like: http://www.mywebsite.com/titles/friendly-title-name instead of http://www.mywebsite.com/titles/31b18dba-4070-4b13-bab5-2fc57de9e3a9

Is it a good practice to use the 'name' column for this purpose? So, apart from the unique, immutable, primary key 'Id', I would have unique, mutable column 'name' to identify an entity.

Handsome Greg
  • 170
  • 11
  • 1
    Yes. (more characters) – Paweł Dyl Oct 31 '18 at 12:28
  • 1
    In order to make sure that name conforms to the [rules](https://stackoverflow.com/q/4669692/11683), you will have to normalize it (i.e. replace fancy characters with less fancy characters), and add the hyphens. There will be no way to convert the normalized form back to the original (because there would be infinitely many way to do so), so it's the normalized name that you will need the client to provide and store in the database. If you're happy with that and it still keeps your names unique, then why not. – GSerg Oct 31 '18 at 12:28
  • 1
    If `Name` must be unique, why not make it the primary key and dump the GUID? If you want an immutable composite key, then just use and `INT IDENTITY(1,1)` as a surrogate key. GUID's don't perform well and unless you need it, for legitimate global uniqueness, then you could get rid of it here. – S3S Oct 31 '18 at 12:53
  • @scsimon the issue here is that I want the 'name' to be mutable. – Handsome Greg Oct 31 '18 at 13:26
  • Right, so what's preventing you from doing that? Having it mutable doesn't remove the unique constraint. – S3S Oct 31 '18 at 13:55
  • @scsimon I am going to do that now, based on your and others answers, I was not sure if this is the best approach :) – Handsome Greg Oct 31 '18 at 14:09
  • And if you are deadset on using a guid (rarely a good idea) at least use the correct datatype (uniqueidentifier) instead of nvarchar. I would argue that if the name is mutable it is not in the running as a primary key. I think you need a surrogate key. Why not use identity and save yourself lots of anguish? – Sean Lange Oct 31 '18 at 14:35
  • I have extended the Asp.Net Identity database and Microsoft uses nvarchar(450) for Id (PK) column, so I thought it's a good idea to follow this approach (I am not a backend dev). The MS Identity tables are AspNetUsers, AspNetRoles etc. They all use nvarchar(450) and a String as a backing type in Entity Framework – Handsome Greg Oct 31 '18 at 14:44

0 Answers0