Derived Attributes

How do derived attributes work?  I would like to see a typical SQL example, if possible.
Who is Participating?
jdlambert1Connect With a Mentor Commented:
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.
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.  :) ]
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?
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. :) ]
All Courses

From novice to tech pro — start learning today.