Showing posts with label relationship. Show all posts
Showing posts with label relationship. Show all posts

Thursday, March 29, 2012

create table with recursive relationship

I am fairly new to SQL and I am currently trying to create
a SQL table (using Microsoft SQL) that has a recursive
relationship, let me try to explain:

I have a piece of Data let's call it "Item" wich may again contain one
more "Items". Now how would I design a set of SQL Tables that are
capable of storing this information?

I tried the following two approaches:

1.) create a Table "Item" with Column "ItemID" as primary key, some
colums for the Data an Item can store and a Column "ParentItemID". I
set a foreign key for ParentItemID wich links to the primarykey
"ItemID" of the same table.

2.) create separate Table "Item_ParentItem" that stores
ItemID-ParentItemID-pairs. Each column has a foreign key linked to
primary key of the "Item" Column "ItemID".

In both approaches when I try to delete an Item I get an Exception
saying that the DELETE command could not be executed because it
violates a COLUMN REFERENCE constraint. The goal behind these FK_PK
relations is is that when an Item gets deleted, all childItems should
automatically be deleted recursively.

How is this "standard-problem" usually solved in sql? Or do I inned to
implement the recursive deletion myself using stored
procedures or something ?You can get away with the first approach. However, you may not use ON
DELETE CASCADE. Rather, you are looking at a trigger that can manage this.

--
Tom

----------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
"Robert Ludig" <schwertfischtrombose@.gmx.de> wrote in message
news:1147782885.171670.231170@.i40g2000cwc.googlegr oups.com...
I am fairly new to SQL and I am currently trying to create
a SQL table (using Microsoft SQL) that has a recursive
relationship, let me try to explain:

I have a piece of Data let's call it "Item" wich may again contain one
more "Items". Now how would I design a set of SQL Tables that are
capable of storing this information?

I tried the following two approaches:

1.) create a Table "Item" with Column "ItemID" as primary key, some
colums for the Data an Item can store and a Column "ParentItemID". I
set a foreign key for ParentItemID wich links to the primarykey
"ItemID" of the same table.

2.) create separate Table "Item_ParentItem" that stores
ItemID-ParentItemID-pairs. Each column has a foreign key linked to
primary key of the "Item" Column "ItemID".

In both approaches when I try to delete an Item I get an Exception
saying that the DELETE command could not be executed because it
violates a COLUMN REFERENCE constraint. The goal behind these FK_PK
relations is is that when an Item gets deleted, all childItems should
automatically be deleted recursively.

How is this "standard-problem" usually solved in sql? Or do I inned to
implement the recursive deletion myself using stored
procedures or something ?|||On 16 May 2006 05:34:45 -0700, Robert Ludig wrote:

>I am fairly new to SQL and I am currently trying to create
>a SQL table (using Microsoft SQL) that has a recursive
>relationship, let me try to explain:
>I have a piece of Data let's call it "Item" wich may again contain one
>more "Items". Now how would I design a set of SQL Tables that are
>capable of storing this information?
>
>I tried the following two approaches:
(snip)

Hi Robert,

I agree with Tom that the first approach is better than the first. But
there are also some radically different ways to store a recursive
relationship or hierarchy. One of the more popular variants is the
nested set model. It's not nearly as intuitive as the model you are
proposing, but it performs far superior in some scenario's.

Google for "Nested Set Model" if you want to know the details.

--
Hugo Kornelis, SQL Server MVP|||>> How is this "standard-problem" usually solved in sql? <<

Get a copy of TREES & HIERARCHIES IN SQL for several ways to model this
kind of problem.

>> Or do I inned to implement the recursive deletion myself using stored
procedures or something ? <<

No need for recursive procedural code if you use the nested sets model.
Younger programmers who learned HTML, XML, etc. find it to be
intuitive. Older programmers who grew up with pointer chains need to
adjust their mind-set.

Monday, March 19, 2012

create relationship with tables in a linked server

