In todays lab session you will be working on practical assignment 2. To get started:
SERIAL datatype in postgres. The serial datatype is an auto-incrementing number that does not need to be specified on insert:
CREATE TABLE tablename (
colname SERIAL,
other_col VARCHAR
);
INSERT into tablename(other_col) VALUES ('test');
INSERT into tablename(other_col) VALUES ('test1');
INSERT into tablename(other_col) VALUES ('test2');
SELECT * FROM tablename;
prac_08=> SELECT * FROM tablename;
colname | other_col
---------+-----------
1 | test
2 | test1
3 | test2
(3 rows)
prac_08=>
RETURNING clause can be used to return the value of the auto-incrementing serial when an insert is executed:
INSERT into tablename(other_col) VALUES ('test3') RETURNING colname;
INSERT into tablename(other_col) VALUES ('test4') RETURNING colname;
INSERT into tablename(other_col) VALUES ('test5') RETURNING colname;
prac_08=> INSERT into tablename(other_col) VALUES ('test3') RETURNING colname;
colname
---------
4
(1 row)
INSERT 0 1
prac_08=> INSERT into tablename(other_col) VALUES ('test4') RETURNING colname;
colname
---------
5
(1 row)
INSERT 0 1
prac_08=> INSERT into tablename(other_col) VALUES ('test5') RETURNING colname;
colname
---------
6
(1 row)
INSERT 0 1
prac_08=>
This will allow you Java program to access the serial number when a new contact is created.
Once your schema is up and running, start on your Java application.