Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi


Somebody please tell me how i use foreign key
Posted

all information regarding your question ...
http://www.1keydata.com/sql/sql-foreign-key.html[^]
 
Share this answer
 
Comments
Arora1992 3-Nov-14 6:56am    
I create a code Please See this


SqlConnection abc = new SqlConnection(@"Connection string ");
abc.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = abc;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Email_insert_procedure";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = "125";
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = "Mr";



and Thats Sql Table and Procedure


create table Email
(
Id int FOREIGN KEY (Id) REFERENCES contact(Id),
Email varchar(100) NOT NULL
)
go

create procedure Email_insert_procedure(@Id int, @Email varchar(100))as
insert into Email values (@Id, @Email)
Akashjoshi 3-Nov-14 7:07am    
your defination is correct but first you insert data into contact table and those data is used in Email table...

Like
insert into contact VALUES(1)
insert into Email VALUES(1,'aka@gmail.com')
Arora1992 3-Nov-14 7:13am    
I already Insert value into Contact table the id is same 125 but this query is nor working
Akashjoshi 3-Nov-14 7:23am    
Try this...
insert into Email VALUES((select id from contact WHERE Id=@id),@email)
Arora1992 3-Nov-14 8:08am    
I am creating a procedure how i try this Try this...
insert into Email VALUES((select id from contact WHERE Id=@id),@email)
How to use? Normally... to build relationship between tables.

See this:
http://www.w3schools.com/sql/sql_foreignkey.asp[^]
Create Foreign Key Relationships[^]
 
Share this answer
 
Comments
Arora1992 3-Nov-14 6:51am    
I create a code Please See this


SqlConnection abc = new SqlConnection(@"Connection string ");
abc.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = abc;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Email_insert_procedure";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = "125";
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = "Mr";



and Thats Sql Table and Procedure


create table Email
(
Id int FOREIGN KEY (Id) REFERENCES contact(Id),
Email varchar(100) NOT NULL
)
go

create procedure Email_insert_procedure(@Id int, @Email varchar(100))as
insert into Email values (@Id, @Email)
Maciej Los 3-Nov-14 7:30am    
How it is related to your question??
Arora1992 3-Nov-14 7:34am    
I create one more table with the name of Contact in this table i store Id and Name . So I need same Id in Table Email and Email address of user . How i do this
When you create a relationship between 2 table, a master and a slave table. You use a foreign key to be used as a field to create the relationship on.

In your code (from your comment) it is clear that the ID that integer, would be a FOREIGN KEY. As Microsoft SQL Server documentation[^] says,

Quote:
A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables. You can create a foreign key by defining a FOREIGN KEY constraint when you create or modify a table.


Read the attached link, it has more information on this chapter and you will find more details to it there.

Thanks.
 
Share this answer
 
Comments
Arora1992 3-Nov-14 6:56am    
I create a code Please See this


SqlConnection abc = new SqlConnection(@"Connection string ");
abc.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = abc;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Email_insert_procedure";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = "125";
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = "Mr";



and Thats Sql Table and Procedure


create table Email
(
Id int FOREIGN KEY (Id) REFERENCES contact(Id),
Email varchar(100) NOT NULL
)
go

create procedure Email_insert_procedure(@Id int, @Email varchar(100))as
insert into Email values (@Id, @Email)
Afzaal Ahmad Zeeshan 3-Nov-14 6:59am    
..and Microsoft created a documentation for new developers, please see that.

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