Showing posts with label primary. Show all posts
Showing posts with label primary. Show all posts

Wednesday, March 7, 2012

Problem Creating One-To-Many Relationship

Hi guys,

I'm testing some stuff. I have two tables, table1 has a primary key and table2 only has a column. When I try to create a relationship between them, SQL Server complains.

Here's my code:

Alter Table Table1
Add Constraint FK_T1 Foreign Key (T1) References Table2(T1)

And the error:

There are no primary or candidate keys in the referenced table 'Table2' that match the referencing column list in the foreign key 'FK_T1'.

Thanks
DarkneonOK guys, I found what was wrong.

I should go and kill myself for posting this question, now that I realize how stupid it is :o

Saturday, February 25, 2012

Problem copying tables to another server with Primary and Foreign Key constrains

How can I copy a table with a Primary Key to another server's table of
the same name without receiving the following error: (Error at
Destination for Row number 489. Errors encountered so far in this
task:1.The statement has been terminated. Violation of PRIMARY KEY
constraint 'PK_INDIVIDUALS'. Cannot insert duplicate key in object
'INDIVIDUALS'.)
I want to copy about 20 interrelated cascaded tables with Primary and
Foreign Keys to another server's tables with the identical structure
and not receive the above error. I suggested dropping the tables and
recreating them, but my co-worker feel that this would not work because
of the interrelationship of the tables.
I tried to manually delete a table that is part of this group and
received Error 3726: Could not drop object 'dbo.INDIVIDUALS' because
it is referenced by a FOREIGN KEY constraint.
THE REASON FOR MY INQUIRY IS BECAUSE WE HAVE BEEN EXPERIENCING PROBLEM
WITH REPLICATION AND THESE TABLES ARE THE ARTICLES THAT WE BE
REPLICATED.
Any Suggestion?Some odd questions:
What field is the primary key? Is it the replication id? Is it an Identity
Field ? Some other field?
Instead of deleteing the target table you could try truncating it.
Your error says you are trying to load a duplicate key. The only answers are
Delete, Truncate, Drop or Remove Constraint.
What is the problem you are trying to solve? If it is just having a copy of
the database 'somewhere else', you could consider a backup and restore cycle.
--
Joseph R.P. Maloney, CSP,CCP,CDP
"war_wheelan@.yahoo.com" wrote:
> How can I copy a table with a Primary Key to another server's table of
> the same name without receiving the following error: (Error at
> Destination for Row number 489. Errors encountered so far in this
> task:1.The statement has been terminated. Violation of PRIMARY KEY
> constraint 'PK_INDIVIDUALS'. Cannot insert duplicate key in object
> 'INDIVIDUALS'.)
> I want to copy about 20 interrelated cascaded tables with Primary and
> Foreign Keys to another server's tables with the identical structure
> and not receive the above error. I suggested dropping the tables and
> recreating them, but my co-worker feel that this would not work because
> of the interrelationship of the tables.
> I tried to manually delete a table that is part of this group and
> received Error 3726: Could not drop object 'dbo.INDIVIDUALS' because
> it is referenced by a FOREIGN KEY constraint.
> THE REASON FOR MY INQUIRY IS BECAUSE WE HAVE BEEN EXPERIENCING PROBLEM
> WITH REPLICATION AND THESE TABLES ARE THE ARTICLES THAT WE BE
> REPLICATED.
> Any Suggestion?
>|||What field is the primary key? Is it the replication id? Is it an
Identity Field ? Some other field? THE PRIMARY KEY VARIES FROM TABLE
TO TABLE.
Instead of deleteing the target table you could try truncating it. I
AM NOT A T-SQL PROGRAMMER SO IN NON PROGRAMMING TERMS - CAN I DROP THE
KEYS ON THE DESTINATION TABLE(S) I.E. PRIMARY AND FOREIGN KEYS THEN
COPY THE NEW TABLES TO THE DESTINATION? WOULD THIS RE-ESTABLISH THE
ORIGINAL PRIMARY AND FOREIGN KEYS WITHOUT CORRUPTING THE
DATABASE/TABLES AND THEIR RELATIONSHIPS?
Your error says you are trying to load a duplicate key. The only
answers are Delete, Truncate, Drop or Remove Constraint. SAME QUESTION
AS ABOVE, BUT PERHAPS THE REMOVE CONTRAINT OPTION WOULD WORK. NEW
QUESTION: WOULD SQL LET ME REMOVE A CONTRAINT WITH INTER-RELATIONSHIPS
TO OTHER TABLES OR WOULD IT FAIL BECAUSE OF THE INTER-RELATIONSHIPS.
What is the problem you are trying to solve? If it is just having a
copy of the database 'somewhere else', you could consider a backup and
restore cycle. THE PROLEM IS THAT THESE TABLES WOULD BE REPLICATE IF
REPLICATION WERE WORKING. REPLICATION HAS NOT BEEN WORKING FOR ABOUT A
MONTH SO THE PRIMARY AND SECONDARY DATABASES ARE OUT OF SYNC. IF THE
PRIMARY SERVER GOES DOWN WE WOULD BE MISSING A MONTHS WORTH OF CHANGES.
ERGO BACK TO MY ORIGINAL QUESTION (I want to copy about 20
interrelated cascaded tables with Primary and Foreign Keys to another
server's tables with the identical structure and not receive the above
error.
Thanks for you response. I am trying to get suggestions for my PART
TIME T-SQL programmer.

Monday, February 20, 2012

Problem converting rows to a string

I try to accomplish the following:

I have two tables which are connected via a third table (N:N
relationship):

Table 1 "Locations"
LocationID (Primary Key)

Table 2 "Specialists"
SpecialistID (Primary Key)
Name (varchar)

Table 3 "SpecialistLocations"
SpecialistID (Foreign Key)
LocationID (Foreign Key)
(both together are the primary key for this table)

Issuing the following command

SELECT
L.LocationID , S.[Name]
FROM
Locations AS L
LEFT JOIN SpecialistLocations AS SL ON P.PlaceID = SL.LocationID
LEFT JOIN Specialists AS S ON SL.SpecialistID = S.SpecialistID

results in the following table:

LocationID | Name
1Specialist 1
1Specialist 2
2Specialist 3
2Specialist 4
3Specialist 1
4Specialist 4

Now my problem: I would like to have the following output:

LocationID | Names
1Specialist 1, Specialist 2
2Specialist 3, Specialist 4
3Specialist 1
4Specialist 4

...which is grouping by LocationID and concatenating the specialist
names.
Any idea on how to do this?

Thank you very much,
Dennis(dnsstaiger@.gmx.net) writes:
> Now my problem: I would like to have the following output:
> LocationID | Names
> 1 Specialist 1, Specialist 2
> 2 Specialist 3, Specialist 4
> 3 Specialist 1
> 4 Specialist 4
> ...which is grouping by LocationID and concatenating the specialist
> names.
> Any idea on how to do this?

This is one of the rare cases where you need to set up a cursor and
iterate. In SQL 2000 there is no defined way to do this. (There is
a shortcut, but it relies on undefined behaviour, so I don't recommend it.)

In SQL 2005, currently in beta, the story is different. There you
actually have a way to this in a set-based statement, although the
syntax is somewhat bewildering. (It's actually a by-product, of all
the XML stuff they thrown in.)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||I typically do things like this in my application code, i.e. while the
locationid is the same, keep tacking values onto the other column's
display in a comma delim format.|||pb648174 (google@.webpaul.net) writes:
> I typically do things like this in my application code, i.e. while the
> locationid is the same, keep tacking values onto the other column's
> display in a comma delim format.

Yes, that is also a very common advice. But people insists on asking
about how doing this in SQL, that I've given up telling them to use
application code. (And sometimes the application is not any more
sophisticated than Query Analyzer.)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Can you post the SQL 2005 code that will do this? I've been hoping SQL
2005 would have an aggregate function for strings that would turn it
into a delimited string.|||pb648174 (google@.webpaul.net) writes:
> Can you post the SQL 2005 code that will do this? I've been hoping SQL
> 2005 would have an aggregate function for strings that would turn it
> into a delimited string.

Sure, here it is:

select CustomerID,
substring(OrdIdList, 1, datalength(OrdIdList)/2 - 1)
-- strip the last ',' from the list
from
Customers c cross apply
(select convert(nvarchar(30), OrderID) + ',' as [text()]
from Orders o
where o.CustomerID = c.CustomerID
order by o.OrderID
for xml path('')) as Dummy(OrdIdList)
go

This gives you an output like:

ALFKI 10643,10692,10702,10835,10952,11011
ANATR 10308,10625,10759,10926
ANTON 10365,10507,10535,10573,10677,10682,10856

Now, I did definitely come with this on my own, but I got it from one
of the SQL Server developers.

The part that produces the comma separated list, is the text() function,
which is activated by the XML PATH('') at the bottom. The real point
of text() is probably not to produce a comma separated list, but it's
possible to do it.

Then then comma-separated list is combined with Customers through
CROSS APPLY. APPLY is another operator I have not fully digested
yet, but you use it when you want to call a table-valued functions
with parameters from other columns in the query; something you can't
do in SQL 2000.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp