SQL Naming Column
I'm creating a table for my school project and in one of those tables, I'm storing different specs about tablets and when it comes to defining the CPU and the RAM of these devices you also need to specify the measuring unit. So can I put a column name for let's say the CPU something like CPU_Ghz ???? or RAM_GB???
I say this because I'll probably want to perform some queries on those values, and I know I can perform special queries to extract just the number in the case that I save that information like this format '4432mah'(which i think is not very good), but since I'm doing the database from scratch is probably saving it as I mentioned above?? CPU_Ghz ???? or RAM_GB??? Battery_mAh??
Appreciate your help
Have a great day!
sql
add a comment |
I'm creating a table for my school project and in one of those tables, I'm storing different specs about tablets and when it comes to defining the CPU and the RAM of these devices you also need to specify the measuring unit. So can I put a column name for let's say the CPU something like CPU_Ghz ???? or RAM_GB???
I say this because I'll probably want to perform some queries on those values, and I know I can perform special queries to extract just the number in the case that I save that information like this format '4432mah'(which i think is not very good), but since I'm doing the database from scratch is probably saving it as I mentioned above?? CPU_Ghz ???? or RAM_GB??? Battery_mAh??
Appreciate your help
Have a great day!
sql
I had a PC with 5 MB ram years ago.
– jarlh
Nov 13 '18 at 9:13
3
@HoneyBadger - likely correct for a toy project, but may be worth doing for long lived databases or where the range of types of products is wider? E.g. if this same table is going to track embedded systems and compute servers, making sure the units are unambiguous could be argued for.
– Damien_The_Unbeliever
Nov 13 '18 at 9:13
You're right, so I can probably assume that every that goes in there is effectively GB
– caff_91
Nov 13 '18 at 9:14
add a comment |
I'm creating a table for my school project and in one of those tables, I'm storing different specs about tablets and when it comes to defining the CPU and the RAM of these devices you also need to specify the measuring unit. So can I put a column name for let's say the CPU something like CPU_Ghz ???? or RAM_GB???
I say this because I'll probably want to perform some queries on those values, and I know I can perform special queries to extract just the number in the case that I save that information like this format '4432mah'(which i think is not very good), but since I'm doing the database from scratch is probably saving it as I mentioned above?? CPU_Ghz ???? or RAM_GB??? Battery_mAh??
Appreciate your help
Have a great day!
sql
I'm creating a table for my school project and in one of those tables, I'm storing different specs about tablets and when it comes to defining the CPU and the RAM of these devices you also need to specify the measuring unit. So can I put a column name for let's say the CPU something like CPU_Ghz ???? or RAM_GB???
I say this because I'll probably want to perform some queries on those values, and I know I can perform special queries to extract just the number in the case that I save that information like this format '4432mah'(which i think is not very good), but since I'm doing the database from scratch is probably saving it as I mentioned above?? CPU_Ghz ???? or RAM_GB??? Battery_mAh??
Appreciate your help
Have a great day!
sql
sql
asked Nov 13 '18 at 9:08
caff_91caff_91
1
1
I had a PC with 5 MB ram years ago.
– jarlh
Nov 13 '18 at 9:13
3
@HoneyBadger - likely correct for a toy project, but may be worth doing for long lived databases or where the range of types of products is wider? E.g. if this same table is going to track embedded systems and compute servers, making sure the units are unambiguous could be argued for.
– Damien_The_Unbeliever
Nov 13 '18 at 9:13
You're right, so I can probably assume that every that goes in there is effectively GB
– caff_91
Nov 13 '18 at 9:14
add a comment |
I had a PC with 5 MB ram years ago.
– jarlh
Nov 13 '18 at 9:13
3
@HoneyBadger - likely correct for a toy project, but may be worth doing for long lived databases or where the range of types of products is wider? E.g. if this same table is going to track embedded systems and compute servers, making sure the units are unambiguous could be argued for.
– Damien_The_Unbeliever
Nov 13 '18 at 9:13
You're right, so I can probably assume that every that goes in there is effectively GB
– caff_91
Nov 13 '18 at 9:14
I had a PC with 5 MB ram years ago.
– jarlh
Nov 13 '18 at 9:13
I had a PC with 5 MB ram years ago.
– jarlh
Nov 13 '18 at 9:13
3
3
@HoneyBadger - likely correct for a toy project, but may be worth doing for long lived databases or where the range of types of products is wider? E.g. if this same table is going to track embedded systems and compute servers, making sure the units are unambiguous could be argued for.
– Damien_The_Unbeliever
Nov 13 '18 at 9:13
@HoneyBadger - likely correct for a toy project, but may be worth doing for long lived databases or where the range of types of products is wider? E.g. if this same table is going to track embedded systems and compute servers, making sure the units are unambiguous could be argued for.
– Damien_The_Unbeliever
Nov 13 '18 at 9:13
You're right, so I can probably assume that every that goes in there is effectively GB
– caff_91
Nov 13 '18 at 9:14
You're right, so I can probably assume that every that goes in there is effectively GB
– caff_91
Nov 13 '18 at 9:14
add a comment |
3 Answers
3
active
oldest
votes
You could use a separate units table
units table
-----------
id
unit_name
and then in your devices table refer to it
devices table
-------------
id
device_name
cpu_speed
cpu_unit_id
Thanks! Juergen
– caff_91
Nov 13 '18 at 9:16
add a comment |
Yes, you better include the unit in the column name and not in the data. This way you can store the data as a number and this has many advantages over storing it as a string representing value and unit:
- You store only one value per column, this satisfy the 1NF rule.
- The database can enforce values correctness. You can only store the correct type of numbers and within the correct range.
- All queries are far simpler.
- You can tell, just by looking at the column name, the measuring unit.
- You are telling the database users that the value of the column is a specific unit - If you would have stored a string, frequency could have been stored as 1Ghz or 1000Khz and you would have to analyze the units to get the correct value.
- You can use aggregating functions like SUM, AVG etc' (Thanks Damien!)
That was exactly my line of thought with this problem, good to know I wasn't far from that, Many thanks.
– caff_91
Nov 13 '18 at 9:18
juergen's answer is also a very good alternative - if you want to be able to store different units for the same column in different rows you should take his advice and accept his answer.
– Zohar Peled
Nov 13 '18 at 9:23
6. You can compute aggregates.
– Damien_The_Unbeliever
Nov 13 '18 at 9:34
@Damien good point! added.
– Zohar Peled
Nov 13 '18 at 9:35
add a comment |
Your suggestion of including units in the column name is good. Too often you see code where a numeric value is held without any context, so it's unclear whether a disk is 1GB (1024MB), 1TB (1024 GB), or something else.
Better is @juergen_d's answer where you hold this information in a softcoded form, as not only does this tell you what the numeric value actually means, but it gives you the option to change this at runtime / vary to the more appropriate units (e.g. if you're storing disk space in GB and you get a new requirement to track your Raspberry Pi which has 1MB memory, it may be stored as 0GB in the old column (if it's an integer value), which is meaningless.
I'd make a couple of amendments to the unit_table
approach though.
First, I'd have a unit table for each type of unit (i.e. one for capacity units such as KB, MB, GB, TB, whilst having a separate table for calculation speed units (MHz, GHz, etc). That way you avoid any risk of having something nonsensical, such as 32GHz hard disk space.
Second, I'd include a conversion factor. i.e. Include data in the table to allow you to convert to a standard base unit, so that your logic can compare the 1024MB value to the 1GB value and determine that they represent the same underlying value. Should you decide to change your base unit, you have that option.
e.g.
create table CapacityUnit
(
UnitId nvarchar(12) primary key clustered --e.g. GB
, Name nvarchar(36) not null -- optional long form; e.g. Gigabyte
, KbMultiplier bigint not null --i.e. if we store a value of 1GB, what should we multiply by to convert that to KB (or whatever our base unit is)
)
insert CapacityUnit(UnitId, Name, BaseMuliplier)
values ('KB', 'Kilobyte', 1)
, ('MB', 'Megabyte', 1024)
, ('GB', 'Gigabyte', 1024*2014)
, ('TB', 'Terabyte', 1024*2014*2014)
create table ConfigurationSettings
(
OneRecordOnly bit not null default(1) check(OneRecordOnly=1) primary key
, ConfigurationSettingsnvarchar(12) not null foreign key references CapacityUnit(UnitId)
)
insert ConfigurationSettings(ConfigurationSettings) values ('KB')
You can then create a calculated column or view over your data which converts this data to the KB value; so that the value's easy to query at runtime / you can easily compare values of different units.
You can also use this approach the other way around; where you use the same units table to provide conversion information, but store all values in their common base type (i.e. per Zohar's answer, but with the benefit of having the data required to perform conversions when required).
Which approach makes more sense depends on how you'll use the data; if you're performing lots of operations where you need to compare or aggregate different records then Zohar's wins; if you're just creating an inventory management system (i.e. where you just want to record and lookup individual records), then having different units per record is less of a burden / it may be more beneficial to store the values as they'd show on the item's specs. Which design to take depends on what you'll do with the data, and how your application logic will handle it (e.g. if you're going to convert everything to a common base for display purposes anyway that may negate the benefit of keeping the original values).
One other consideration not mentioned in your question... Don't forget that as well as the speed of a processor you can have a different number of processors (sockets), and a different number of cores. Thankfully you don't see (or I've never heard of) cases where there may be different sockets with different numbers of cores / different speeds in the same system; so you can likely just hold that information in additional fields, rather than having to have a child table to store each logical processor associated with the device.
1
Many thanks for your answer John!
– caff_91
Nov 13 '18 at 9:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53277386%2fsql-naming-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use a separate units table
units table
-----------
id
unit_name
and then in your devices table refer to it
devices table
-------------
id
device_name
cpu_speed
cpu_unit_id
Thanks! Juergen
– caff_91
Nov 13 '18 at 9:16
add a comment |
You could use a separate units table
units table
-----------
id
unit_name
and then in your devices table refer to it
devices table
-------------
id
device_name
cpu_speed
cpu_unit_id
Thanks! Juergen
– caff_91
Nov 13 '18 at 9:16
add a comment |
You could use a separate units table
units table
-----------
id
unit_name
and then in your devices table refer to it
devices table
-------------
id
device_name
cpu_speed
cpu_unit_id
You could use a separate units table
units table
-----------
id
unit_name
and then in your devices table refer to it
devices table
-------------
id
device_name
cpu_speed
cpu_unit_id
answered Nov 13 '18 at 9:11
juergen djuergen d
159k24201261
159k24201261
Thanks! Juergen
– caff_91
Nov 13 '18 at 9:16
add a comment |
Thanks! Juergen
– caff_91
Nov 13 '18 at 9:16
Thanks! Juergen
– caff_91
Nov 13 '18 at 9:16
Thanks! Juergen
– caff_91
Nov 13 '18 at 9:16
add a comment |
Yes, you better include the unit in the column name and not in the data. This way you can store the data as a number and this has many advantages over storing it as a string representing value and unit:
- You store only one value per column, this satisfy the 1NF rule.
- The database can enforce values correctness. You can only store the correct type of numbers and within the correct range.
- All queries are far simpler.
- You can tell, just by looking at the column name, the measuring unit.
- You are telling the database users that the value of the column is a specific unit - If you would have stored a string, frequency could have been stored as 1Ghz or 1000Khz and you would have to analyze the units to get the correct value.
- You can use aggregating functions like SUM, AVG etc' (Thanks Damien!)
That was exactly my line of thought with this problem, good to know I wasn't far from that, Many thanks.
– caff_91
Nov 13 '18 at 9:18
juergen's answer is also a very good alternative - if you want to be able to store different units for the same column in different rows you should take his advice and accept his answer.
– Zohar Peled
Nov 13 '18 at 9:23
6. You can compute aggregates.
– Damien_The_Unbeliever
Nov 13 '18 at 9:34
@Damien good point! added.
– Zohar Peled
Nov 13 '18 at 9:35
add a comment |
Yes, you better include the unit in the column name and not in the data. This way you can store the data as a number and this has many advantages over storing it as a string representing value and unit:
- You store only one value per column, this satisfy the 1NF rule.
- The database can enforce values correctness. You can only store the correct type of numbers and within the correct range.
- All queries are far simpler.
- You can tell, just by looking at the column name, the measuring unit.
- You are telling the database users that the value of the column is a specific unit - If you would have stored a string, frequency could have been stored as 1Ghz or 1000Khz and you would have to analyze the units to get the correct value.
- You can use aggregating functions like SUM, AVG etc' (Thanks Damien!)
That was exactly my line of thought with this problem, good to know I wasn't far from that, Many thanks.
– caff_91
Nov 13 '18 at 9:18
juergen's answer is also a very good alternative - if you want to be able to store different units for the same column in different rows you should take his advice and accept his answer.
– Zohar Peled
Nov 13 '18 at 9:23
6. You can compute aggregates.
– Damien_The_Unbeliever
Nov 13 '18 at 9:34
@Damien good point! added.
– Zohar Peled
Nov 13 '18 at 9:35
add a comment |
Yes, you better include the unit in the column name and not in the data. This way you can store the data as a number and this has many advantages over storing it as a string representing value and unit:
- You store only one value per column, this satisfy the 1NF rule.
- The database can enforce values correctness. You can only store the correct type of numbers and within the correct range.
- All queries are far simpler.
- You can tell, just by looking at the column name, the measuring unit.
- You are telling the database users that the value of the column is a specific unit - If you would have stored a string, frequency could have been stored as 1Ghz or 1000Khz and you would have to analyze the units to get the correct value.
- You can use aggregating functions like SUM, AVG etc' (Thanks Damien!)
Yes, you better include the unit in the column name and not in the data. This way you can store the data as a number and this has many advantages over storing it as a string representing value and unit:
- You store only one value per column, this satisfy the 1NF rule.
- The database can enforce values correctness. You can only store the correct type of numbers and within the correct range.
- All queries are far simpler.
- You can tell, just by looking at the column name, the measuring unit.
- You are telling the database users that the value of the column is a specific unit - If you would have stored a string, frequency could have been stored as 1Ghz or 1000Khz and you would have to analyze the units to get the correct value.
- You can use aggregating functions like SUM, AVG etc' (Thanks Damien!)
edited Nov 13 '18 at 9:35
answered Nov 13 '18 at 9:15
Zohar PeledZohar Peled
52.8k73273
52.8k73273
That was exactly my line of thought with this problem, good to know I wasn't far from that, Many thanks.
– caff_91
Nov 13 '18 at 9:18
juergen's answer is also a very good alternative - if you want to be able to store different units for the same column in different rows you should take his advice and accept his answer.
– Zohar Peled
Nov 13 '18 at 9:23
6. You can compute aggregates.
– Damien_The_Unbeliever
Nov 13 '18 at 9:34
@Damien good point! added.
– Zohar Peled
Nov 13 '18 at 9:35
add a comment |
That was exactly my line of thought with this problem, good to know I wasn't far from that, Many thanks.
– caff_91
Nov 13 '18 at 9:18
juergen's answer is also a very good alternative - if you want to be able to store different units for the same column in different rows you should take his advice and accept his answer.
– Zohar Peled
Nov 13 '18 at 9:23
6. You can compute aggregates.
– Damien_The_Unbeliever
Nov 13 '18 at 9:34
@Damien good point! added.
– Zohar Peled
Nov 13 '18 at 9:35
That was exactly my line of thought with this problem, good to know I wasn't far from that, Many thanks.
– caff_91
Nov 13 '18 at 9:18
That was exactly my line of thought with this problem, good to know I wasn't far from that, Many thanks.
– caff_91
Nov 13 '18 at 9:18
juergen's answer is also a very good alternative - if you want to be able to store different units for the same column in different rows you should take his advice and accept his answer.
– Zohar Peled
Nov 13 '18 at 9:23
juergen's answer is also a very good alternative - if you want to be able to store different units for the same column in different rows you should take his advice and accept his answer.
– Zohar Peled
Nov 13 '18 at 9:23
6. You can compute aggregates.
– Damien_The_Unbeliever
Nov 13 '18 at 9:34
6. You can compute aggregates.
– Damien_The_Unbeliever
Nov 13 '18 at 9:34
@Damien good point! added.
– Zohar Peled
Nov 13 '18 at 9:35
@Damien good point! added.
– Zohar Peled
Nov 13 '18 at 9:35
add a comment |
Your suggestion of including units in the column name is good. Too often you see code where a numeric value is held without any context, so it's unclear whether a disk is 1GB (1024MB), 1TB (1024 GB), or something else.
Better is @juergen_d's answer where you hold this information in a softcoded form, as not only does this tell you what the numeric value actually means, but it gives you the option to change this at runtime / vary to the more appropriate units (e.g. if you're storing disk space in GB and you get a new requirement to track your Raspberry Pi which has 1MB memory, it may be stored as 0GB in the old column (if it's an integer value), which is meaningless.
I'd make a couple of amendments to the unit_table
approach though.
First, I'd have a unit table for each type of unit (i.e. one for capacity units such as KB, MB, GB, TB, whilst having a separate table for calculation speed units (MHz, GHz, etc). That way you avoid any risk of having something nonsensical, such as 32GHz hard disk space.
Second, I'd include a conversion factor. i.e. Include data in the table to allow you to convert to a standard base unit, so that your logic can compare the 1024MB value to the 1GB value and determine that they represent the same underlying value. Should you decide to change your base unit, you have that option.
e.g.
create table CapacityUnit
(
UnitId nvarchar(12) primary key clustered --e.g. GB
, Name nvarchar(36) not null -- optional long form; e.g. Gigabyte
, KbMultiplier bigint not null --i.e. if we store a value of 1GB, what should we multiply by to convert that to KB (or whatever our base unit is)
)
insert CapacityUnit(UnitId, Name, BaseMuliplier)
values ('KB', 'Kilobyte', 1)
, ('MB', 'Megabyte', 1024)
, ('GB', 'Gigabyte', 1024*2014)
, ('TB', 'Terabyte', 1024*2014*2014)
create table ConfigurationSettings
(
OneRecordOnly bit not null default(1) check(OneRecordOnly=1) primary key
, ConfigurationSettingsnvarchar(12) not null foreign key references CapacityUnit(UnitId)
)
insert ConfigurationSettings(ConfigurationSettings) values ('KB')
You can then create a calculated column or view over your data which converts this data to the KB value; so that the value's easy to query at runtime / you can easily compare values of different units.
You can also use this approach the other way around; where you use the same units table to provide conversion information, but store all values in their common base type (i.e. per Zohar's answer, but with the benefit of having the data required to perform conversions when required).
Which approach makes more sense depends on how you'll use the data; if you're performing lots of operations where you need to compare or aggregate different records then Zohar's wins; if you're just creating an inventory management system (i.e. where you just want to record and lookup individual records), then having different units per record is less of a burden / it may be more beneficial to store the values as they'd show on the item's specs. Which design to take depends on what you'll do with the data, and how your application logic will handle it (e.g. if you're going to convert everything to a common base for display purposes anyway that may negate the benefit of keeping the original values).
One other consideration not mentioned in your question... Don't forget that as well as the speed of a processor you can have a different number of processors (sockets), and a different number of cores. Thankfully you don't see (or I've never heard of) cases where there may be different sockets with different numbers of cores / different speeds in the same system; so you can likely just hold that information in additional fields, rather than having to have a child table to store each logical processor associated with the device.
1
Many thanks for your answer John!
– caff_91
Nov 13 '18 at 9:48
add a comment |
Your suggestion of including units in the column name is good. Too often you see code where a numeric value is held without any context, so it's unclear whether a disk is 1GB (1024MB), 1TB (1024 GB), or something else.
Better is @juergen_d's answer where you hold this information in a softcoded form, as not only does this tell you what the numeric value actually means, but it gives you the option to change this at runtime / vary to the more appropriate units (e.g. if you're storing disk space in GB and you get a new requirement to track your Raspberry Pi which has 1MB memory, it may be stored as 0GB in the old column (if it's an integer value), which is meaningless.
I'd make a couple of amendments to the unit_table
approach though.
First, I'd have a unit table for each type of unit (i.e. one for capacity units such as KB, MB, GB, TB, whilst having a separate table for calculation speed units (MHz, GHz, etc). That way you avoid any risk of having something nonsensical, such as 32GHz hard disk space.
Second, I'd include a conversion factor. i.e. Include data in the table to allow you to convert to a standard base unit, so that your logic can compare the 1024MB value to the 1GB value and determine that they represent the same underlying value. Should you decide to change your base unit, you have that option.
e.g.
create table CapacityUnit
(
UnitId nvarchar(12) primary key clustered --e.g. GB
, Name nvarchar(36) not null -- optional long form; e.g. Gigabyte
, KbMultiplier bigint not null --i.e. if we store a value of 1GB, what should we multiply by to convert that to KB (or whatever our base unit is)
)
insert CapacityUnit(UnitId, Name, BaseMuliplier)
values ('KB', 'Kilobyte', 1)
, ('MB', 'Megabyte', 1024)
, ('GB', 'Gigabyte', 1024*2014)
, ('TB', 'Terabyte', 1024*2014*2014)
create table ConfigurationSettings
(
OneRecordOnly bit not null default(1) check(OneRecordOnly=1) primary key
, ConfigurationSettingsnvarchar(12) not null foreign key references CapacityUnit(UnitId)
)
insert ConfigurationSettings(ConfigurationSettings) values ('KB')
You can then create a calculated column or view over your data which converts this data to the KB value; so that the value's easy to query at runtime / you can easily compare values of different units.
You can also use this approach the other way around; where you use the same units table to provide conversion information, but store all values in their common base type (i.e. per Zohar's answer, but with the benefit of having the data required to perform conversions when required).
Which approach makes more sense depends on how you'll use the data; if you're performing lots of operations where you need to compare or aggregate different records then Zohar's wins; if you're just creating an inventory management system (i.e. where you just want to record and lookup individual records), then having different units per record is less of a burden / it may be more beneficial to store the values as they'd show on the item's specs. Which design to take depends on what you'll do with the data, and how your application logic will handle it (e.g. if you're going to convert everything to a common base for display purposes anyway that may negate the benefit of keeping the original values).
One other consideration not mentioned in your question... Don't forget that as well as the speed of a processor you can have a different number of processors (sockets), and a different number of cores. Thankfully you don't see (or I've never heard of) cases where there may be different sockets with different numbers of cores / different speeds in the same system; so you can likely just hold that information in additional fields, rather than having to have a child table to store each logical processor associated with the device.
1
Many thanks for your answer John!
– caff_91
Nov 13 '18 at 9:48
add a comment |
Your suggestion of including units in the column name is good. Too often you see code where a numeric value is held without any context, so it's unclear whether a disk is 1GB (1024MB), 1TB (1024 GB), or something else.
Better is @juergen_d's answer where you hold this information in a softcoded form, as not only does this tell you what the numeric value actually means, but it gives you the option to change this at runtime / vary to the more appropriate units (e.g. if you're storing disk space in GB and you get a new requirement to track your Raspberry Pi which has 1MB memory, it may be stored as 0GB in the old column (if it's an integer value), which is meaningless.
I'd make a couple of amendments to the unit_table
approach though.
First, I'd have a unit table for each type of unit (i.e. one for capacity units such as KB, MB, GB, TB, whilst having a separate table for calculation speed units (MHz, GHz, etc). That way you avoid any risk of having something nonsensical, such as 32GHz hard disk space.
Second, I'd include a conversion factor. i.e. Include data in the table to allow you to convert to a standard base unit, so that your logic can compare the 1024MB value to the 1GB value and determine that they represent the same underlying value. Should you decide to change your base unit, you have that option.
e.g.
create table CapacityUnit
(
UnitId nvarchar(12) primary key clustered --e.g. GB
, Name nvarchar(36) not null -- optional long form; e.g. Gigabyte
, KbMultiplier bigint not null --i.e. if we store a value of 1GB, what should we multiply by to convert that to KB (or whatever our base unit is)
)
insert CapacityUnit(UnitId, Name, BaseMuliplier)
values ('KB', 'Kilobyte', 1)
, ('MB', 'Megabyte', 1024)
, ('GB', 'Gigabyte', 1024*2014)
, ('TB', 'Terabyte', 1024*2014*2014)
create table ConfigurationSettings
(
OneRecordOnly bit not null default(1) check(OneRecordOnly=1) primary key
, ConfigurationSettingsnvarchar(12) not null foreign key references CapacityUnit(UnitId)
)
insert ConfigurationSettings(ConfigurationSettings) values ('KB')
You can then create a calculated column or view over your data which converts this data to the KB value; so that the value's easy to query at runtime / you can easily compare values of different units.
You can also use this approach the other way around; where you use the same units table to provide conversion information, but store all values in their common base type (i.e. per Zohar's answer, but with the benefit of having the data required to perform conversions when required).
Which approach makes more sense depends on how you'll use the data; if you're performing lots of operations where you need to compare or aggregate different records then Zohar's wins; if you're just creating an inventory management system (i.e. where you just want to record and lookup individual records), then having different units per record is less of a burden / it may be more beneficial to store the values as they'd show on the item's specs. Which design to take depends on what you'll do with the data, and how your application logic will handle it (e.g. if you're going to convert everything to a common base for display purposes anyway that may negate the benefit of keeping the original values).
One other consideration not mentioned in your question... Don't forget that as well as the speed of a processor you can have a different number of processors (sockets), and a different number of cores. Thankfully you don't see (or I've never heard of) cases where there may be different sockets with different numbers of cores / different speeds in the same system; so you can likely just hold that information in additional fields, rather than having to have a child table to store each logical processor associated with the device.
Your suggestion of including units in the column name is good. Too often you see code where a numeric value is held without any context, so it's unclear whether a disk is 1GB (1024MB), 1TB (1024 GB), or something else.
Better is @juergen_d's answer where you hold this information in a softcoded form, as not only does this tell you what the numeric value actually means, but it gives you the option to change this at runtime / vary to the more appropriate units (e.g. if you're storing disk space in GB and you get a new requirement to track your Raspberry Pi which has 1MB memory, it may be stored as 0GB in the old column (if it's an integer value), which is meaningless.
I'd make a couple of amendments to the unit_table
approach though.
First, I'd have a unit table for each type of unit (i.e. one for capacity units such as KB, MB, GB, TB, whilst having a separate table for calculation speed units (MHz, GHz, etc). That way you avoid any risk of having something nonsensical, such as 32GHz hard disk space.
Second, I'd include a conversion factor. i.e. Include data in the table to allow you to convert to a standard base unit, so that your logic can compare the 1024MB value to the 1GB value and determine that they represent the same underlying value. Should you decide to change your base unit, you have that option.
e.g.
create table CapacityUnit
(
UnitId nvarchar(12) primary key clustered --e.g. GB
, Name nvarchar(36) not null -- optional long form; e.g. Gigabyte
, KbMultiplier bigint not null --i.e. if we store a value of 1GB, what should we multiply by to convert that to KB (or whatever our base unit is)
)
insert CapacityUnit(UnitId, Name, BaseMuliplier)
values ('KB', 'Kilobyte', 1)
, ('MB', 'Megabyte', 1024)
, ('GB', 'Gigabyte', 1024*2014)
, ('TB', 'Terabyte', 1024*2014*2014)
create table ConfigurationSettings
(
OneRecordOnly bit not null default(1) check(OneRecordOnly=1) primary key
, ConfigurationSettingsnvarchar(12) not null foreign key references CapacityUnit(UnitId)
)
insert ConfigurationSettings(ConfigurationSettings) values ('KB')
You can then create a calculated column or view over your data which converts this data to the KB value; so that the value's easy to query at runtime / you can easily compare values of different units.
You can also use this approach the other way around; where you use the same units table to provide conversion information, but store all values in their common base type (i.e. per Zohar's answer, but with the benefit of having the data required to perform conversions when required).
Which approach makes more sense depends on how you'll use the data; if you're performing lots of operations where you need to compare or aggregate different records then Zohar's wins; if you're just creating an inventory management system (i.e. where you just want to record and lookup individual records), then having different units per record is less of a burden / it may be more beneficial to store the values as they'd show on the item's specs. Which design to take depends on what you'll do with the data, and how your application logic will handle it (e.g. if you're going to convert everything to a common base for display purposes anyway that may negate the benefit of keeping the original values).
One other consideration not mentioned in your question... Don't forget that as well as the speed of a processor you can have a different number of processors (sockets), and a different number of cores. Thankfully you don't see (or I've never heard of) cases where there may be different sockets with different numbers of cores / different speeds in the same system; so you can likely just hold that information in additional fields, rather than having to have a child table to store each logical processor associated with the device.
edited Nov 13 '18 at 10:10
answered Nov 13 '18 at 9:29
JohnLBevanJohnLBevan
14.3k145105
14.3k145105
1
Many thanks for your answer John!
– caff_91
Nov 13 '18 at 9:48
add a comment |
1
Many thanks for your answer John!
– caff_91
Nov 13 '18 at 9:48
1
1
Many thanks for your answer John!
– caff_91
Nov 13 '18 at 9:48
Many thanks for your answer John!
– caff_91
Nov 13 '18 at 9:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53277386%2fsql-naming-column%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
I had a PC with 5 MB ram years ago.
– jarlh
Nov 13 '18 at 9:13
3
@HoneyBadger - likely correct for a toy project, but may be worth doing for long lived databases or where the range of types of products is wider? E.g. if this same table is going to track embedded systems and compute servers, making sure the units are unambiguous could be argued for.
– Damien_The_Unbeliever
Nov 13 '18 at 9:13
You're right, so I can probably assume that every that goes in there is effectively GB
– caff_91
Nov 13 '18 at 9:14