dbTalk Databases Forums  

To laugh or cry...

comp.databases.theory comp.databases.theory


Discuss To laugh or cry... in the comp.databases.theory forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Cimode
 
Posts: n/a

Default To laugh or cry... - 07-12-2009 , 07:27 PM






This is an extract from a board forum where I regularly participate to
try to help other fellows about SQL. I have to admit that by the end
of this thread the nature of the questions asked did make me feel like
crying. In this thread the questionner is called al4629740 and the
answerer is called momi_sabag. I let you judge for yourself.

Quote:
Questionner
Relate data in SQL
Question:
I have tables A and B. There are two columns in table A, that I
would like to have also in Table B. They need to relate. So if I
change the value in Table A, then Table B receives the change.

Is this called a relational table. How do I set that up in SQL Server
2005?
Quote:
momi_sabag:
you can do this by setting up a foreign key from table b to table a
and have that foreign key defined with update cascade

this means that each time the two columns in table a are updated, the
same update will be applied to the matching rows in table b

alter table tableB add constraint myconstraint foreign key(col1, col2)
references tablea (col1, col2) on update cascade

Quote:
al4629740:
Can this work between databases? Or does it only work within the
tables of one database?

Quote:
momi_sabag:
just tryif it does not work, you can create a simple trigger:

create trigger myTrigger on db1.tab1 after update as

update t2
set cola = t1.cola, colb = t1.colb
from updated t1 join db2.tab2 t2 on t1.id = t2.id

Quote:
al4629740:
What is "myconstraint"? What goes there?

Quote:
al4629740:
Maybe you can show me
:
My first table is frmProjectedBudget
Second table is frmProjectedBudget2

I need column "Last Name" "First Name in the first table to replicate
over into the second table.


Is this correct:

alter table frmProjectedBudget2 add constraint myconstraint foreign key
(Last Name, First Name) references frmProjectedBudget (Last Name,
First Name) on update cascade

Quote:
momi_sabag:
you are correct
myConstraint is just the name you give to the constraint, you can put
there what ever you want
just make sure you surround the column names with [] since you have a
blank in the name
alter table frmProjectedBudget2 add constraint myconstraint foreign key
([Last Name], [First Name]) references frmProjectedBudget ([Last
Name], [First Name]) on update cascade

Quote:
al4629740:
I get the following error:

Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table
'frmProjectedBudget' that match the referencing column list in the
foreign key 'myconstraint'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

what is wrong with my tables?

Quote:
momi_sabag:
you need to have either a primary key or a unique index defined on
columns (first_name,last_name) in frmProjectedBudget

Quote:
al4629740:
How does a relational database tables work? If I enter one data in
First Name on the Projected Budget table, does it automatically appear
in the other table?

Reply With Quote
  #2  
Old   
Mark D Powell
 
Posts: n/a

Default Re: To laugh or cry... - 07-14-2009 , 08:08 AM






On Jul 12, 8:27*pm, Cimode <cim... (AT) hotmail (DOT) com> wrote:
Quote:
This is an extract from a board forum where I regularly participate to
try to help other fellows about SQL. *I have to admit that by the end
of this thread the nature of the questions asked did make me feel like
crying. *In this thread the questionner is called al4629740 and the
answerer is called momi_sabag. *I let you judge for yourself.

Questionner

Relate data in SQL
Question:
I have tables A and B. * There are two columns in table A, that I
would like to have also in Table B. * They need to relate. *So if I
change the value in Table A, then Table B receives the change.

Is this called a relational table. *How do I set that up in SQL Server
2005?> momi_sabag:

you can do this by setting up a foreign key from table b to table a
and have that foreign key defined with update cascade

this means that each time the two columns in table a are updated, the
same update will be applied to the matching rows in table b

alter table tableB add constraint myconstraint foreign key(col1, col2)
references tablea (col1, col2) on update cascade

al4629740:

Can this work between databases? *Or does it only work within the
tables of one database?

momi_sabag:

just tryif it does not work, you can create a simple trigger:

create trigger myTrigger on db1.tab1 after update as

update t2
set cola = t1.cola, colb = t1.colb
from updated t1 join db2.tab2 t2 on t1.id = t2.id

al4629740:

