Dipendra543
Dipendra543

Reputation: 3

Why use Unique when Primary key on same key is already defined

While reading Database Systems, I have encountered through a query as shown below:

 CREATE TABLE works (
    eid       INTEGER NOT NULL,
    did       INTEGER NOT NULL,
    pcttime   INTEGER,
    PRIMARY KEY ( eid,
    did ),
    UNIQUE ( eid ),
    FOREIGN KEY ( did )
        REFERENCES dept
);

Since primary key on eid,did is already defined, why would we require unique on eid? Because each eid,did pair is already unique.

Upvotes: 0

Views: 83

Answers (1)

Chris Mack
Chris Mack

Reputation: 5208

The primary key is on two columns, so you could still have non-unique eids. E.g.,

eid   did
1     1
1     2

This doesn't violate your primary key, but would violate your unique constraint.

Upvotes: 4

Related Questions