473,412 Members | 4,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

adding SERIAL to a table

Hello

Now perhaps this is a bit dumb, but...

I just populated a new table via \copy. After that, I realize that perhaps is a good thing to have a row identifier in it, so I try

clapidus=> alter table tickets add column rid serial;
NOTICE: ALTER TABLE will create implicit sequence "tickets_rid_seq" for SERIAL column "tickets.rid"
ERROR: adding columns with defaults is not implemented

So my next guess is to define a test table from scratch, this time with theserial field in place. Next I try the \copy:

clapidus=> create table test(rid serial, col_a text);
NOTICE: CREATE TABLE will create implicit sequence "test_rid_seq" for SERIAL column "test.rid"
CREATE TABLE
clapidus=> \d test
Table "test"
Attribute | Type | Modifier
-----------+---------+-------------------------------------------------------
rid | integer | not null default nextval('public.test_rid_seq'::text)
col_a | text |

clapidus=> \copy test from stdin
23 a record
45 another record
\.
clapidus=> select * from test ;
rid | col_a
-----+------------
23 | a record
45 | another record
(2 rows)
Now the first character from stdin is a tab, in a try to let the sequence come into action:

clapidus=> \copy test from stdin
still another record
\.
ERROR: invalid input syntax for integer: ""
PQendcopy: resetting connection

Grrr. Third attempt:

clapidus=> \copy test from stdin with null as 'NULL'
NULL still another one
\.
ERROR: null value for attribute "rid" violates NOT NULL constraint
PQendcopy: resetting connection
So? Is there a way to add the sequence to an existing table?
Or, alternatively, is there a way to issue a \copy command while letting the sequence fill in the serial field?

thanks in advance
cl.

Nov 11 '05 #1
3 16976
Do it in three steps...

CREATE SEQUENCE tickets_rid_seq;
ALTER TABLE tickets ADD COLUMN rid INTEGER;
ALTER TABLE tickets ALTER COLUMN rid SET DEFAULT nextval('tickets_rid_seq')


""Claudio Lapidus"" <cl******@hotmail.com> wrote in message
news:BA*************************@hotmail.com...
Hello

Now perhaps this is a bit dumb, but...

I just populated a new table via \copy. After that, I realize that perhaps
is a good thing to have a row identifier in it, so I try

clapidus=> alter table tickets add column rid serial;
NOTICE: ALTER TABLE will create implicit sequence "tickets_rid_seq" for
SERIAL column "tickets.rid"
ERROR: adding columns with defaults is not implemented

So my next guess is to define a test table from scratch, this time with the
serial field in place. Next I try the \copy:

clapidus=> create table test(rid serial, col_a text);
NOTICE: CREATE TABLE will create implicit sequence "test_rid_seq" for
SERIAL column "test.rid"
CREATE TABLE
clapidus=> \d test
Table "test"
Attribute | Type | Modifier
-----------+---------+------------------------------------------------------
-
rid | integer | not null default nextval('public.test_rid_seq'::text)
col_a | text |

clapidus=> \copy test from stdin
23 a record
45 another record
\.
clapidus=> select * from test ;
rid | col_a
-----+------------
23 | a record
45 | another record
(2 rows)
Now the first character from stdin is a tab, in a try to let the sequence
come into action:

clapidus=> \copy test from stdin
still another record
\.
ERROR: invalid input syntax for integer: ""
PQendcopy: resetting connection

Grrr. Third attempt:

clapidus=> \copy test from stdin with null as 'NULL'
NULL still another one
\.
ERROR: null value for attribute "rid" violates NOT NULL constraint
PQendcopy: resetting connection
So? Is there a way to add the sequence to an existing table?
Or, alternatively, is there a way to issue a \copy command while letting the
sequence fill in the serial field?

thanks in advance
cl.
Nov 11 '05 #2
"Claudio Lapidus" <cl******@hotmail.com> writes:
So? Is there a way to add the sequence to an existing table?
Sure. You have to break the SERIAL down to its component parts though.
Something like

