如何在C#中将密钥设为外国 [英] How to make key foreign in c#

查看:119
本文介绍了如何在C#中将密钥设为外国的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我是bishnu
我有一个项目,我想做一个外键,这是另一个表中的主键

像..........一个表的表名为tblEmployee,其中Employeeid为主键,而我的另一个表为tblAttendance,其中,希望Employeeid为外键.我正在使用sqlserver express2005.请尽快提供查询或代码帮助?

hi friends, i m bishnu
i have a project and i want to make a foreign key which is primary key in another table

like.......... one table has table name tblEmployee where Employeeid is primary key and i have another table tblAttendance where Employeeid is wished to be made foreign key. i am using sqlserver express 2005. please help with query or code as soon as possible?

推荐答案



解决方案在于SQL,而不是c#.

您可以通过以下方式向您的字段添加外键:

Hi,

The solution lies in SQL, not in c#.

You can add a foreign key to your field this way :

ALTER TABLE [tblAttendance]
ADD CONSTRAINT [FK_tblAttendance_tblEmployee]
FOREIGN KEY [Employeeid]
REFERENCES [tblEmplyee] ([Employeeid])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO



建议您在外键上也加一个索引:



It is advised to also put an index on your foreign key :

CREATE INDEX [IX_FK_tblAttendance_tblEmployee]
ON [tblAttendance] ([Employeeid]);
GO


如果您正在谈论DataRelation对象,请检查论坛讨论 [ ^ ]应该可以为您提供帮助.
If you are talking about DataRelation objects, then check this[^] out.

If you want to do this through code (for a table), then this forum discussion[^] should help you out.


如果在创建表时不使用该表,是否已经创建了该表. .......

试试这个查询

创建表tblAttendance
(
Employeeid data_type外键引用tblEmployee(Employeeid),
other_col data_type
)

如果已经创建了表,请使用philo先生建议的alter table
并尝试Abhinav先生提供的链接
祝你好运........
Have you created the table already if no use this when you create it.........

Try this query

create table tblAttendance
(
Employeeid data_type foreign key refrences tblEmployee(Employeeid),
other_col data_type
)

In case table is already created use alter table as suggested by Mr philo
and try links provided by Mr Abhinav
Best of luck........


这篇关于如何在C#中将密钥设为外国的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