Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Varchar(max) column not allowed to be a Primary Key in SQL Server

Tags:

sql

sql-server

Varchar(max) column not allowed to be a Primary Key in SQL Server.

What is the max length of a varchar type that can be made a primary key.

This might be a bad practice, but came across the situation.

like image 480
singsuyash Avatar asked May 11 '12 17:05

singsuyash


People also ask

Can you make a varchar Max column as primary key in SQL?

Varchar(max) column not allowed to be a Primary Key in SQL Server.

Can varchar can 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.

Can Max be used on varchar?

VARCHAR(MAX) is different from VARCHAR because it supports character strings up to 2 GB (2,147,483,647 bytes) in length. You should consider using VARCHAR(MAX) only when each string stored in this data type varies considerably in length, and a value might exceed 8000 bytes in size.

Is varchar max 255?

VARCHAR(255) stores 255 characters, which may be more than 255 bytes.


2 Answers

900 bytes is the max length for a primary key, this is a bad idea in my opinion, why don't you use a surrogate key instead?

like image 154
SQLMenace Avatar answered Oct 29 '22 01:10

SQLMenace


This question has already been answered in this thread; the short answer is that an index field cannot exceed 900 bytes. That said, it's generally a bad idea to use a long VARCHAR field as an index for myriad reasons such as efficiency and maintainability.

like image 28
Jgreenst Avatar answered Oct 29 '22 03:10

Jgreenst