Understanding SQL Relationships and How to Define Them in SQL Server

  • Thread starter Thread starter FrankJ777
  • Start date Start date
  • Tags Tags
    Relationships Sql
Click For Summary

Discussion Overview

The discussion revolves around defining relationships in SQL Server, particularly how to assert these relationships using SQL statements rather than graphical methods. Participants also explore the purpose of defining relationships in the context of database integrity and query operations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant inquires about how to define relationships in SQL Server using SQL statements, expressing uncertainty about the process.
  • Another participant suggests that defining relationships may not be necessary since JOIN operations can achieve similar results in queries.
  • A different participant provides an example illustrating the importance of relationships for maintaining referential integrity, noting that without a defined relationship, accidental deletions could lead to issues.
  • Some participants reference external resources for further information on defining relationships and enforcing referential integrity.

Areas of Agreement / Disagreement

There is no consensus on the necessity of defining relationships, as some participants question their importance while others emphasize their role in maintaining data integrity.

Contextual Notes

Participants express varying levels of familiarity with SQL, which may influence their understanding and perspectives on the topic. There are also references to external resources that may not be universally accessible or applicable.

FrankJ777
Messages
140
Reaction score
6
I'm trying to learn SQL, specifically for SQL server. One question I have is about defining relationships. I know how to build a relationship graphically (showing that the primary key from one table is the foreign key of another), but I'm not sure how to assert the relationship with an equivalent SQL statement. Is this possible?

Also, what is the purpose of defining a relationship? It seems that for any query I could accomplish the same thing using a JOIN operation.

Any attempt to enlighten me would be most appreciated.
Thanks
 
Technology news on Phys.org
FrankJ777 said:
I'm trying to learn SQL, specifically for SQL server. One question I have is about defining relationships. I know how to build a relationship graphically (showing that the primary key from one table is the foreign key of another), but I'm not sure how to assert the relationship with an equivalent SQL statement. Is this possible?

This should help:
http://msdn.microsoft.com/en-us/library/aa933118(SQL.80).aspx

Also, what is the purpose of defining a relationship? It seems that for any query I could accomplish the same thing using a JOIN operation.

To enforce http://en.wikipedia.org/wiki/Referential_integrity" .
 
Last edited by a moderator:
Couldn't understand the first question.. I don't know too much of sql (just enough to get done simple tasks)

FrankJ777 said:
Also, what is the purpose of defining a relationship? It seems that for any query I could accomplish the same thing using a JOIN operation.
From experience at work:
Say, I have two tables
Tasks
TaskDetails (contains taskId and more details about task)

If they have no relationship:
One of the records in task get accidentally deleted, SQL wouldn't complain .. and in future either this would cause some trouble or my program that uses this database would crash :(

With relationship:
If I attempt to delete task I would get an error that it cannot be deleted because it is referenced in TaskDetails.
 
Thanks for the succinct answers. I've been thumbing through several books tryong to find those answers!
 

Similar threads

  • · Replies 51 ·
2
Replies
51
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K