Re: Auto Adjust Age

From: Andrew Sullivan <ajs(at)crankycanuck(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Auto Adjust Age
Date: 2011-04-06 15:20:35
Message-ID: 20110406152034.GG7335@shinkuro.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Apr 06, 2011 at 10:53:49AM -0400, Carlos Mennens wrote:
> online that shows me how to do so. I've created a table called 'users'
> and I have it configured as follows:
>
> CREATE TABLE users
> (
> id integer PRIMARY KEY UNIQUE NOT NULL, --ID
> fname character varying(40) NOT NULL, --First name
> lname character varying(40) NOT NULL, --Last name
> email character varying NOT NULL, --email address
> office integer NOT NULL, --Office number
> dob date NOT NULL, --Date of birth
> age integer NOT NULL --Age
> )
> ;
>
> Is there a way in SQL I can have the users 'age' be auto adjusted
> based on the 'id' & 'dob'?

Why do you have the age stored at all? When you SELECT from the table
and want someone's age, just do

SELECT [. . .], extract('years' from age(CURRENT_TIMESTAMP,dob)) as age
. . . FROM users . . .

By and large, it's not a good idea to store something you can
calculate from other data you have.

A
--
Andrew Sullivan
ajs(at)crankycanuck(dot)ca

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message JC de Villa 2011-04-06 15:24:08 Re: Auto Adjust Age
Previous Message Rob Sargent 2011-04-06 15:19:13 Re: Auto Adjust Age