kchiu
kchiu

Reputation: 41

how to use constants in SQL CREATE TABLE?

I have 3 SQL tables, defined as follows:

 CREATE TABLE organs(
    abbreviation   VARCHAR(16),
    -- ... other stuff
 );
 CREATE TABLE blocks(
    abbreviation   VARCHAR(16),
    -- ... other stuff
 );
 CREATE TABLE slides(
    title          VARCHAR(16),
    -- ... other stuff
 );

The 3 fields above all use VARCHAR(16) because they're related and have the same length restriction.

Is there a (preferably portable) way to put '16' into a constant / variable and reference that instead in CREATE TABLE? eg. something like this would be nice:

 CREATE TABLE slides(
    title          VARCHAR(MAX_TITLE_LENGTH),
    -- ... other stuff
 );

I'm using PostgreSQL 8.4.

Upvotes: 4

Views: 1196

Answers (1)

user330315
user330315

Reputation:

That's what domains are for:

CREATE DOMAIN title_data AS varchar(16);
CREATE TABLE slides(
    title          title_data,
    -- ... other stuff
 );

Upvotes: 7

Related Questions