473,408 Members | 1,854 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,408 software developers and data experts.

newby - AUTOINCREMENT problem.

Hi,

I'm enclosed a snippet of test code which highlights my problem. The Stored
procedure insertValue should insert text into the parent, then insert other
text into the child table but the 2 tables should auto increment in sync
(i.e. so that they both end up with the same id numbers). I've tried taking
the auto increment out of the child table but then I don't know how to get
the right parent id into the child table.

Any advice appreciated - this is my first database, so I'm just in the
learning process really. Code follows:

CREATE TABLE Parent
(id INTEGER DEFAULT AUTOINCREMENT,
parenttext VARCHAR(16),
PRIMARY KEY (id))!

CREATE TABLE Child
(childID INTEGER INTEGER DEFAULT AUTOINCREMENT,
childtext VARCHAR(16),
FOREIGN KEY (childid) REFERENCES Parent(id),
PRIMARY KEY (childID))!

CREATE PROCEDURE insertValues(in p VARCHAR(16), in c VARCHAR(16))
BEGIN
insert into parent (parenttext) values (p);
insert into child (childtext) values (c);
END!

call insertValues('from parent', 'from child')!
select * from parent, child where parent.id = child.childid!
Nov 5 '05 #1
5 9660
In message <43**********@mk-nntp-2.news.uk.tiscali.com>, Mary Walker
<12*@123.com> writes
Hi,

I'm enclosed a snippet of test code which highlights my problem. The Stored
procedure insertValue should insert text into the parent, then insert other
text into the child table but the 2 tables should auto increment in sync
(i.e. so that they both end up with the same id numbers). I've tried taking
the auto increment out of the child table but then I don't know how to get
the right parent id into the child table.

Any advice appreciated - this is my first database, so I'm just in the
learning process really. Code follows:


OK. The first piece of advice is don't rely on the autonumber to return
any particular value. There are ways that the values in the two tables
could get out of sync and it would be a PITA to fix.

There are two options that I could suggest.

One is that you create the parent record and then take the autonumber
value from there and explicitly insert it into the child table. Don't
use an autonumber in child table, use a simple integer instead. This is
the way I recommend that you do it.

The second method has limited applicability but can make things easier.
First redesign your child table with an integer key. Then create a query
that links the two tables. You can now update the query instead of
updating the two tables separately.


--
Bernard Peek
London, UK. DBA, Manager, Trainer & Author.

Nov 5 '05 #2
>> this is my first database, so I'm just in the learning process really. <<

Get a book on RDBMS and learn why an auto-increment can NEVER be a
relational key. Learn why a table name should be a collective or
plural name (unless there is only one row in the table). Learn way
"id" is too vague to be data element name. Learn why camelCase is a
bitch to read --hint: where does your eye jump when you see an
Uppercase letter?

I know you want to have a "Magical Universal Key" that will solve all
your design problems, without having to really think or learn anything.
What is your **real key** in your **real** problem?

Going to a Newsgroup to get what usually takes a few YEARS of college
and experience does not work.

Nov 6 '05 #3

"--CELKO--" <jc*******@earthlink.net> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
this is my first database, so I'm just in the learning process really.
<<


Get a book on RDBMS and learn why an auto-increment can NEVER be a
relational key. Learn why a table name should be a collective or
plural name (unless there is only one row in the table). Learn way
"id" is too vague to be data element name. Learn why camelCase is a
bitch to read --hint: where does your eye jump when you see an
Uppercase letter?

I know you want to have a "Magical Universal Key" that will solve all
your design problems, without having to really think or learn anything.
What is your **real key** in your **real** problem?

Going to a Newsgroup to get what usually takes a few YEARS of college
and experience does not work.


LOL. Thanks very much you for reply. I have actually learnt a lot by
simply reading your reply - but I'll buy the book anyway :-)
Nov 6 '05 #4
In your book 'sql for smarties' you have an example with a table named
'Warehouse' - if you where to follow your own naming standards then it
should be called 'Warehouses'.

'id' is fine within the context of the table, if the column belongs to the
table 'Child' then its obviously Child.id.

What key would you use for a message board? Would you still key it on
subject and posting date or like the rest of us and how NNTP works, create a
guid with the domain?

The auto-number (IDENTITY property) is usually used as a artificial (or
surrogate) key, there is no such thing as a relational key - I think you
mean 'natural key', see: http://en.wikipedia.org/wiki/Natural_key.

Quoting from that aritcle...

