I have designed my database and I use varchar to make unique name. I made a table products and each product has a product code, and this code is unique, it cannot be repeated and mostly this is my primary key in parent table. The idea is, for example I have a candle, and candles have different colors so a different product, I am able to make product name distinct by its product code. I didn't use or name a table as an indexable id which is of type int and that it auto_increments when new item is inserted. I just used the unique code as an indexer to my other tables on foreign keys.
I do not know if this is of great idea, I am very new to PHP MySQL and I got some problem when it comes to inserting values into different tables so I use the product code to link them both. Now I just knew about mysqli_insert_id stuff which could have solve my problem a long time ago.
So can I just use a non-standard row id of string than row id of type int???
You are doing the right thing - identity field should be numeric and not string based, both for space saving and for performance reasons (matching keys on strings is slower than matching on integers).
There's nothing wrong with using a CHAR or VARCHAR as a primary key. Sure it'll take up a little more space than an INT in many cases, but there are many cases where it is the most logical choice and may even reduce the number of columns you need, improving efficiency, by avoiding the need to have a separate ID field.
ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers. Identifiers may begin with a digit but unless quoted may not consist solely of digits. Database, table, and column names cannot end with space characters.
Personally I would say to use an auto inc ID. Using your product code as an ID / primary key means you can't (or will be harder to) change the product code later on down the line which may cause problems.
To elaborate:
If you end up expanding your database so that your products are linked to various tables, using a product code as your ID means that if you want to change your product code you will have to update each entry in ALL tables. Using an ID that isn't the product code means you just have to update the code in one place (stock table) and the ID's will do the rest as you join the stock table to get the product code.
Big disadvantage of string ids is that it make indexes (much) bigger, which could affect performance.
I would keep it simple and go with standard auto increment int.
I also agree with @hd1 and @webnoob answers -- row id should be constant and independent from value which it describes -- classic wrong (in my opinion) row id is American SSN or any other unique number assigned by government to man or company.
Any unique [and ideally non-changing] value, group of values can be used as a Key/Primary Key in a database. The main reason that surrogate keys [eg: autoincremented INT columns] are so common in mySQL is because:
name
column with length of 5 collides on names Tom Cruise
and Tom Clancy
because the key is Tom C
.The pros and cons of natural vs surrogate keys will continue to be argued about for as long as we have relational databases. I say: do what you like, but if you are going to use a surrogate, try to make sure that, where possible, there is also a way of uniquely identifying rows by means of a natural key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With