Does the concept of candidate key exist only in theory?What happens to the index of a primary key after a...
What did I blunder at move 23?
Why don't you get burned by the wood benches in a sauna?
What's the function of the word "ли" in the following contexts?
How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?
How does holding onto an active but un-used credit card affect your ability to get a loan?
Is the percentage symbol a constant?
How can I handle players killing my NPC outside of combat?
Coworker is trying to get me to sign his petition to run for office. How to decline politely?
Is there a way to pause a running process on Linux systems and resume later?
Why are 'and/or' operations in this Python statement behaving unexpectedly?
Why do single electrical receptacles exist?
I am a loser when it comes to jobs, what possibilities do I have?
How can changes in personality/values of a person who turned into a vampire be explained?
Why write a book when there's a movie in my head?
Short story about a man betting a group he could tell a story, and one of them would disappear and the others would not notice
Why does this quiz question say that protons and electrons do not combine to form neutrons?
Was the Soviet N1 really capable of sending 9.6 GB/s of telemetry?
Is it possible to detect 100% of SQLi with a simple regex?
Why don't programs completely uninstall (remove all their files) when I remove them?
Have any astronauts or cosmonauts died in space?
Cryptic cross... with words
How do I avoid the "chosen hero" feeling?
Exploding Numbers
How can I persuade an unwilling soul to become willing?
Does the concept of candidate key exist only in theory?
What happens to the index of a primary key after a DROP CONSTRAINT?PRIMARY KEY CONSTRAINT fundamentalsCandidate key = Key = Minimal Superkey?Is matriculation number a good primary key?Why would a table use its primary key as a foreign key to itselfNVARCHAR column as PRIMARY KEY or as UNIQUE columnDB2 10.5 - When inserting, the database is trying to reuse index keys on GENERATE ALWAYSSQL Server equivalent of Oracle USING INDEX clauseMS Access ODBC Prepared statement needs to be re-prepared (#1615)When should a primary key be meaningful?
I know the concept of candidate key in RDBMS theory, but do candidate keys really exist in actual SQL engines? I mean is there any way to designate a particular column or set of columns as a candidate key in any of the SQL database management systems, say SQL Server, Postgres, MySQL, Oracle etc.?
Is there any reserved keyword for designating column(s) as a candidate key like PRIMARY KEY
or UNIQUE
in case of primary key column and unique column?
I feel UNIQUE
constraint itself provides implementation of the candidate key concept. I don't see any practical value of having a separate CANDIDATE KEY
keyword. Is it so?
primary-key relational-theory rdbms unique-constraint candidate-key
|
show 5 more comments
I know the concept of candidate key in RDBMS theory, but do candidate keys really exist in actual SQL engines? I mean is there any way to designate a particular column or set of columns as a candidate key in any of the SQL database management systems, say SQL Server, Postgres, MySQL, Oracle etc.?
Is there any reserved keyword for designating column(s) as a candidate key like PRIMARY KEY
or UNIQUE
in case of primary key column and unique column?
I feel UNIQUE
constraint itself provides implementation of the candidate key concept. I don't see any practical value of having a separate CANDIDATE KEY
keyword. Is it so?
primary-key relational-theory rdbms unique-constraint candidate-key
2
AFAIK: The wordCandidate key
has no meaning in any SQL DBMS. The set of UNIQUE constraints and the PRIMARY key is the CANDIDATE keys. I have never really understood why Primary keys where invented in the first place, i.e. why one of the CANDIDATE keys is considered special, and in need of a special name.
– Lennart
Jan 30 at 8:36
@Lennart I have never really understood why Primary key ... is considered special I personally understand the difference between a unique and a primary key this way. Unique key provides key expression uniqueness over current table data. Primary key provides key expression uniqueness over all table lifetime data. If a record with some unique combination was deleted, another record with that expression value can be inserted later. If a record with some primary key value was deleted, the creation of new record with that primary key value is at least wrong practice. And forbidden is better.
– Akina
Jan 30 at 8:44
those are good points. @Akina I dont know whether "...is at least wrong practice" is part of standard definition of primary key. I believe its more sort of recurring real world scenario or de facto practice.
– anir
Jan 30 at 8:49
1
@Lennart: the difference between a primary key and a unique key is that a primary key by definition does not allow NULL values, a unique key however might allow them.
– a_horse_with_no_name
Jan 30 at 9:59
2
@Kondybas: a table is never "ordered" by anything. Only the result of a query can be ordered. An neither the PK or a UK define any ordering to begin with. They only provide a way to uniquely identify a single row.
– a_horse_with_no_name
Jan 30 at 10:13
|
show 5 more comments
I know the concept of candidate key in RDBMS theory, but do candidate keys really exist in actual SQL engines? I mean is there any way to designate a particular column or set of columns as a candidate key in any of the SQL database management systems, say SQL Server, Postgres, MySQL, Oracle etc.?
Is there any reserved keyword for designating column(s) as a candidate key like PRIMARY KEY
or UNIQUE
in case of primary key column and unique column?
I feel UNIQUE
constraint itself provides implementation of the candidate key concept. I don't see any practical value of having a separate CANDIDATE KEY
keyword. Is it so?
primary-key relational-theory rdbms unique-constraint candidate-key
I know the concept of candidate key in RDBMS theory, but do candidate keys really exist in actual SQL engines? I mean is there any way to designate a particular column or set of columns as a candidate key in any of the SQL database management systems, say SQL Server, Postgres, MySQL, Oracle etc.?
Is there any reserved keyword for designating column(s) as a candidate key like PRIMARY KEY
or UNIQUE
in case of primary key column and unique column?
I feel UNIQUE
constraint itself provides implementation of the candidate key concept. I don't see any practical value of having a separate CANDIDATE KEY
keyword. Is it so?
primary-key relational-theory rdbms unique-constraint candidate-key
primary-key relational-theory rdbms unique-constraint candidate-key
edited 8 mins ago
MDCCL
6,81331745
6,81331745
asked Jan 30 at 8:18
aniranir
1394
1394
2
AFAIK: The wordCandidate key
has no meaning in any SQL DBMS. The set of UNIQUE constraints and the PRIMARY key is the CANDIDATE keys. I have never really understood why Primary keys where invented in the first place, i.e. why one of the CANDIDATE keys is considered special, and in need of a special name.
– Lennart
Jan 30 at 8:36
@Lennart I have never really understood why Primary key ... is considered special I personally understand the difference between a unique and a primary key this way. Unique key provides key expression uniqueness over current table data. Primary key provides key expression uniqueness over all table lifetime data. If a record with some unique combination was deleted, another record with that expression value can be inserted later. If a record with some primary key value was deleted, the creation of new record with that primary key value is at least wrong practice. And forbidden is better.
– Akina
Jan 30 at 8:44
those are good points. @Akina I dont know whether "...is at least wrong practice" is part of standard definition of primary key. I believe its more sort of recurring real world scenario or de facto practice.
– anir
Jan 30 at 8:49
1
@Lennart: the difference between a primary key and a unique key is that a primary key by definition does not allow NULL values, a unique key however might allow them.
– a_horse_with_no_name
Jan 30 at 9:59
2
@Kondybas: a table is never "ordered" by anything. Only the result of a query can be ordered. An neither the PK or a UK define any ordering to begin with. They only provide a way to uniquely identify a single row.
– a_horse_with_no_name
Jan 30 at 10:13
|
show 5 more comments
2
AFAIK: The wordCandidate key
has no meaning in any SQL DBMS. The set of UNIQUE constraints and the PRIMARY key is the CANDIDATE keys. I have never really understood why Primary keys where invented in the first place, i.e. why one of the CANDIDATE keys is considered special, and in need of a special name.
– Lennart
Jan 30 at 8:36
@Lennart I have never really understood why Primary key ... is considered special I personally understand the difference between a unique and a primary key this way. Unique key provides key expression uniqueness over current table data. Primary key provides key expression uniqueness over all table lifetime data. If a record with some unique combination was deleted, another record with that expression value can be inserted later. If a record with some primary key value was deleted, the creation of new record with that primary key value is at least wrong practice. And forbidden is better.
– Akina
Jan 30 at 8:44
those are good points. @Akina I dont know whether "...is at least wrong practice" is part of standard definition of primary key. I believe its more sort of recurring real world scenario or de facto practice.
– anir
Jan 30 at 8:49
1
@Lennart: the difference between a primary key and a unique key is that a primary key by definition does not allow NULL values, a unique key however might allow them.
– a_horse_with_no_name
Jan 30 at 9:59
2
@Kondybas: a table is never "ordered" by anything. Only the result of a query can be ordered. An neither the PK or a UK define any ordering to begin with. They only provide a way to uniquely identify a single row.
– a_horse_with_no_name
Jan 30 at 10:13
2
2
AFAIK: The word
Candidate key
has no meaning in any SQL DBMS. The set of UNIQUE constraints and the PRIMARY key is the CANDIDATE keys. I have never really understood why Primary keys where invented in the first place, i.e. why one of the CANDIDATE keys is considered special, and in need of a special name.– Lennart
Jan 30 at 8:36
AFAIK: The word
Candidate key
has no meaning in any SQL DBMS. The set of UNIQUE constraints and the PRIMARY key is the CANDIDATE keys. I have never really understood why Primary keys where invented in the first place, i.e. why one of the CANDIDATE keys is considered special, and in need of a special name.– Lennart
Jan 30 at 8:36
@Lennart I have never really understood why Primary key ... is considered special I personally understand the difference between a unique and a primary key this way. Unique key provides key expression uniqueness over current table data. Primary key provides key expression uniqueness over all table lifetime data. If a record with some unique combination was deleted, another record with that expression value can be inserted later. If a record with some primary key value was deleted, the creation of new record with that primary key value is at least wrong practice. And forbidden is better.
– Akina
Jan 30 at 8:44
@Lennart I have never really understood why Primary key ... is considered special I personally understand the difference between a unique and a primary key this way. Unique key provides key expression uniqueness over current table data. Primary key provides key expression uniqueness over all table lifetime data. If a record with some unique combination was deleted, another record with that expression value can be inserted later. If a record with some primary key value was deleted, the creation of new record with that primary key value is at least wrong practice. And forbidden is better.
– Akina
Jan 30 at 8:44
those are good points. @Akina I dont know whether "...is at least wrong practice" is part of standard definition of primary key. I believe its more sort of recurring real world scenario or de facto practice.
– anir
Jan 30 at 8:49
those are good points. @Akina I dont know whether "...is at least wrong practice" is part of standard definition of primary key. I believe its more sort of recurring real world scenario or de facto practice.
– anir
Jan 30 at 8:49
1
1
@Lennart: the difference between a primary key and a unique key is that a primary key by definition does not allow NULL values, a unique key however might allow them.
– a_horse_with_no_name
Jan 30 at 9:59
@Lennart: the difference between a primary key and a unique key is that a primary key by definition does not allow NULL values, a unique key however might allow them.
– a_horse_with_no_name
Jan 30 at 9:59
2
2
@Kondybas: a table is never "ordered" by anything. Only the result of a query can be ordered. An neither the PK or a UK define any ordering to begin with. They only provide a way to uniquely identify a single row.
– a_horse_with_no_name
Jan 30 at 10:13
@Kondybas: a table is never "ordered" by anything. Only the result of a query can be ordered. An neither the PK or a UK define any ordering to begin with. They only provide a way to uniquely identify a single row.
– a_horse_with_no_name
Jan 30 at 10:13
|
show 5 more comments
2 Answers
2
active
oldest
votes
As far as I know, no SQL database management system (DBMS) supplies the CANDIDATE KEY
keyword as such, but (as I consider that you are suggesting in the question) that does not mean that the notion (or the functionality) of candidate key cannot be configured in a SQL table.
How to represent a candidate key
For example, if
- there is no primary declared for the table under consideration, and
- a whole set of columns (i.e., one or more) that is configured with a UNIQUE constraint is also fixed with the corresponding NOT NULL constraint(s),
then the designer is, precisely, representing a candidate key.
For example, the following table shows three distinct candidate keys:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_CK1 UNIQUE (FooId), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK2 UNIQUE (Bar), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK3 UNIQUE (Baz, Qux, Corge) -- Multi-column CANDIDATE KEY
);
A candidate key set up as such is, as you know, susceptible of being the reference of one or more foreign key constraints.
It is worth to stress the fact that, since SQL and its dialects include the idea of NULL marks, a UNIQUE constraint alone is not sufficient to stand for a candidate key (as expounded in the DDL sample above). This point is particularly significant because the column(s) constrained as a candidate key cannot retain NULL marks, otherwise it could not be deemed a real candidate key (besides, a table enclosing NULL marks in any of its columns cannot be considered a relational table but, yes, that is a different subject).
How does this differ from the CANDIDATE KEY
keyword approach?
In this way, if the vendors/developers of a certain SQL DBMS wants to provide the CANDIDATE KEY
keyword, then this kind of constraint, apart from the evident uniqueness enforcement, must also ensure the rejection of NULL marks in the relevant column(s), factor that would make it different from the approach combining the UNIQUE and NOT NULL constraint(s).
How to portray an alternate key
If, on the contrary,
- a certain set of columns was chosen and defined as the PRIMARY KEY of a given table, and
- other set of columns is constrained as UNIQUE, and each of the members of such set is also fixed with a NOT NULL constraint,
then the designer is representing an alternate key (if, a certain table has one or more candidate keys, and one of these is granted the status of primary, then the remaining ones become alternate keys).
For instance, the following table presents one PRIMARY KEY and three ALTERNATE KEYs:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId), -- Single-column PRIMARY KEY
CONSTRAINT Foo_AK1 UNIQUE (Bar), -- Single-column ALTERNATE KEY
CONSTRAINT Foo_AK2 UNIQUE (Baz, Qux, Corge), -- Multi-column ALTERNATE KEY
CONSTRAINT Foo_AK3 UNIQUE (Grault) -- Single-column ALTERNATE KEY
);
A candidate key put up as demonstrated above can be, evidently, referenced from one or more foreign key constraints.
Using the CANDIDATE KEY
keyword when there is already a PRIMARY KEY?
Assuming that there is a DBMS that does provide the CANDIDATE KEY
keyword, if a table has a primary key declared, then the creation of a candidate key should be rejected, and said DBMS should as well provide the ALTERNATE KEY
keyword to represent one or more alternate keys when applicable.
Illustration of the same table (i) with one candidate key and (ii) with primary key
Yes, at the logical level of abstraction of a database, a candidate key that is established by means of a table-level UNIQUE constraint in conjunction with the applicable column-level NOT NULL counterpart(s), as exemplified as follows:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_CK UNIQUE (FooId)
);
…would be equivalent to a primary key set up as shown below:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId) -- Single-column PRIMARY KEY, constraint that implies the rejection of NULL marks.
);
This is so because, if a given table has only one candidate key (which, as illustrated before, can be composite, i.e., made up of two or more columns), then it can be considered, automatically, the primary key.
Physical-level support
And, yes, in order to support a UNIQUE constraint, some DBMSs may employ an index whose type is different from the one of the index utilized to sustain a PRIMARY KEY counterpart (e.g., non-clustered vs clustered), but this is a factor that is part of the physical (or internal) level of abstraction (which, by the way, might or might not apply depending of the DBMS of use), therefore it is entirely outside of the scope of the logical level constraints configured for a table (as long as the DBMS guarantees the uniqueness of the values contained in the column[s] involved).
add a comment |
I'll go out on a limb and guess you've done a course based on Elmasri and Navathe's textbook. This book is quite theoretical and isn't the clearest of prose, and it tends to use terminology that isn't widely used elsewhere. The net effect it that it seems to confuse students on a semi-regular basis - many if not most questions like this seem to originate from students trying to make sense of this textbook.
In data modelling, a candidate key is a possible key that you could use to identify a record. There may be just one or more than one; some could be single references, some could be combinations of attributes. It is purely a modelling concept to describe keys that you could select to use as a primary identifier.
Tables in a RDBMS can only have a single primary key (selected at design time) but they can have multiple uniqueness constraints. Normally you would select a key (or add a synthetic key) and use that as the primary identifier. You can also create uniqueness constraints on column(s) that would make up other candidate keys, although these are not called candidate keys when physicalised in this way.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228423%2fdoes-the-concept-of-candidate-key-exist-only-in-theory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As far as I know, no SQL database management system (DBMS) supplies the CANDIDATE KEY
keyword as such, but (as I consider that you are suggesting in the question) that does not mean that the notion (or the functionality) of candidate key cannot be configured in a SQL table.
How to represent a candidate key
For example, if
- there is no primary declared for the table under consideration, and
- a whole set of columns (i.e., one or more) that is configured with a UNIQUE constraint is also fixed with the corresponding NOT NULL constraint(s),
then the designer is, precisely, representing a candidate key.
For example, the following table shows three distinct candidate keys:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_CK1 UNIQUE (FooId), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK2 UNIQUE (Bar), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK3 UNIQUE (Baz, Qux, Corge) -- Multi-column CANDIDATE KEY
);
A candidate key set up as such is, as you know, susceptible of being the reference of one or more foreign key constraints.
It is worth to stress the fact that, since SQL and its dialects include the idea of NULL marks, a UNIQUE constraint alone is not sufficient to stand for a candidate key (as expounded in the DDL sample above). This point is particularly significant because the column(s) constrained as a candidate key cannot retain NULL marks, otherwise it could not be deemed a real candidate key (besides, a table enclosing NULL marks in any of its columns cannot be considered a relational table but, yes, that is a different subject).
How does this differ from the CANDIDATE KEY
keyword approach?
In this way, if the vendors/developers of a certain SQL DBMS wants to provide the CANDIDATE KEY
keyword, then this kind of constraint, apart from the evident uniqueness enforcement, must also ensure the rejection of NULL marks in the relevant column(s), factor that would make it different from the approach combining the UNIQUE and NOT NULL constraint(s).
How to portray an alternate key
If, on the contrary,
- a certain set of columns was chosen and defined as the PRIMARY KEY of a given table, and
- other set of columns is constrained as UNIQUE, and each of the members of such set is also fixed with a NOT NULL constraint,
then the designer is representing an alternate key (if, a certain table has one or more candidate keys, and one of these is granted the status of primary, then the remaining ones become alternate keys).
For instance, the following table presents one PRIMARY KEY and three ALTERNATE KEYs:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId), -- Single-column PRIMARY KEY
CONSTRAINT Foo_AK1 UNIQUE (Bar), -- Single-column ALTERNATE KEY
CONSTRAINT Foo_AK2 UNIQUE (Baz, Qux, Corge), -- Multi-column ALTERNATE KEY
CONSTRAINT Foo_AK3 UNIQUE (Grault) -- Single-column ALTERNATE KEY
);
A candidate key put up as demonstrated above can be, evidently, referenced from one or more foreign key constraints.
Using the CANDIDATE KEY
keyword when there is already a PRIMARY KEY?
Assuming that there is a DBMS that does provide the CANDIDATE KEY
keyword, if a table has a primary key declared, then the creation of a candidate key should be rejected, and said DBMS should as well provide the ALTERNATE KEY
keyword to represent one or more alternate keys when applicable.
Illustration of the same table (i) with one candidate key and (ii) with primary key
Yes, at the logical level of abstraction of a database, a candidate key that is established by means of a table-level UNIQUE constraint in conjunction with the applicable column-level NOT NULL counterpart(s), as exemplified as follows:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_CK UNIQUE (FooId)
);
…would be equivalent to a primary key set up as shown below:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId) -- Single-column PRIMARY KEY, constraint that implies the rejection of NULL marks.
);
This is so because, if a given table has only one candidate key (which, as illustrated before, can be composite, i.e., made up of two or more columns), then it can be considered, automatically, the primary key.
Physical-level support
And, yes, in order to support a UNIQUE constraint, some DBMSs may employ an index whose type is different from the one of the index utilized to sustain a PRIMARY KEY counterpart (e.g., non-clustered vs clustered), but this is a factor that is part of the physical (or internal) level of abstraction (which, by the way, might or might not apply depending of the DBMS of use), therefore it is entirely outside of the scope of the logical level constraints configured for a table (as long as the DBMS guarantees the uniqueness of the values contained in the column[s] involved).
add a comment |
As far as I know, no SQL database management system (DBMS) supplies the CANDIDATE KEY
keyword as such, but (as I consider that you are suggesting in the question) that does not mean that the notion (or the functionality) of candidate key cannot be configured in a SQL table.
How to represent a candidate key
For example, if
- there is no primary declared for the table under consideration, and
- a whole set of columns (i.e., one or more) that is configured with a UNIQUE constraint is also fixed with the corresponding NOT NULL constraint(s),
then the designer is, precisely, representing a candidate key.
For example, the following table shows three distinct candidate keys:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_CK1 UNIQUE (FooId), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK2 UNIQUE (Bar), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK3 UNIQUE (Baz, Qux, Corge) -- Multi-column CANDIDATE KEY
);
A candidate key set up as such is, as you know, susceptible of being the reference of one or more foreign key constraints.
It is worth to stress the fact that, since SQL and its dialects include the idea of NULL marks, a UNIQUE constraint alone is not sufficient to stand for a candidate key (as expounded in the DDL sample above). This point is particularly significant because the column(s) constrained as a candidate key cannot retain NULL marks, otherwise it could not be deemed a real candidate key (besides, a table enclosing NULL marks in any of its columns cannot be considered a relational table but, yes, that is a different subject).
How does this differ from the CANDIDATE KEY
keyword approach?
In this way, if the vendors/developers of a certain SQL DBMS wants to provide the CANDIDATE KEY
keyword, then this kind of constraint, apart from the evident uniqueness enforcement, must also ensure the rejection of NULL marks in the relevant column(s), factor that would make it different from the approach combining the UNIQUE and NOT NULL constraint(s).
How to portray an alternate key
If, on the contrary,
- a certain set of columns was chosen and defined as the PRIMARY KEY of a given table, and
- other set of columns is constrained as UNIQUE, and each of the members of such set is also fixed with a NOT NULL constraint,
then the designer is representing an alternate key (if, a certain table has one or more candidate keys, and one of these is granted the status of primary, then the remaining ones become alternate keys).
For instance, the following table presents one PRIMARY KEY and three ALTERNATE KEYs:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId), -- Single-column PRIMARY KEY
CONSTRAINT Foo_AK1 UNIQUE (Bar), -- Single-column ALTERNATE KEY
CONSTRAINT Foo_AK2 UNIQUE (Baz, Qux, Corge), -- Multi-column ALTERNATE KEY
CONSTRAINT Foo_AK3 UNIQUE (Grault) -- Single-column ALTERNATE KEY
);
A candidate key put up as demonstrated above can be, evidently, referenced from one or more foreign key constraints.
Using the CANDIDATE KEY
keyword when there is already a PRIMARY KEY?
Assuming that there is a DBMS that does provide the CANDIDATE KEY
keyword, if a table has a primary key declared, then the creation of a candidate key should be rejected, and said DBMS should as well provide the ALTERNATE KEY
keyword to represent one or more alternate keys when applicable.
Illustration of the same table (i) with one candidate key and (ii) with primary key
Yes, at the logical level of abstraction of a database, a candidate key that is established by means of a table-level UNIQUE constraint in conjunction with the applicable column-level NOT NULL counterpart(s), as exemplified as follows:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_CK UNIQUE (FooId)
);
…would be equivalent to a primary key set up as shown below:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId) -- Single-column PRIMARY KEY, constraint that implies the rejection of NULL marks.
);
This is so because, if a given table has only one candidate key (which, as illustrated before, can be composite, i.e., made up of two or more columns), then it can be considered, automatically, the primary key.
Physical-level support
And, yes, in order to support a UNIQUE constraint, some DBMSs may employ an index whose type is different from the one of the index utilized to sustain a PRIMARY KEY counterpart (e.g., non-clustered vs clustered), but this is a factor that is part of the physical (or internal) level of abstraction (which, by the way, might or might not apply depending of the DBMS of use), therefore it is entirely outside of the scope of the logical level constraints configured for a table (as long as the DBMS guarantees the uniqueness of the values contained in the column[s] involved).
add a comment |
As far as I know, no SQL database management system (DBMS) supplies the CANDIDATE KEY
keyword as such, but (as I consider that you are suggesting in the question) that does not mean that the notion (or the functionality) of candidate key cannot be configured in a SQL table.
How to represent a candidate key
For example, if
- there is no primary declared for the table under consideration, and
- a whole set of columns (i.e., one or more) that is configured with a UNIQUE constraint is also fixed with the corresponding NOT NULL constraint(s),
then the designer is, precisely, representing a candidate key.
For example, the following table shows three distinct candidate keys:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_CK1 UNIQUE (FooId), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK2 UNIQUE (Bar), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK3 UNIQUE (Baz, Qux, Corge) -- Multi-column CANDIDATE KEY
);
A candidate key set up as such is, as you know, susceptible of being the reference of one or more foreign key constraints.
It is worth to stress the fact that, since SQL and its dialects include the idea of NULL marks, a UNIQUE constraint alone is not sufficient to stand for a candidate key (as expounded in the DDL sample above). This point is particularly significant because the column(s) constrained as a candidate key cannot retain NULL marks, otherwise it could not be deemed a real candidate key (besides, a table enclosing NULL marks in any of its columns cannot be considered a relational table but, yes, that is a different subject).
How does this differ from the CANDIDATE KEY
keyword approach?
In this way, if the vendors/developers of a certain SQL DBMS wants to provide the CANDIDATE KEY
keyword, then this kind of constraint, apart from the evident uniqueness enforcement, must also ensure the rejection of NULL marks in the relevant column(s), factor that would make it different from the approach combining the UNIQUE and NOT NULL constraint(s).
How to portray an alternate key
If, on the contrary,
- a certain set of columns was chosen and defined as the PRIMARY KEY of a given table, and
- other set of columns is constrained as UNIQUE, and each of the members of such set is also fixed with a NOT NULL constraint,
then the designer is representing an alternate key (if, a certain table has one or more candidate keys, and one of these is granted the status of primary, then the remaining ones become alternate keys).
For instance, the following table presents one PRIMARY KEY and three ALTERNATE KEYs:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId), -- Single-column PRIMARY KEY
CONSTRAINT Foo_AK1 UNIQUE (Bar), -- Single-column ALTERNATE KEY
CONSTRAINT Foo_AK2 UNIQUE (Baz, Qux, Corge), -- Multi-column ALTERNATE KEY
CONSTRAINT Foo_AK3 UNIQUE (Grault) -- Single-column ALTERNATE KEY
);
A candidate key put up as demonstrated above can be, evidently, referenced from one or more foreign key constraints.
Using the CANDIDATE KEY
keyword when there is already a PRIMARY KEY?
Assuming that there is a DBMS that does provide the CANDIDATE KEY
keyword, if a table has a primary key declared, then the creation of a candidate key should be rejected, and said DBMS should as well provide the ALTERNATE KEY
keyword to represent one or more alternate keys when applicable.
Illustration of the same table (i) with one candidate key and (ii) with primary key
Yes, at the logical level of abstraction of a database, a candidate key that is established by means of a table-level UNIQUE constraint in conjunction with the applicable column-level NOT NULL counterpart(s), as exemplified as follows:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_CK UNIQUE (FooId)
);
…would be equivalent to a primary key set up as shown below:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId) -- Single-column PRIMARY KEY, constraint that implies the rejection of NULL marks.
);
This is so because, if a given table has only one candidate key (which, as illustrated before, can be composite, i.e., made up of two or more columns), then it can be considered, automatically, the primary key.
Physical-level support
And, yes, in order to support a UNIQUE constraint, some DBMSs may employ an index whose type is different from the one of the index utilized to sustain a PRIMARY KEY counterpart (e.g., non-clustered vs clustered), but this is a factor that is part of the physical (or internal) level of abstraction (which, by the way, might or might not apply depending of the DBMS of use), therefore it is entirely outside of the scope of the logical level constraints configured for a table (as long as the DBMS guarantees the uniqueness of the values contained in the column[s] involved).
As far as I know, no SQL database management system (DBMS) supplies the CANDIDATE KEY
keyword as such, but (as I consider that you are suggesting in the question) that does not mean that the notion (or the functionality) of candidate key cannot be configured in a SQL table.
How to represent a candidate key
For example, if
- there is no primary declared for the table under consideration, and
- a whole set of columns (i.e., one or more) that is configured with a UNIQUE constraint is also fixed with the corresponding NOT NULL constraint(s),
then the designer is, precisely, representing a candidate key.
For example, the following table shows three distinct candidate keys:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_CK1 UNIQUE (FooId), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK2 UNIQUE (Bar), -- Single-column CANDIDATE KEY
CONSTRAINT Foo_CK3 UNIQUE (Baz, Qux, Corge) -- Multi-column CANDIDATE KEY
);
A candidate key set up as such is, as you know, susceptible of being the reference of one or more foreign key constraints.
It is worth to stress the fact that, since SQL and its dialects include the idea of NULL marks, a UNIQUE constraint alone is not sufficient to stand for a candidate key (as expounded in the DDL sample above). This point is particularly significant because the column(s) constrained as a candidate key cannot retain NULL marks, otherwise it could not be deemed a real candidate key (besides, a table enclosing NULL marks in any of its columns cannot be considered a relational table but, yes, that is a different subject).
How does this differ from the CANDIDATE KEY
keyword approach?
In this way, if the vendors/developers of a certain SQL DBMS wants to provide the CANDIDATE KEY
keyword, then this kind of constraint, apart from the evident uniqueness enforcement, must also ensure the rejection of NULL marks in the relevant column(s), factor that would make it different from the approach combining the UNIQUE and NOT NULL constraint(s).
How to portray an alternate key
If, on the contrary,
- a certain set of columns was chosen and defined as the PRIMARY KEY of a given table, and
- other set of columns is constrained as UNIQUE, and each of the members of such set is also fixed with a NOT NULL constraint,
then the designer is representing an alternate key (if, a certain table has one or more candidate keys, and one of these is granted the status of primary, then the remaining ones become alternate keys).
For instance, the following table presents one PRIMARY KEY and three ALTERNATE KEYs:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
Qux INT NOT NULL,
Corge CHAR(25) NOT NULL,
Grault INT NOT NULL,
Garply BIT NOT NULL,
Plugh TEXT NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId), -- Single-column PRIMARY KEY
CONSTRAINT Foo_AK1 UNIQUE (Bar), -- Single-column ALTERNATE KEY
CONSTRAINT Foo_AK2 UNIQUE (Baz, Qux, Corge), -- Multi-column ALTERNATE KEY
CONSTRAINT Foo_AK3 UNIQUE (Grault) -- Single-column ALTERNATE KEY
);
A candidate key put up as demonstrated above can be, evidently, referenced from one or more foreign key constraints.
Using the CANDIDATE KEY
keyword when there is already a PRIMARY KEY?
Assuming that there is a DBMS that does provide the CANDIDATE KEY
keyword, if a table has a primary key declared, then the creation of a candidate key should be rejected, and said DBMS should as well provide the ALTERNATE KEY
keyword to represent one or more alternate keys when applicable.
Illustration of the same table (i) with one candidate key and (ii) with primary key
Yes, at the logical level of abstraction of a database, a candidate key that is established by means of a table-level UNIQUE constraint in conjunction with the applicable column-level NOT NULL counterpart(s), as exemplified as follows:
CREATE TABLE Foo (
FooId INT NOT NULL,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_CK UNIQUE (FooId)
);
…would be equivalent to a primary key set up as shown below:
CREATE TABLE Foo (
FooId INT,
Bar CHAR(30) NOT NULL,
Baz DATETIME NOT NULL,
CONSTRAINT Foo_PK PRIMARY KEY (FooId) -- Single-column PRIMARY KEY, constraint that implies the rejection of NULL marks.
);
This is so because, if a given table has only one candidate key (which, as illustrated before, can be composite, i.e., made up of two or more columns), then it can be considered, automatically, the primary key.
Physical-level support
And, yes, in order to support a UNIQUE constraint, some DBMSs may employ an index whose type is different from the one of the index utilized to sustain a PRIMARY KEY counterpart (e.g., non-clustered vs clustered), but this is a factor that is part of the physical (or internal) level of abstraction (which, by the way, might or might not apply depending of the DBMS of use), therefore it is entirely outside of the scope of the logical level constraints configured for a table (as long as the DBMS guarantees the uniqueness of the values contained in the column[s] involved).
edited 14 hours ago
answered 16 hours ago
MDCCLMDCCL
6,81331745
6,81331745
add a comment |
add a comment |
I'll go out on a limb and guess you've done a course based on Elmasri and Navathe's textbook. This book is quite theoretical and isn't the clearest of prose, and it tends to use terminology that isn't widely used elsewhere. The net effect it that it seems to confuse students on a semi-regular basis - many if not most questions like this seem to originate from students trying to make sense of this textbook.
In data modelling, a candidate key is a possible key that you could use to identify a record. There may be just one or more than one; some could be single references, some could be combinations of attributes. It is purely a modelling concept to describe keys that you could select to use as a primary identifier.
Tables in a RDBMS can only have a single primary key (selected at design time) but they can have multiple uniqueness constraints. Normally you would select a key (or add a synthetic key) and use that as the primary identifier. You can also create uniqueness constraints on column(s) that would make up other candidate keys, although these are not called candidate keys when physicalised in this way.
add a comment |
I'll go out on a limb and guess you've done a course based on Elmasri and Navathe's textbook. This book is quite theoretical and isn't the clearest of prose, and it tends to use terminology that isn't widely used elsewhere. The net effect it that it seems to confuse students on a semi-regular basis - many if not most questions like this seem to originate from students trying to make sense of this textbook.
In data modelling, a candidate key is a possible key that you could use to identify a record. There may be just one or more than one; some could be single references, some could be combinations of attributes. It is purely a modelling concept to describe keys that you could select to use as a primary identifier.
Tables in a RDBMS can only have a single primary key (selected at design time) but they can have multiple uniqueness constraints. Normally you would select a key (or add a synthetic key) and use that as the primary identifier. You can also create uniqueness constraints on column(s) that would make up other candidate keys, although these are not called candidate keys when physicalised in this way.
add a comment |
I'll go out on a limb and guess you've done a course based on Elmasri and Navathe's textbook. This book is quite theoretical and isn't the clearest of prose, and it tends to use terminology that isn't widely used elsewhere. The net effect it that it seems to confuse students on a semi-regular basis - many if not most questions like this seem to originate from students trying to make sense of this textbook.
In data modelling, a candidate key is a possible key that you could use to identify a record. There may be just one or more than one; some could be single references, some could be combinations of attributes. It is purely a modelling concept to describe keys that you could select to use as a primary identifier.
Tables in a RDBMS can only have a single primary key (selected at design time) but they can have multiple uniqueness constraints. Normally you would select a key (or add a synthetic key) and use that as the primary identifier. You can also create uniqueness constraints on column(s) that would make up other candidate keys, although these are not called candidate keys when physicalised in this way.
I'll go out on a limb and guess you've done a course based on Elmasri and Navathe's textbook. This book is quite theoretical and isn't the clearest of prose, and it tends to use terminology that isn't widely used elsewhere. The net effect it that it seems to confuse students on a semi-regular basis - many if not most questions like this seem to originate from students trying to make sense of this textbook.
In data modelling, a candidate key is a possible key that you could use to identify a record. There may be just one or more than one; some could be single references, some could be combinations of attributes. It is purely a modelling concept to describe keys that you could select to use as a primary identifier.
Tables in a RDBMS can only have a single primary key (selected at design time) but they can have multiple uniqueness constraints. Normally you would select a key (or add a synthetic key) and use that as the primary identifier. You can also create uniqueness constraints on column(s) that would make up other candidate keys, although these are not called candidate keys when physicalised in this way.
edited 2 hours ago
answered 2 hours ago
ConcernedOfTunbridgeWellsConcernedOfTunbridgeWells
15.9k24768
15.9k24768
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228423%2fdoes-the-concept-of-candidate-key-exist-only-in-theory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
AFAIK: The word
Candidate key
has no meaning in any SQL DBMS. The set of UNIQUE constraints and the PRIMARY key is the CANDIDATE keys. I have never really understood why Primary keys where invented in the first place, i.e. why one of the CANDIDATE keys is considered special, and in need of a special name.– Lennart
Jan 30 at 8:36
@Lennart I have never really understood why Primary key ... is considered special I personally understand the difference between a unique and a primary key this way. Unique key provides key expression uniqueness over current table data. Primary key provides key expression uniqueness over all table lifetime data. If a record with some unique combination was deleted, another record with that expression value can be inserted later. If a record with some primary key value was deleted, the creation of new record with that primary key value is at least wrong practice. And forbidden is better.
– Akina
Jan 30 at 8:44
those are good points. @Akina I dont know whether "...is at least wrong practice" is part of standard definition of primary key. I believe its more sort of recurring real world scenario or de facto practice.
– anir
Jan 30 at 8:49
1
@Lennart: the difference between a primary key and a unique key is that a primary key by definition does not allow NULL values, a unique key however might allow them.
– a_horse_with_no_name
Jan 30 at 9:59
2
@Kondybas: a table is never "ordered" by anything. Only the result of a query can be ordered. An neither the PK or a UK define any ordering to begin with. They only provide a way to uniquely identify a single row.
– a_horse_with_no_name
Jan 30 at 10:13