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

one to one relationship

I created 2 tables with one to one relationship. if I add a record in
table A, how does table B record get created? Does SQL do this
automatically because it is one to one relationship? or do I need to
create a trigger? if i need a trigger, how do I get the ID of new
record to create the same ID in table B?
thanks for any help.
Joe Klein

Jul 23 '05 #1
7 10883
rj****@hotmail.com wrote:
I created 2 tables with one to one relationship. if I add a record in
table A, how does table B record get created? Does SQL do this
automatically because it is one to one relationship? or do I need to
create a trigger? if i need a trigger, how do I get the ID of new
record to create the same ID in table B?
thanks for any help.
Joe Klein


From the standpoint of pure design: What justification is there for
two tables with a 1:1 relationship?
--
Daniel A. Morgan
http://www.psoug.org
da******@x.washington.edu
(replace x with u to respond)
Jul 23 '05 #2
I understand what you are getting at- I can keep the data all in a
single table, but the 2nd table will have fields related to one aspect
of the application, which will be easier to maintain.

Jul 23 '05 #3
If you change the design to a single table you could create a view
containing the fields related to the one aspect of the application.

<rj****@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I understand what you are getting at- I can keep the data all in a
single table, but the 2nd table will have fields related to one aspect
of the application, which will be easier to maintain.

Jul 23 '05 #4
that makes sense. one more question, why have 1:1 if it is not very
pratical?

Jul 23 '05 #5
(rj****@hotmail.com) writes:
I created 2 tables with one to one relationship. if I add a record in
table A, how does table B record get created?
You insert it, one way or another.
Does SQL do this automatically because it is one to one relationship?
No. SQL Server can't really see what you are doing. It can tell if the
relation is one-to-one, or one-to-optional-one.
or do I need to create a trigger? if i need a trigger, how do I get the
ID of new record to create the same ID in table B?


Trigger is one way. That presumes that you have information enough in
the trigger to insert that other row. Copying the ids of the inserted
rows in A to table B is a trivial matter:

CREATE TRIGGER A_tri ON A FOR INSERT AS
INSERT B (keycol)
SELECT keycol FROM inserted

Note that a trigger fires once per statement, and must be able to
handle multi-row operatons. But this only possible if all rows in B
beside the key colunm are nullable.

(If you were silly enough to attribute the key column in B as IDENTITY,
you need to change that.)

You can also issue to separate INSERT statments, one for each table. That
would be necessary if you have data for both tables ar this point.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
thanks.

Jul 23 '05 #7
Stu
Actually, it's more practical than theoretical. While a good design
usually reduces the need for 1:1, there may be situations where the
seperation of data is warranted. For example, how many of you have
seen this:

Warning: The table 'blah' has been created but its maximum row size
(10662) exceeds the maximum number of bytes per row (8060). INSERT or
UPDATE of a row in this table will fail if the resulting row length
exceeds 8060 bytes.
From the Books OnLine (Create Table):


SQL Server can have as many as two billion tables per database and
1,024 columns per table. The number of rows and total size of the table
are limited only by the available storage. The maximum number of bytes
per row is 8,060. If you create tables with varchar, nvarchar, or
varbinary columns in which the total defined width exceeds 8,060 bytes,
the table is created, but a warning message appears. Trying to insert
more than 8,060 bytes into such a row or to update a row so that its
total row size exceeds 8,060 produces an error message and the
statement fails.

Granted, most designs won't have huge data columns, but you might. A
good design will eliminate 80% of your problems, while a great one will
eliminate 90%. You will still always have to deal with an exception to
your design at some point; a 1:1 join is a method of managing some of
those exceptions.

HTH,
Stu

Jul 23 '05 #8

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

Similar topics

0
by: Alcyone Oliveira | last post by:
------=_NextPart_000_000D_01C36572.F6480CE0 Content-Type: multipart/alternative; boundary="----=_NextPart_001_000E_01C36572.F6480CE0" ------=_NextPart_001_000E_01C36572.F6480CE0 Content-Type:...
0
by: Mike Sutton | last post by:
Please let me know what you think of this scheme I have come up with (not to imply that noone else has before me) for managing relationships. I have created an entities table with Individual and...
8
by: Andante.in.Blue | last post by:
Hello, I have just inherited a legacy Access 97 database. While going through it, I noticed something strange... its Relationships window (the one accessed by Tools --> Relationships) is almost...
2
by: Larry R Harrison Jr | last post by:
I have an Access 97 database with 2 tables that have a one-many relationship. I have a SQL statement in the "one" table which I want to execute and insert 7 records into the "many" table, and I...
7
by: francophone77 | last post by:
I deleted a relationship in the relationship view, but when I create a query that includes those two tables, the relationship reappears. When I go back to the relationship view there is NO...
0
by: Joey Lee | last post by:
Hi, I have created a dataset schema(*.xsd) which contain two element "InvoiceHeader" and "InvoiceDetail". I set the primary key of both the element. for example "InvoiceHeader" -> id and...
5
by: BrianDP | last post by:
This is a strange one-- I have an Access 2K database with a split front end/back end. There are two tables in the back end - RFile and RLine. There is a one to many relationship from...
1
by: BuckBaxter | last post by:
Recently had trouble creating a one-to-many relationship between Customer table and Order table. After drag & dropping the field CustomerNo (key) in Customer table to CustomerNo (foreign) in Order...
4
by: Phil Stanton | last post by:
Having spent ages sorting out the layout of my relationship window with about 60 tables and getting it all tidy and with enough of the tables visible to show the PK an FK relaionshipsOn a subseqent...
6
by: NicoleCartrette | last post by:
Going back to school is easier said than done.. This was posted to an older thread earlier but I don't think it got any attention. Your help is appreciated Professor requires we create a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.