What is "myconstraint"? *What goes there?

al4629740:

Maybe you can show me
:
My first table is frmProjectedBudget
Second table is frmProjectedBudget2

I need column "Last Name" "First Name in the first table to replicate
over into the second table.

Is this correct:

alter table frmProjectedBudget2 add constraint myconstraint foreign key
(Last Name, First Name) references frmProjectedBudget (Last Name,
First Name) on update cascade

momi_sabag:

you are correct
myConstraint is just the name you give to the constraint, you can put
there what ever you want
just make sure you surround the column names with [] since you have a
blank in the name
alter table frmProjectedBudget2 add constraint myconstraint foreign key
([Last Name], [First Name]) references frmProjectedBudget ([Last
Name], [First Name]) on update cascade

al4629740:

I get the following error:

Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table
'frmProjectedBudget' that match the referencing column list in the
foreign key 'myconstraint'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

what is wrong with my tables?

momi_sabag:

you need to have either a primary key or a unique index defined on
columns (first_name,last_name) in frmProjectedBudget

al4629740:

How does a relational database tables work? *If I enter one data in
First Name on the Projected Budget table, does it automatically appear
in the other table?

LOL, what would really be scary is if the OP's job title is system
architect.

IMHO -- Mark D Powell --

Reply With Quote
  #3  
Old   
TroyK
 
Posts: n/a

Default Re: To laugh or cry... - 07-16-2009 , 02:12 PM



On Jul 14, 8:08*am, Mark D Powell <Mark.Pow... (AT) eds (DOT) com> wrote:
Quote:
On Jul 12, 8:27*pm, Cimode <cim... (AT) hotmail (DOT) com> wrote:





This is an extract from a board forum where I regularly participate to
try to help other fellows about SQL. *I have to admit that by the end
of this thread the nature of the questions asked did make me feel like
crying. *In this thread the questionner is called al4629740 and the
answerer is called momi_sabag. *I let you judge for yourself.

Questionner

Relate data in SQL
Question:
I have tables A and B. * There are two columns in table A, that I
would like to have also in Table B. * They need to relate. *So if I
change the value in Table A, then Table B receives the change.

Is this called a relational table. *How do I set that up in SQL Server
2005?> momi_sabag:

you can do this by setting up a foreign key from table b to table a
and have that foreign key defined with update cascade

this means that each time the two columns in table a are updated, the
same update will be applied to the matching rows in table b

alter table tableB add constraint myconstraint foreign key(col1, col2)
references tablea (col1, col2) on update cascade

al4629740:

Can this work between databases? *Or does it only work within the
tables of one database?

momi_sabag:

just tryif it does not work, you can create a simple trigger:

create trigger myTrigger on db1.tab1 after update as

update t2
set cola = t1.cola, colb = t1.colb
from updated t1 join db2.tab2 t2 on t1.id = t2.id

al4629740:

What is "myconstraint"? *What goes there?

al4629740:

Maybe you can show me
:
My first table is frmProjectedBudget
Second table is frmProjectedBudget2

I need column "Last Name" "First Name in the first table to replicate
over into the second table.

Is this correct:

alter table frmProjectedBudget2 add constraint myconstraint foreign key
(Last Name, First Name) references frmProjectedBudget (Last Name,
First Name) on update cascade

momi_sabag:

you are correct
myConstraint is just the name you give to the constraint, you can put
there what ever you want
just make sure you surround the column names with [] since you have a
blank in the name
alter table frmProjectedBudget2 add constraint myconstraint foreign key
([Last Name], [First Name]) references frmProjectedBudget ([Last
Name], [First Name]) on update cascade

al4629740:

I get the following error:

Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table
'frmProjectedBudget' that match the referencing column list in the
foreign key 'myconstraint'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

what is wrong with my tables?

momi_sabag:

you need to have either a primary key or a unique index defined on
columns (first_name,last_name) in frmProjectedBudget

al4629740:

How does a relational database tables work? *If I enter one data in
First Name on the Projected Budget table, does it automatically appear
in the other table?

LOL, what would really be scary is if the OP's job title is system
architect.

IMHO -- Mark D Powell --- Hide quoted text -

- Show quoted text -
No - what would be really _common_ is if the OP's job title is system
architect.

Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.