Hi Guys,
How do we create relation ships between tables using a sql query?
im using MS Access and VB.net.
Please post an example,tht would b really appreciated.
many thanks.

First you need to understand a whole lot about keys and constraints.

Creating a Table with a Foreign Key Reference
This demonstrates how to create a table with a foreign key. In this example, I define two foreign key references within the definition of a CREATE TABLE statement:

CREATE TABLE Person.EmployeeCreditRating(
EmployeeCreditRating int NOT NULL PRIMARY KEY,
BusinessEntityID int NOT NULL,
CreditRatingID int NOT NULL,
CONSTRAINT FK_EmployeeCreditRating_Employee
FOREIGN KEY(BusinessEntityID)
REFERENCES HumanResources.Employee(BusinessEntityID),
CONSTRAINT FK_EmployeeCreditRating_CreditRating
FOREIGN KEY(CreditRatingID)
REFERENCES Person.CreditRating(CreditRatingID)
)

How It Works:
In this example, a table was created with two foreign key references. The first four lines of code
defined the table name and its three columns:

CREATE TABLE Person.EmployeeCreditRating(
EmployeeCreditRating int NOT NULL PRIMARY KEY,
BusinessEntityID int NOT NULL,
CreditRatingID int NOT NULL,

On the next line, the name of the first foreign key constraint is defined (must be a unique name in the current database):

CONSTRAINT FK_EmployeeCreditRating_Employee

The constraint type is defined, followed by the table’s column (which will be referencing an outside primary key table):

FOREIGN KEY(BusinessEntityID)

The referenced table is defined, with that table’s primary key column defined in parentheses:

REFERENCES HumanResources.Employee(BusinessEntityID),

A second foreign key is then created for the CreditRatingID column, which references the primary key of the Person.CreditRating table:

CONSTRAINT FK_EmployeeCreditRating_CreditRating
FOREIGN KEY(CreditRatingID)
REFERENCES Person.CreditRating(CreditRatingID)

thanks for the reply.,i have got a lil idea how to we do this by seeing at ur code.and btw,how do v create one-one,one-many,many-many relationships here?

This article has been dead for over six months. Start a new discussion instead.