I need to create a relationship between a local table and tables on a
linked server. I used the design table wizard and selected the
relationship property wizard. In the reslationship property wizard,
the tables that I need to get the keys from in the linked server do not
show up. Is there a way to do this, or I simply don't have enough
permission to tables in the linked server. On the local server, the
Security tab of linked server property has Local Loging "sa", Remote
User "sa" and Remote Password "****". Thanks for your help.Hi

You can not create a relationship in this way, you are restricted to
definining FKs to tables in the same database. If you wish to force a
constraint between the local table and the remote one, you can write a
function that can be used in a check constraint or enforce it through
writing a trigger (which may be the faster and easier solution to
implement).

John

"js" <androidsun@.yahoo.com> wrote in message
news:1102725393.883680.211450@.z14g2000cwz.googlegr oups.com...
>I need to create a relationship between a local table and tables on a
> linked server. I used the design table wizard and selected the
> relationship property wizard. In the reslationship property wizard,
> the tables that I need to get the keys from in the linked server do not
> show up. Is there a way to do this, or I simply don't have enough
> permission to tables in the linked server. On the local server, the
> Security tab of linked server property has Local Loging "sa", Remote
> User "sa" and Remote Password "****". Thanks for your help.

Create relationship programmatically

Hi,
I have metadata that stored my table structure and relationship. I would like to know is it possible to create table relationship programatically? Any sample?

Thank you


Something like this should work for you:

Code Snippet


CREATE TABLE MyTable
( Column1 int,
Column2 varchar(n)
FKCol3 int REFERENCES MyOtherTable(MyPKColumn)
)

For complete syntax, refer to Books Online, Topic: 'CREATE TABLE'

create relationship between tables in different databases??

Hi!
I have two databases. They represent two different systems. Now the two
systems will be merged in many ways. This means that there will now be
need for relations between tables in the two databases. I have existing
database diagrams for both, but is there a way to add tables in a
diagram from another database on the same sql server' Or should I just
have all the tables in one database' Any suggestions would be
appretiated!
Thanks
Henning :-)
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!You cannot diagram over several databases, nor can you define relationships
(foreign keys) cross
database. If you want to enforce referential integrity cross database, you c
an do it using triggers.
Or, as you say, another option is to put all objects into one database. Whet
her that is the best
thing to do or not is difficult to asses over a newsgroup post.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"kongsballa" <kongsgballa@.devdex.com> wrote in message news:eET3QYFBFHA.1396@.tk2msftngp13.p
hx.gbl...
> Hi!
> I have two databases. They represent two different systems. Now the two
> systems will be merged in many ways. This means that there will now be
> need for relations between tables in the two databases. I have existing
> database diagrams for both, but is there a way to add tables in a
> diagram from another database on the same sql server' Or should I just
> have all the tables in one database' Any suggestions would be
> appretiated!
> Thanks
> Henning :-)
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!|||Hi Tibor, and thanks for the quick response!
I guess there are a lot of pros and cons wether too have one or two
databases. The total size of the databases are no more than 500 MB, and
I would really like to see the day it reaches 1 GB. Having said that,
can you give me a hint or some place where this has been discussed'
Henning :-)
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!|||Hej Henning,
These are small databases, so that is IMO not a factor. I'd like to say as a
rule of thumb that if
the data is related, then go for one database. One practical thing to consid
er is backup. Having
several databases mean that backup of all data will not be a snapshot in tim
e (you first do one
database, then next). I'd search the Google archives (see my web-site) as th
is topic has been
discussed here before.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"kongsballa" <kongsgballa@.devdex.com> wrote in message news:uMeUPNGBFHA.3592@.TK2MSFTNGP09.p
hx.gbl...
> Hi Tibor, and thanks for the quick response!
> I guess there are a lot of pros and cons wether too have one or two
> databases. The total size of the databases are no more than 500 MB, and
> I would really like to see the day it reaches 1 GB. Having said that,
> can you give me a hint or some place where this has been discussed'
> Henning :-)
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!|||Tjna Tibor!
Thanks again. I will look around for information on the subject. Have a
nice day in Sweden!
Henning :-)
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!