LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2005, 04:20 PM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
PostgreSQL insert command


hello,
i'm having trouble inserting values into my database. I want create a table that will auto-increment everytime i insert a new row. this is the command i used to create myTable:
[code]
create table myTable (id serial, names text);
[code]

now when i try to insert a new record by doing:
Code:
insert into myTable values("myName");
i get an error saying it can't be parsed. but when i do:
Code:
insert into myTable values(0, "myName");
if works fine, but my question is----is there a way i can create a table that every time i create do an insert it will increment my id field, for example
Code:
insert into myTable values("myName");
i want my database to increment the id column by one and insert myName into my database.

thanks,

djgerbavor3
 
Old 01-31-2005, 04:42 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Which version of PGSQL are you using? In 7.4 and
newer it SHOULD work like that
Code:
insert into myTable (names) values("myName");
Alternatively try:
Code:
insert into myTable values(DEFAULT, "myName");

Cheers,
Tink
 
Old 01-31-2005, 04:55 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
I believe you need to entry the data as such. That way the id field will auto increment.
INSERT INTO myTable (names) VALUES ('myname');
 
Old 01-31-2005, 07:11 PM   #4
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
Quote:
I believe you need to entry the data as such. That way the id field will auto increment.
INSERT INTO myTable (names) VALUES ('mynames');
that worked fine, thanks, but why do you think i have to put names in parthenise?

thanks,

djgerbavor3
 
Old 01-31-2005, 07:31 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Std SQL:

INSERT INTO tablename
(
col1, col2, ..)
VALUES
(val1, val2, ..)
;


Note that if you do NOT specify col names, values will be inserted in in the order that the cols exist on the table... this is not reqd to be the order you used in the CREATE statement (although it usually is).
Basically, NEVER assume ... always specify. You can however skip cols that you do not supply values for.
HTH
 
Old 01-31-2005, 09:13 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
Quote:
but why do you think i have to put names in parthenise?
It just the command language syntax.

The following also works:
insert into myTable values(DEFAULT, "myName");

Last edited by michaelk; 01-31-2005 at 09:16 PM.
 
Old 02-01-2005, 01:31 PM   #7
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Originally posted by djgerbavore
that worked fine, thanks, but why do you think i have to put names in parthenise?
Because string can be everything. Imagine this:
INSERT INTO table VALUES ("one, two, three");
Without quotes it will be used as separate parameters. Note that you should in fact quote all values.
 
Old 02-01-2005, 02:09 PM   #8
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
I mean the actually column named name.


insert into table myTable (name) values("foo");
 
Old 02-01-2005, 03:06 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by djgerbavore
I mean the actually column named name.


insert into table myTable (name) values("foo");
So the database knows which column to insert the
value to ... you need to a) either specify ALL values
(including the WORD default for the automatic columns)
or b) tell it which columns you want to populate.


Cheers,
Tink
 
Old 02-01-2005, 07:27 PM   #10
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
thanks, i did that and it worked perfectly.


thanks again

djgerbavor3
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing postgresql in a VPS server - cannot find 'createlang' command jbsarma Linux - Newbie 2 08-18-2005 08:11 AM
sed command to insert tab? iluvatar Linux - Software 4 06-29-2004 01:56 AM
postgresql php insert problem BobNz Linux - General 3 05-19-2004 10:46 PM
PostgreSQL: executing a .SQL file from command line? J_Szucs Linux - Software 0 09-24-2003 11:49 PM
postgresql -odbc & postgresql-jdbc installation kjsubbu Linux - Software 0 06-19-2003 02:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration