Table creation: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 4: Line 4:


==[[PostgreSQL]]==
==[[PostgreSQL]]==

Postgres developers, please feel free to add additional fields you commonly use to this example.


-- This is a comment
-- This is a comment
Line 23: Line 25:
note text not null,
note text not null,
unique(account_id, note)
unique(account_id, note)
);
);


-- bool: 't', 'f' or NULL
-- bool: 't', 'f' or NULL
-- int2: -32768 to +32767
-- int2: -32768 to +32767
-- int4: -2147483648 to +2147483647
-- int4: -2147483648 to +2147483647
-- float: decimal
-- float: decimal
-- date: obvious
-- date: obvious
-- timestamp: date time
-- timestamp: date time
-- char(#): space padded text field with length of #
-- char(#): space padded text field with length of #
-- varchar(#): variable length text field up to #
-- varchar(#): variable length text field up to #
-- text: not limited
-- text: not limited

Revision as of 15:12, 23 January 2007

Task
Table creation
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to create a table to exemplify most commonly used data types and options.

PostgreSQL

Postgres developers, please feel free to add additional fields you commonly use to this example.

-- This is a comment

CREATE SEQUENCE account_seq start 100;
CREATE TABLE account (
  account_id  int4 PRIMARY KEY DEFAULT nextval('account_seq'),
  created     date not null default now(),
  active      bool not null default 't',
  username    varchar(16) unique not null,
  balance     float default 0,
  age         int2,
  notes       text
);

CREATE TABLE account_note (
  account_id  int4 not null REFERENCES account,
  created     timestamp not null default now(),
  note        text not null,
  unique(account_id, note)
); 
-- bool:       't', 'f' or NULL
-- int2:       -32768 to +32767
-- int4:       -2147483648 to +2147483647
-- float:      decimal
-- date:       obvious
-- timestamp:  date time
-- char(#):    space padded text field with length of #
-- varchar(#): variable length text field up to #
-- text:       not limited