Do candidate key concept exist only in theory?What happens to the index of a primary key after a DROP...
For the Circle of Spores druid's Halo of Spores feature, is your reaction used regardless of whether the other creature succeeds on the saving throw?
SQL Server 2017 crashes when backing up because filepath is wrong
How can I make my enemies feel real and make combat more engaging?
What does an unprocessed RAW file look like?
Can I combine Divination spells with Arcane Eye?
Why are `&array` and `array` pointing to the same address?
Log to console in a Lightning Web Component
Is there a way to pause a running process on Linux systems and resume later?
How to typeset a small black square as a binary operator?
Coworker asking me to not bring cakes due to self control issue. What should I do?
Isn't a semicolon (';') needed after a function declaration in C++?
How to play songs that contain one guitar when we have two or more guitarists?
Is corrosion inhibitor paste conductive?
Multiplying elements of a list
Coworker is trying to get me to sign his petition to run for office. How to decline politely?
What does it mean when an external ID field follows a DML Statement?
Boss asked me to sign a resignation paper without a date on it along with my new contract
Can a Hydra make multiple opportunity attacks at once?
Aliased pipeline using head and cut
80-bit collision resistence because of 80-bit x87 registers?
Are encryption algorithms with fixed-point free permutations inherently flawed?
How do I handle a blinded enemy which wants to attack someone it's sure is there?
simplicial objects in a model category
Can I legally make a website about boycotting a certain company?
Do candidate key concept exist only in theory?
What happens to the index of a primary key after a DROP CONSTRAINT?Candidate key = Key = Minimal Superkey?Is matriculation number a good primary key?What is the difference between Primary key and Super key in DBMSWhy 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 ALWAYSWindows Azure Table Row Key strategy, what makes a useful RowKey?Ensure same tenant in a Many to Many relationshipWhen 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 anyway to designate particular column or a set of columns as candidate key in any of database systems, say SQL Server, postgres, MySQL, Oracle etc.? Is there any reserved keyword for designating column as candidate key like PRIMARY KEY or UNIQUE in case of primary key column and unique key column? I feel UNIQUE constraint itself provides implementation of the candidate key concept. I dont see any practical value of separate CANDIDATE KEY keyword. Is it so?
primary-key unique-key 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 anyway to designate particular column or a set of columns as candidate key in any of database systems, say SQL Server, postgres, MySQL, Oracle etc.? Is there any reserved keyword for designating column as candidate key like PRIMARY KEY or UNIQUE in case of primary key column and unique key column? I feel UNIQUE constraint itself provides implementation of the candidate key concept. I dont see any practical value of separate CANDIDATE KEY keyword. Is it so?
primary-key unique-key candidate-key
2
AFAIK: The wordCandidate keyhas 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
1
@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 anyway to designate particular column or a set of columns as candidate key in any of database systems, say SQL Server, postgres, MySQL, Oracle etc.? Is there any reserved keyword for designating column as candidate key like PRIMARY KEY or UNIQUE in case of primary key column and unique key column? I feel UNIQUE constraint itself provides implementation of the candidate key concept. I dont see any practical value of separate CANDIDATE KEY keyword. Is it so?
primary-key unique-key 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 anyway to designate particular column or a set of columns as candidate key in any of database systems, say SQL Server, postgres, MySQL, Oracle etc.? Is there any reserved keyword for designating column as candidate key like PRIMARY KEY or UNIQUE in case of primary key column and unique key column? I feel UNIQUE constraint itself provides implementation of the candidate key concept. I dont see any practical value of separate CANDIDATE KEY keyword. Is it so?
primary-key unique-key candidate-key
primary-key unique-key candidate-key
asked Jan 30 at 8:18
aniranir
1294
1294
2
AFAIK: The wordCandidate keyhas 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
1
@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 keyhas 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
1
@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
1
1
@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
1 Answer
1
active
oldest
votes
As far as I know, no SQL database management system (DBMS) supplies the CANDIDATE KEY keyword as such, but 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.
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.
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 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 |
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%2fdo-candidate-key-concept-exist-only-in-theory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
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 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.
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.
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 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 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.
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.
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 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 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.
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.
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 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 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.
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.
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 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).
answered 4 mins ago
MDCCLMDCCL
6,78331745
6,78331745
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%2fdo-candidate-key-concept-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 keyhas 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
1
@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