patternMinor
"Table of Constants" - is this common practice?
Viewed 0 times
thispracticeconstantscommontable
Problem
College SQL class, using the book "SQL Fundamentals" by John J. Patrick. In the third chapter, he talks about using a "table of constants" to add columns to a select statement, where all rows have the same value.
For example, if you have table "characters", as so:
and you want a SELECT that adds a column "hometown" with value "BEDROCK" to all rows, he recommends making a second table in the database, "temp", with
and then doing
The idea is that this avoids putting string constants in the SELECT statement, and that if you've got a lot of SELECTs that need the same constants, it's easier to update one table than fifty queries.
The thing is, I've been working with SQL databases for the last fifteen years, and I have never seen this construction. Is it something totally common that I've just missed, or is it something that I can erase from my memory after this assignment is over?
For example, if you have table "characters", as so:
first_name last_name dept_code
----------- ---------- -------------------
Fred Flintstone ROCKS
Barney Rubble ROCKS
Wilma Flintstone FACEPALMING_AT_FREDand you want a SELECT that adds a column "hometown" with value "BEDROCK" to all rows, he recommends making a second table in the database, "temp", with
hometown
--------
BEDROCKand then doing
SELECT first_name, last_name, dept_code, hometown FROM characters, tempThe idea is that this avoids putting string constants in the SELECT statement, and that if you've got a lot of SELECTs that need the same constants, it's easier to update one table than fifty queries.
The thing is, I've been working with SQL databases for the last fifteen years, and I have never seen this construction. Is it something totally common that I've just missed, or is it something that I can erase from my memory after this assignment is over?
Solution
Joe Celko mentions tables of constants in a couple of his books.
He suggests if using a table that a check constraint is added that ensures the table can contain no more than one row.
Or alternatively a view can be used for a similar purpose.
Not something I've used much, if at all, myself but always worth having additional possible techniques to consider.
The specific use case in your question isn't something I would consider a table of constants for though. TBH the hardcoded same "hometown" for everyone just seems a nonsensical requirement.
He suggests if using a table that a check constraint is added that ensures the table can contain no more than one row.
CREATE TABLE Constants
(
lock CHAR(1) DEFAULT 'X' NOT NULL PRIMARY KEY CHECK (lock = 'X'),
pi FLOAT DEFAULT 3.142592653 NOT NULL,
e FLOAT DEFAULT 2.71828182 NOT NULL,
phi FLOAT DEFAULT 1.6180339887 NOT NULL
);Or alternatively a view can be used for a similar purpose.
CREATE VIEW Constants
AS
SELECT *
FROM
(VALUES(3.142592653,
2.71828182,
1.6180339887)) V(pi,e,phi)Not something I've used much, if at all, myself but always worth having additional possible techniques to consider.
The specific use case in your question isn't something I would consider a table of constants for though. TBH the hardcoded same "hometown" for everyone just seems a nonsensical requirement.
Code Snippets
CREATE TABLE Constants
(
lock CHAR(1) DEFAULT 'X' NOT NULL PRIMARY KEY CHECK (lock = 'X'),
pi FLOAT DEFAULT 3.142592653 NOT NULL,
e FLOAT DEFAULT 2.71828182 NOT NULL,
phi FLOAT DEFAULT 1.6180339887 NOT NULL
);CREATE VIEW Constants
AS
SELECT *
FROM
(VALUES(3.142592653,
2.71828182,
1.6180339887)) V(pi,e,phi)Context
StackExchange Database Administrators Q#15619, answer score: 9
Revisions (0)
No revisions yet.