Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

A script to create a relationship in SQL Server

Posted on 2009-02-21
5
Medium Priority
?
1,207 Views
Last Modified: 2012-05-06
Hi

I have been unable to find the sytax to create a relationship between two tables in SQL Server.  There is a good chance that i have a fundamental misunderstanding about relationships.

The relationship I need to create is not based on a FK.

I can create a foreign key relationship without issue.  And I can create a 'relationship' via the SQL Server Managemnt Studio by right clicking the table in design mode and choosing 'Relationships...'.  But i need to be able to script that type of relationship somehow.

Lastly, is there any difference between a Foreign Key relationship and a relationship that does not use a FK but a unique field.

Thanks
Andrew
0
Comment
Question by:agrogers
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1400 total points
ID: 23702285
let's explain several things:
* a INDEX is just a structure beside the table data, making a "lookup" of a row based on 1 or several column values faster.
* a UNIQUE INDEX is also nothing else than a INDEX combined with a UNIQUE constraint, to make sure each value is only present once in the table
* a UNIQUE CONSTRAINT is the same than a UNIQUE INDEX: a index with unique constraint...
* a PRIMARY KEY is the same as a UNIQUE CONSTRAINT.

* a FOREIGN KEY is nothing else than a constraint that points to a UNIQUE INDEX/PRIMARY KEY/UNIQUE CONSTRAINT, usually on another table, it can be on the same table.
0
 
LVL 5

Assisted Solution

by:sampipes
sampipes earned 360 total points
ID: 23702428
On the point of getting the t-sql for something you have done in SSMS. You could create the relationship there and use Script Table from the context of that table to view the SQL that would create it.
If not everything you need is included you may want to look at the Scripting in the Options.
0
 
LVL 43

Assisted Solution

by:Eugene Z
Eugene Z earned 160 total points
ID: 23702450
PK and FK scripts + Tables structure scriot will give you what you need
-----------------------------------------------------------------------------------------------------
see
Design A Database Using an Entity-Relationship Diagram
http://www.sqlservercentral.com/articles/Miscellaneous/designadatabaseusinganentityrelationshipdiagram/1159/
  http://www.informit.com/guides/content.aspx?g=sqlserver&seqNum=51
http://www.sqlteam.com/article/database-design-and-modeling-fundamentals 
-----------------------------
Also you can try inner join in your sql syntax
http://www.w3schools.com/sql/sql_join.asp 
 
http://msdn.microsoft.com/en-us/library/aa213234(SQL.80).aspx 
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 80 total points
ID: 23713355
>> * a PRIMARY KEY is the same as a UNIQUE CONSTRAINT. <<

That is not technically correct, since:

1) A Primary Key by default will always be clustered (unless a clus index already exists), but a unique constraint will not unless you explicitly tell SQL to cluster on that index.

2) A PK cannot contain any NULL values; a UC can, as long as each set of index columns is not duplicated, including the null.  For example, if a PK is (colA, colB), you cannot insert (1, NULL).  If it's a UC, you can, and (NULL, 1) but you could not then insert another (1, NULL)/(NULL, 1).
0
 
LVL 2

Author Comment

by:agrogers
ID: 23914990
Hi guys. My appreciation for your responses is inversely proportional to the tardiness of this response.  I have now managed to do what i needed.  Part of the problem was SQLDMO giving me some unexpected results.  But all good now.  And so to assign points...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question