이상한 문제를 발견 한 SQL Server 2016 데이터베이스가 있거나 아마도 누락되었습니다.NULL 레코드가있는 이상한 기본 키 문제 SQL Server
varchar 열에 기본 키가 아닌 테이블이있는 경우 N "값의 INSERT를 입력하면 NULL 값이 허용됩니다. where as as that column에 NULL을 직접 사용하면 오류가 발생하여 NULL을 삽입 할 수 없습니다.
N ''은 기본 키 열에 대해 유효한 문자로 간주됩니까?
다음은 문제를 재현하는 TSQL입니다.
CREATE TABLE [dbo].[testprmarykeynull](
[EmailId] [varchar](20) NOT NULL PRIMARY KEY CLUSTERED
(
[EmailId] ASC
)
)
INSERT INTO testprmarykeynull VALUES('[email protected]');
INSERT INTO testprmarykeynull VALUES(N''); - Successfully insert this for the first time and the second time throws duplicate error. But from my understanding this should fail the first time itself since it's a NULL value.
INSERT INTO testprmarykeynull VALUES(NULL); - Fails though even if I try with the first NULL
은'당신이'NULL' 언급하는 두 번째 one' 맞죠? 기본 키가 아닌 null 제약 조건을 위반해야합니다. – Squirrel
@Squirrel no - 빈 문자열 ('' '')을 두 번 삽입하려고했습니다. 처음으로 성공했을 때 ** 빈 문자열 **을 다시 삽입하려고하면 고유성을 위반하기 때문에 실패하게됩니다. 설명을 주신 덕분에 – Mureinik
주셔서 감사합니다. – Squirrel