What data type is recommended for ID columns?

后端 未结 9 1656
既然无缘
既然无缘 2021-02-08 04:35

I realize this question is very likely to have been asked before, but I\'ve searched around a little among questions on StackOverflow, and I didn\'t really find an answer to

相关标签:
9条回答
  • 2021-02-08 05:23

    I asked a similar question which has a few answers that might help. Replication seems to be the biggest advantage of using GUIDs.

    Reasons not to use an auto-incrementing number for a primary key

    0 讨论(0)
  • 2021-02-08 05:26

    If the database is distributed, where you could get records from other databases, the primary key needs to be unique within a table across all the databases. GUID solves this issue, albeit at the cost of space. A combination of autoincrement and namespace would be a good tradeoff.

    It would be nice if databases could provide inbuild support for autoincrements with "prefixes". So in one database, I get IDs like X1,X2,X3 ... and so on whereas in the other database it could be Y1,Y2,Y3 ... and so on.

    0 讨论(0)
  • 2021-02-08 05:27

    I never liked integers and incremented identifiers. It makes a problem when you want to copy data across different tables (two tables same ID) or across different databases. Guid is big as a string representative and it also comes to problem when you include ids into your web application urls. So I decided to use a short string version of Guid which in the db is like varchar(16). See code bellow (method WebHash()):

    public static class IdentifyGenerator
    {
        private static object objLock = new object();
    
        private static char[] sybmols = {
                             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                             'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                             'u', 'v', 'w', 'x', 'y', 'z',
                         };
    
        /// <summary>
        /// Creates a new Unique Identity HashCode (length 16 chars)
        /// </summary>
        /// <returns></returns>
        public static string WebHash(Guid fromGuid = default(Guid))
        {
            lock (objLock)
                return RandomString(16, (fromGuid != default(Guid) ? fromGuid.ToByteArray() : null));
        }
    
        public static string RandomString(int length, byte[] customBytes = null)
        {
            Stack<byte> bytes = customBytes != null ? new Stack<byte>(customBytes) : new Stack<byte>();
            string output = string.Empty;
    
            for (int i = 0; i < length; i++)
            {
                if (bytes.Count == 0)
                    bytes = new Stack<byte>(Guid.NewGuid().ToByteArray());
                byte pop = bytes.Pop();
                output += sybmols[pop % sybmols.Length];
            }
            return output;
        }
    }
    

    The only disadvantage is when you create new rows in SQL. So you have to crate a similar sql function.

    Will be happy to receive any critic in my address.

    0 讨论(0)
提交回复
热议问题