"
The main disadvantage of choosing a natural key is that it may need to
change if your business requirements change. For example, if you have chosen
CustomerNumber as the primary key for a customer, and, subsequently,
CustomerNumber becomes alphanumeric instead of numeric, then as well as
changing the type of the column, you will need to make changes to all other
tables where CustomerNumber is used as a foreign key.

Retrieved from "http://en.wikipedia.org/wiki/Natural_key"

"

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"--CELKO--" <jc*******@earthlink.net> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
this is my first database, so I'm just in the learning process really.
<<


Get a book on RDBMS and learn why an auto-increment can NEVER be a
relational key. Learn why a table name should be a collective or
plural name (unless there is only one row in the table). Learn way
"id" is too vague to be data element name. Learn why camelCase is a
bitch to read --hint: where does your eye jump when you see an
Uppercase letter?

I know you want to have a "Magical Universal Key" that will solve all
your design problems, without having to really think or learn anything.
What is your **real key** in your **real** problem?

Going to a Newsgroup to get what usually takes a few YEARS of college
and experience does not work.

Nov 6 '05 #5
Hi Mary,

The SCOPE_IDENTITY() will return the last inserted IDENTITY value for that
connection.

insert into parent (parenttext) values (p);
print scope_identity() -- gives the id from parent

insert into child (childtext) values (c);
print scope_identity() -- gives id from child

Does that help?

Tony.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"Mary Walker" <12*@123.com> wrote in message
news:43**********@mk-nntp-2.news.uk.tiscali.com...
Hi,

I'm enclosed a snippet of test code which highlights my problem. The
Stored procedure insertValue should insert text into the parent, then
insert other text into the child table but the 2 tables should auto
increment in sync (i.e. so that they both end up with the same id
numbers). I've tried taking the auto increment out of the child table but
then I don't know how to get the right parent id into the child table.

Any advice appreciated - this is my first database, so I'm just in the
learning process really. Code follows:

CREATE TABLE Parent
(id INTEGER DEFAULT AUTOINCREMENT,
parenttext VARCHAR(16),
PRIMARY KEY (id))!

CREATE TABLE Child
(childID INTEGER INTEGER DEFAULT AUTOINCREMENT,
childtext VARCHAR(16),
FOREIGN KEY (childid) REFERENCES Parent(id),
PRIMARY KEY (childID))!

CREATE PROCEDURE insertValues(in p VARCHAR(16), in c VARCHAR(16))
BEGIN
insert into parent (parenttext) values (p);
insert into child (childtext) values (c);
END!

call insertValues('from parent', 'from child')!
select * from parent, child where parent.id = child.childid!

Nov 6 '05 #6

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

Similar topics

0
by: Marko Maehner | last post by:
Hi, I have a strange problem with my xml file. In the schema of this xml file I have set one column to autoincrement. When I enter the data in my xml file directly, the autoincrement-column gets...
10
by: Fred Nelson | last post by:
Hi: I have programmed in VB.NET for about a year and I'm in the process of learing C#. I'm really stuck on this question - and I know it's a "newby" question: In VB.NET I have several...
0
by: Neil | last post by:
Hi, I'm getting some strange results using the autoincrement column on my datatable. I'm populating a datatable with data from my database and displaying this in a datagrid. The first time I get...
0
by: Agnes | last post by:
I got master-detail relationship grid. in my detail grid, i set the column ..autoincrement. that grid contains serveral column.1st column is description. 1)the user input 2rows. and then press'...
6
by: Dennis | last post by:
I have set a DataTable and one of the columns I set "AutoIncrement" to True. I then populate the Table by setting the columns to values then add the row to the table. I inadverently set the...
1
by: Sam | last post by:
Hi all I have a simple datagrid form which has 4 columns. The first one is used just like a key and the other threes allow user to enter numbers. The problem that I am having is that when I...
6
by: Michael | last post by:
I am trying to create an access database within Net 2003 using the ADOX library which works fine except when I try to add the AutoIncrement property to the ContactId column. I am experiencing a...
3
by: MP | last post by:
context: vb6/ ado / .mdb format / jet 4.0 (not using Access - ADO only) - creating tables via ADO (don't have access) - all tables have a primary key (PK) - many of the PK will become FK(Foreign...
4
by: Tim | last post by:
Hello All, I could use some help on an error that is just now popping it's head up. Seems that the autoincrement numeric has hit 32,767. The autoincrement is used in various locations in the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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.