Contents
Should varchar be primary key?
It is perfectly acceptable to use a varchar column as the primary key. This is often the case when one uses a natural key that doesn’t happen to be an integer. Keep in mind that even if you introduce a surrogate as the pimary key, you’ll still need to create a unique constraint on product_id.
Can I use string as a primary key?
The short answer : It’s perfectly fine to use a string as a primary key. What is a good primary key candidate ? It should be unique. It should rarely, if at all, change.
Can foreign key be varchar?
I would say it is fine to use VARCHAR as both PRIMARY and FOREIGN KEYS. Only issue I could forsee is if you have a table, lets say Instruments (share instruments) and you create the PRIMARY/FOREIGN KEY as VARCHAR, and it happens that the CODE changes.
Can SQL text primary key?
You can not set primary key to text and ntext columns. But you can achiveve the same functionality by setting datatype as a varchar(8000). The difference is this column can now contain at most 8000 chars. Ideally you should not use this datatype column as a primary key.
Is it OK to use varchar as a primary key?
The answer to a question of whether or not you should do it is different, though: in most situations, values that have a meaning outside your database system should not be chosen to be a primary key.
Which is slower an int or A varchar?
If you are going to be joining to other tables, a varchar, particularly a wide varchar, can be slower than an int. Additionally if you have many child records and the varchar is something subject to change, cascade updates can causes blocking and delays for all users. A varchar like a car VIN number that will rarely if ever change is fine.
Is it safe to use A varchar for PKS?
A varchar like a car VIN number that will rarely if ever change is fine. A varchar like a name that will change can be a nightmare waiting to happen. PKs should be stable if at all possible.
When to use a surrogate key for varchar?
If you do decide to use a surrogate key, then make a unique index for the varchar field. This gets you the benefits of the faster joins and fewer records to update if something changes but maintains the uniquess that you want.