Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2004, 04:28 AM
Silver Machine Silver Machine is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: North East England
Posts: 46 Silver Machine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Add one value per row

I want to increment a code +1 every time someone inserts a new row. I was looking at something like:


insert into Table 1 values ((Code='last insert'+1) 'CD003', 'CT', '2003-05-18')((Code='LAST INSERT+1,)----etc.

But it won't work Syntax error. or something like

insert into Table 1 values (SELECT COUNT(Code) FROM
Table 1+1 ) ------etc

cannot quite get my head around how to use them with an insert statement, any ideas please, Chris

Reply With Quote
  #2  
Old January 11th, 2004, 05:07 AM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,005 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 11 h 33 m 53 sec
Reputation Power: 67
Use a sequence:

insert into table1 values (table1_sequence.nextval, 'CD003', 'CT');

Reply With Quote
  #3  
Old January 11th, 2004, 06:14 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,913 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 24 m 34 sec
Reputation Power: 1018
which database?

for oracle, use a sequence

for mysql, use an auto_increment field

for access, use an autonumber field

for sql server, use an identity field

et cetera

Reply With Quote
  #4  
Old January 11th, 2004, 07:34 AM
Silver Machine Silver Machine is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: North East England
Posts: 46 Silver Machine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I have been looking at auto_increment in the manual and from what I understand it needs to be created with the table or inserted with and alter table statement which I cannot do. Is this correct?
I have been working on:

Select code AS last_code
from Guide
ORDER BY code DESC LIMIT 1
insert into Guide values ('last_code+1', 'CD003', 'KN', '2003-05-14');

the select statement gives me the last number I want when run alone and the insert bit works with out the 'last_code' bit I have been looking for a way to put them together and fiddling with brackets with no sucess. I am sure I saw somthing about doing this once but I can not remember where. I am on MySQL, Per, Apache. Any ideas or pointers where to look please, Chris

Reply With Quote
  #5  
Old January 11th, 2004, 08:13 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,913 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 24 m 34 sec
Reputation Power: 1018
Quote:
Originally posted by Silver Machine
... it needs to be created with the table or inserted with and alter table statement which I cannot do.
yes, those are basically the only options -- declared with CREATE TABLE or later with ALTER TABLE

if you are going to SELECT and then INSERT, you need something to hold the value, like a programming language variable

in you example, you have quotes around 'last_code+1', which makes it a string

you'll want to try something like $lastcode+1 (sorry i don't know perl)
__________________
r937.com | rudy.ca

Reply With Quote
  #6  
Old January 11th, 2004, 08:50 AM
Silver Machine Silver Machine is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: North East England
Posts: 46 Silver Machine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Yes thats the lines I was thinking of, but I think I will be on forever debugging! Chris

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Add one value per row


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT