如何在表本身中存储forien键的值。 [英] how to store the value of forien key in table itself.

查看:177
本文介绍了如何在表本身中存储forien键的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中包含State,City和branch的文本框。

我还有三个表State [stateid int,states],City [cityid int,city,stateid int ],分支[branchid int,branchname,cityid int]。

这里stateid是状态表中的主键和city表中的forien键。

和cityid是主键分支表中的City Table和forien键。

现在我想在状态文本框中输入值,然后在自己的表[State Table。]中输入值,并自己增加1。然后,当我输入城市文本框,然后cityid增加1,城市名称和stateid也将显示在城市表本身。

像这样



州表

州政府国家

1哈里亚纳邦

2旁遮普邦

3拉贾斯坦邦



City

cityid cityname stateid

1 sirsa 1

2 rohtak 1

3 Barnala 2

4 Hanumangarh 3



这样我想通过文本框输入数据。

我该怎么办。



Thanx。

I have a form in which i have textbox of State,City, and branch.
I have also three tables State[stateid int, States], City[cityid int, city, stateid int], Branch[branchid int, branchname, cityid int].
here stateid is primary key in State Table and forien key in city table.
and cityid is primary key in City Table and forien key in branch table.
Now i want as i enter the value in state text box then the value store in own table[State Table.] and increase by 1 itself. Then as i enter city textbox then cityid increase by 1 and city name and stateid will also show in City Table itself.
Like this

State Table
stateid States
1 Haryana
2 Punjab
3 Rajasthan

City
cityid cityname stateid
1 sirsa 1
2 rohtak 1
3 Barnala 2
4 Hanumangarh 3

like this i want to enter the data by text boxes.
How can i do.

Thanx.

推荐答案

是的,你可以这样做。



要插入,您需要先插入主表/主表,然后插入外键表。



因此,在您的示例中,对于State,首先需要在State Table中插入然后获取插入的ID并插入City选项卡le。



要获取最近插入的ID,您可以使用 @@ IDENTITY SCOPE_IDENTITY()。请参阅一个示例 - Re:如何将主键列的值添加到其他表的外键列中? [< a href =http://forums.asp.net/post/5624883.aspxtarget =_ blanktitle =新窗口> ^ ]



插入主表时,如果您希望该ID应为自动增量,则需要将 Is Identity 属性设置为 Identity Increment 属性为您要增加的数字。



参考 - 在SQL Server Management Studio 2012中自动增加主键 [ ^ ]
Yes, you can do that.

For inserting, you need to first insert into the Main Table/Primary table, then in the Foreign Key Table.

So, in your example, for State, first you need to insert in State Table and then get the inserted ID and insert into the City Table.

To get the most recently inserted ID, you can use @@IDENTITY or SCOPE_IDENTITY(). Refer one example - Re: How to add values of primary key column into foreign key column of other table?[^]

While inserting into main Tables, if you want that ID should be Auto-Incremented, then you need to set the "Is Identity" property to "Yes" and "Identity Increment" property to the number you want to increment to.

Refer - Auto increment primary key in SQL Server Management Studio 2012[^]


这篇关于如何在表本身中存储forien键的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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