Wednesday, March 28, 2012

How to make table relations using query.

.

Hi,

How can I make relations between two tables using query? Tables are already there with data.

RegardsDo you mean you want to add a Foreign Key constraint?

Or do you just want to select the data?

To select the data, you would use a JOIN:


SELECT
Table1.name,
Table2.address
FROM
Table1
INNER JOIN
Table2 ON Table1.ID = Table2.ID

If you give us some more information we should be able to provide better help.

Terri|||Hi,

Thanks. Actually I uploaded my SQl Server DB to a free server for testing purpose. Tables were uploaded successfully but the relationship between the tables were erased, I mean Primary key and Foriegn key. The problem is that they have only a web based panel working to update database and I have to use Query Commands to manage my DB.

Tables are already there, I just need to make relations (Primary & Foreign Kies) between these tables.

I hope I could make it a little bit clear.

Regards,|||You would need something along these lines (assuming column names for myPKColumn and myFKColumn):


ALTER TABLE
myTable
ADD CONSTRAINT
myPKConstraintName
PRIMARY KEY (myPKColumn)

ALTER TABLE
myTable
ADD CONSTRAINT
myFKConstraintName
FOREIGN KEY (myFKColumn)

See BOL orConstrain your SQL Server data with T-SQL for more info.

Terri

No comments:

Post a Comment