CREATE SEQUENCE seq;
ALTER TABLE tab ADD COLUMN ser INTEGER;
UPDATE tab SET ser = nextval('seq'); -- this will take awhile
ALTER TABLE tab ALTER COLUMN ser SET DEFAULT nextval('seq');
ALTER TABLE tab ALTER COLUMN ser SET NOT NULL;
-- possibly also add a UNIQUE constraint

We haven't yet got round to supporting ADD COLUMN ... DEFAULT because
according to the SQL spec that implies doing the UPDATE to fill the
column values immediately, and that's just a chunk of code no one's
written yet.
Or, alternatively, is there a way to issue a \copy command while letting th=
e sequence fill in the serial field?


Yes, you have to list in the COPY command just the columns that are
actually being supplied in the input data. Columns not mentioned (like
the serial column) get filled from their defaults. I think this is
new in 7.3 ... it's pretty recent anyway.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #3
On Mon, 1 Sep 2003, Tom Lane wrote:
"Claudio Lapidus" <cl******@hotmail.com> writes:
So? Is there a way to add the sequence to an existing table?


Sure. You have to break the SERIAL down to its component parts though.
Something like

CREATE SEQUENCE seq;
ALTER TABLE tab ADD COLUMN ser INTEGER;
UPDATE tab SET ser = nextval('seq'); -- this will take awhile
ALTER TABLE tab ALTER COLUMN ser SET DEFAULT nextval('seq');
ALTER TABLE tab ALTER COLUMN ser SET NOT NULL;
-- possibly also add a UNIQUE constraint


For folks just starting out, you can also do it this way:

=>begin;
=>create table a (info text, date date);
CREATE TABLE
=> insert into a values ('abc','2003-04-03');
INSERT 1127459 1
=> create table b (info text, date date, id serial);
NOTICE: CREATE TABLE will create implicit sequence "b_id_seq" for SERIAL
column "b.id"
CREATE TABLE
=> insert into b (select * from a);
INSERT 1127468 1
=> select * from b;
info | date | id
------+------------+----
abc | 2003-04-03 | 1

=>drop table a;
DROP TABLE
=> alter table b rename to a;
ALTER TABLE
=>commit;
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: t_pet422 | last post by:
Hi, I've got a table in PostgreSql 7.3.2 that has a serial column as a primary key. I want to insert data into the table (inserting values for everything EXCEPT the serial column) and at the...
1
by: RC | last post by:
I have an Access 2002 database with many tables and forms (but just to keep things simple, let's say the DB has one Table "Table1" and one Form "Form1"). I have managed to cobble together so much...
5
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
12
by: Prabu Subroto | last post by:
Dear my friends... I am using postgres 7.4 and SuSE 9.1. I want to use auto_increment as on MySQL. I look up the documentation on www.postgres.com and I found "serial" . But I don't know...
15
by: Robby Russell | last post by:
I am trying to track down a method of determining what a sequence name is for a SERIAL is in postgresql. For example, CREATE TABLE foo (id SERIAL PRIMARY KEY NOT NULL, bar TEXT); \d foo...
3
by: tai.cabrera | last post by:
What I would like to do is input a range of serial numbers in a form and have that range populate in the table without me having to put them in one at a time manually. The numbers do not exist yet,...
3
by: george.e.sullivan | last post by:
Hello all, I have three tables Employess, Equipment, and Software. I have tried without success to figure out how to show a single Equipment Serial Number show all the software installed on that...
1
by: access345 | last post by:
I am trying to streamline my forms by just having one form that does two functions. My form is called frmInformation my textbox is called txtSerialNumber. On the form I have that Data entry function...
1
by: TBass | last post by:
After successfully creating a connection to an Access Database file on my computer, I try to add a new row. Everything seems to go fine, but when I open the file in Access, the row isn't there. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.