Table with default value

From: Sharon Cowling <sharon(dot)cowling(at)sslnz(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Table with default value
Date: 2002-01-11 03:14:00
Message-ID: 200201110310.g0B3ALv03741@lambton.sslnz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

In my java code I test if a licence number already exists, if it does, error, if not carry on and insert into database. I am not using the unique constraint as this does not work through the front-end for some reason (It would make life easier if it did!). I get an error in my code when I test for the value and get a null back (null values are perfectly acceptable). So I thought I would re create my table with a default value of 'No' for drivers licence. For some reason this does not work?! It should do, heres the info, any ideas?!

CREATE TABLE person4(
person_id INT NOT NULL,
firstname VARCHAR(25) NOT NULL,
lastname VARCHAR(25) NOT NULL,
dob date NOT NULL,
street VARCHAR(50) NOT NULL,
suburb VARCHAR(50),
city VARCHAR(50) NOT NULL,
homephone VARCHAR(15),
workphone VARCHAR(15),
mobile VARCHAR(15),
type VARCHAR(30) NOT NULL,
date_approved DATE NOT NULL,
approved_by VARCHAR(50) NOT NULL,
vehicle_type VARCHAR(50),
vehicle_rego VARCHAR(6),
drivers_licence VARCHAR(20) DEFAULT 'No',
firearms_licence VARCHAR(20) DEFAULT 'No',
notes VARCHAR(80),
PRIMARY KEY (person_id));

user=> \d
Table "person4"
Attribute | Type | Modifier
------------------+-----------------------+--------------
person_id | integer | not null
firstname | character varying(25) | not null
lastname | character varying(25) | not null
dob | date | not null
street | character varying(50) | not null
suburb | character varying(50) |
city | character varying(50) | not null
homephone | character varying(15) |
workphone | character varying(15) |
mobile | character varying(15) |
type | character varying(30) | not null
date_approved | date | not null
approved_by | character varying(50) | not null
vehicle_type | character varying(50) |
vehicle_rego | character varying(6) |
drivers_licence | character varying(20) | default 'No'
firearms_licence | character varying(20) | default 'No'
notes | character varying(80) |
Index: person4_pkey

user=> select * from person4;
person_id | firstname | lastname | dob | street | suburb | city | homephone | workphone | mobile | type | date_approved | approved_by | vehicle_type | vehicle_rego | drivers_licence | firearms_licence | notes
-----------+-----------+----------+------------+--------+--------+------+-----------+-----------+--------+----------------+---------------+-------------+--------------+--------------+-----------------+------------------+-------
778 | sdf | sdf | 11/11/1977 | sf | | dsf | | | | Owner/Relative | 11/01/2002 | test | | | | |
(1 row)

As you can see there is no default value of 'No' for drivers licence.

The below error is from the front end when trying to insert a value for drivers_licence - A query runs to select drivers_licence from person4 where user input value = drivers_licence to see if it exists, should be 'No' and carry on without error:

java.lang.NullPointerException
at org.postgresql.jdbc2.ResultSet.getString(ResultSet.java:171)
at org.postgresql.jdbc2.ResultSet.getString(ResultSet.java:611)

Regards,

Sharon Cowling

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Mel Roman 2002-01-11 04:52:10 How do you do a subselect in postgresql?
Previous Message root 2002-01-10 23:28:31 location for pgsql data