Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2889
  • Last Modified:

Derived Attributes

How do derived attributes work?  I would like to see a typical SQL example, if possible.
0
lcor
Asked:
lcor
  • 3
1 Solution
 
BillAn1Commented:
homework????
0
 
jdlambert1Commented:
I'm guessing from your prior questions that you're a professional, so here's an typical example if a table already has numeric columns named TotalSales and TotalCosts:

ALTER TABLE TableA ADD GrossProfit as TotalSales - TotalCosts

[If I'm wrong, and you are asking for homework, then my answer is: I used to have a Chevy Attribute that I would derive to work, and it worked by pushing the gas peddle.  :) ]
0
 
jdlambert1Commented:
Oh, as to how they work, you just define a column with a forumla or expression. You can use any column and most of the functions available in the flavor of SQL used by your brand of database.

When a new record is INSERTED, you don't specify the derived column, or try to insert a value into it, and the database will do that automatically.

It hurts the performance slightly for inserts and updates, but can dramatically improve performance on SELECT's, if you're SELECT'ing whatever the derived value is, because the SELECT will only have to fetch it, not calcuate it on the fly for every row you're querying.
0
 
lcorAuthor Commented:
Gee, guys, I hope I didn't offend anyone with my question.  I was just trying to be specific about what I was looking for.

Anyway, would the ALTER SQL command be included in script that creates the schema?
0
 
jdlambert1Commented:
Sorry, didn't notice the follow up question until now.

You can add an ALTER TABLE command to a longer script, or, if you're creating the table, you can define the derived column in the CREATE TABLE command:

CREATE TABLE TableA(Col1 int, Col2 int, Col3 as Col1 + Col2)

[I don't think anyone was offended. And I was happy to get to use my "derive to work" joke. :) ]
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now