Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Friends

I am new, so I don't konw how to start my question ? I need help for foreign key constraint or say join 2 tables. please find me a source code for this.

Ridhi Shrestha
Posted
Comments
Sandeep Mewara 8-Sep-10 13:12pm    
why directly source code? Will that make you learn? :doh:

Hi Ridhi,

http://www.java2s.com/Code/SQLServer/Constraints/Howtouseforeignkeyconstraints.htm[^]

http://www.1keydata.com/sql/sql-foreign-key.htm[^]

http://www.sql-tutorial.net[^]

Refer those links.

In my suggestion first you can learn basics of sql.There are lot of books available in online.Search in google.

Cheers :)
 
Share this answer
 
ok first, Foreign keys do not really 'join' tables, they rather 'relate' tables and the main reason why it is useful to create foreign key constraint in a table is serve as a checking mechanism to prevent actions that will cause data in a related table through an action like dropping a table etc

Below is a basic sql code showing a table with a foreign key cons.
SQL
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)


but in joining two tables (in a sort of extension), then you would have to use sql joins, which if you dont have any knowledge of yet, I would edge you to do google it up and read..Also check up the sql union..:)
 
Share this answer
 
Hi Ridhi Shrestha,

follow the below links

http://www.1keydata.com/sql/sql-foreign-key.html

http://www.mssqlcity.com/Articles/General/using_constraints.htm


Table CUSTOMER
column name characteristic
------------ ------------
SID int Primary Key
Last_Name varchar(50)
First_Name varchar(50)

Table ORDERS

CREATE TABLE ORDERS
(Order_ID integer primary key,
Order_Date datetime,
Customer_SID int references CUSTOMER(SID),
Amount money)


Cheers :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900