Thread: Auto increment
When creating a table how do you make an auto incrementing field? I'm using: Create table hosts ( id int not null unique primary key, ip char(16), hostname text ); Is there a way to make the host.id field automagicaly increment? ***PRIVILEGED & CONFIDENTIAL*** Unless expressly stated otherwise, this message (and any attachment(s) thereto) is confidential and may be privileged. It is intended for the addressee(s) only. If you are not an addressee, any disclosure or copying of the contents of this e-mail or any action taken (or not taken) in reliance on it is strictly prohibited. If you are not an addressee, please inform sender immediately and delete this message from your system.
Am Die, 2003-05-27 um 19.37 schrieb Fontenot, Paul: > When creating a table how do you make an auto incrementing field? I'm > using: > > Create table hosts ( > id int not null unique primary key, > ip char(16), > hostname text > ); > > Is there a way to make the host.id field automagicaly increment? id SERIAL PRIMARY KEY will be translated into int with auto value automatically P.S.: Primary Keys are automatically not null HTH -- e-Trolley Sayegh & John, Nabil Sayegh Tel.: 0700 etrolley /// 0700 38765539 Fax.: +49 69 8299381-8 PGP : www.e-trolley.de
On Tuesday 27 May 2003 10:37 am, Fontenot, Paul wrote: > When creating a table how do you make an auto incrementing field? I'm > using: > > Create table hosts ( > id int not null unique primary key, > ip char(16), > hostname text > ); > > Is there a way to make the host.id field automagicaly increment? > > ***PRIVILEGED & CONFIDENTIAL*** > Unless expressly stated otherwise, this message (and any attachment(s) > thereto) is confidential and may be privileged. It is intended for the > addressee(s) only. If you are not an addressee, any disclosure or > copying of the contents of this e-mail or any action taken (or not > taken) in reliance on it is strictly prohibited. If you are not an > addressee, please inform sender immediately and delete this message from > your system. > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org use the 'serial' field type. e.g. create table hosts ( id serial, ip char(16), hostname text ); insert into hosts (ip,hostname) values ('127.0.0.1','localhost'); -doug ps - you also might want to use 'cidr' instead of char for the ip field.
Take a look at the SERIAL data type. http://www.postgresql.org/docs/view.php?version=7.3&idoc=0&file=datatype.htm l adam > When creating a table how do you make an auto incrementing field? I'm > using: > > Create table hosts ( > id int not null unique primary key, > ip char(16), > hostname text > ); > > Is there a way to make the host.id field automagicaly increment? > > ***PRIVILEGED & CONFIDENTIAL*** > Unless expressly stated otherwise, this message (and any attachment(s) > thereto) is confidential and may be privileged. It is intended for the > addressee(s) only. If you are not an addressee, any disclosure or > copying of the contents of this e-mail or any action taken (or not > taken) in reliance on it is strictly prohibited. If you are not an > addressee, please inform sender immediately and delete this message from > your system. > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
create sequence(foo_sequence); create table foo (mysequence int not null default nextval('foo_sequence'), somedata text); See "create sequence" for options like start value and step. Note: sequences are used to create unique values - not to create sequences without gaps. A sequence number grabbed in a transaction that is rolled back will leave the sequence incremented. Cheers, Steve On Tuesday 27 May 2003 10:37 am, Fontenot, Paul wrote: > When creating a table how do you make an auto incrementing field? I'm > using: > > Create table hosts ( > id int not null unique primary key, > ip char(16), > hostname text > ); > > Is there a way to make the host.id field automagicaly increment? > > ***PRIVILEGED & CONFIDENTIAL*** > Unless expressly stated otherwise, this message (and any attachment(s) > thereto) is confidential and may be privileged. It is intended for the > addressee(s) only. If you are not an addressee, any disclosure or > copying of the contents of this e-mail or any action taken (or not > taken) in reliance on it is strictly prohibited. If you are not an > addressee, please inform sender immediately and delete this message from > your system. > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org