문제

Im wondering if on a relational table I set the two values below as a PRIMARY KEY if that automatically makes the table know that all entries should be unique....

CREATE TABLE UserHasSecurity
(
    userID int REFERENCES Users(userID) NOT NULL,
    securityID int REFERENCES Security(securityID) NOT NULL,
     PRIMARY KEY(userID,securityID)
)

or do I need to be more explicit like this...

CREATE TABLE UserHasSecurity
(
    userID int REFERENCES Users(userID) NOT NULL,
    securityID int REFERENCES Security(securityID) NOT NULL,
     PRIMARY KEY(userID,securityID),
     UNIQUE(userID,securityID)
)
도움이 되었습니까?

해결책

You don't need UNIQUE here. PRIMARY KEY will make sure there is no duplicate (userID,securityID) pairs.

다른 팁

No, you don't need to specify UNIQUE in addition to PRIMARY KEY. A primary key by definition must be unique.

A PRIMARY KEY has to be unique, so you only need to declare as a primary key. The underlying index is unique by definition.

Creating Unique Indexes

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top