Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Relationship within tables

Author  Topic 

morietrenatus
Starting Member

2 Posts

Posted - 2013-04-21 : 22:42:57
Can anyone help me teaching me the code to make a relationship between two tables and explain me the meaning of each command.

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-04-21 : 23:04:15
You can start here:
http://en.wikipedia.org/wiki/Relational_Database
http://en.wikipedia.org/wiki/SQL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-22 : 00:44:37
the relation ship between two tables is defined by means of foreign key relationship
see this illustration

CREATE TABLE Products
(
ProductID int NOT NULL PRIMARY KEY,
ProductName varchar(100)
)

CREATE TABLE Orders
(
OrderID int NOT NULL PRIMARY KEY,
ProductID int NOT NULL,
Qty int,
Price money
)

ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (ProductID)
REFERENCES Product(ProductID)



the above code will create a relationship between Orders and Products

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -