将主键值作为外键复制到另一个表中?

时间:2012-10-03 16:12:08

标签: foreign-keys

CREATE TRIGGER copy_pk_into_fk_trigger
    AFTER insert ON tableA
    FOR EACH ROW
    BEGIN
      insert into tableB (tblA_ID) values (new.tblA_ID);
END$$

我正在使用上面的触发器将tableA和inset中的Primary Key值复制到tableB中作为外键。但在那之后,每当我使用ASP.net中的detailsView编辑tableB中的记录时,外键值变为“NULL”。这导致不再与父母和孩子建立联系。请帮助我。

[tableA有col:tblA_ID,tblA_val和
TableB包含tblB_ID,tblA_ID,tblB_str
等等]

2 个答案:

答案 0 :(得分:0)

我不认为这与你的触发器有关。因为触发器仅在触发插入后运行。你能告诉我在detailsView中更新的查询吗?你确定你没有提到更新声明吗?

答案 1 :(得分:0)

这是SqlDATASource和DetailsView的完整源代码:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HADIConnectionString %>" 
            DeleteCommand="DELETE FROM &quot;INDENT_FILE_NO&quot; WHERE &quot;FILE_NO_ID&quot; = :original_FILE_NO_ID AND ((&quot;INDENT_ID&quot; = :original_INDENT_ID) OR (&quot;INDENT_ID&quot; IS NULL AND :original_INDENT_ID IS NULL)) AND ((&quot;FILE_NO&quot; = :original_FILE_NO) OR (&quot;FILE_NO&quot; IS NULL AND :original_FILE_NO IS NULL)) AND ((&quot;FILE_DESCRIPTION&quot; = :original_FILE_DESCRIPTION) OR (&quot;FILE_DESCRIPTION&quot; IS NULL AND :original_FILE_DESCRIPTION IS NULL)) AND ((&quot;INDENTER&quot; = :original_INDENTER) OR (&quot;INDENTER&quot; IS NULL AND :original_INDENTER IS NULL))" InsertCommand="INSERT INTO &quot;INDENT_FILE_NO&quot; (&quot;FILE_NO_ID&quot;, &quot;INDENT_ID&quot;, &quot;FILE_NO&quot;, &quot;FILE_DESCRIPTION&quot;, &quot;INDENTER&quot;) VALUES (:FILE_NO_ID, :INDENT_ID, :FILE_NO, :FILE_DESCRIPTION, :INDENTER)" OldValuesParameterFormatString="original_{0}" ProviderName="<%$ ConnectionStrings:HADIConnectionString.ProviderName %>" 
            SelectCommand="SELECT * FROM &quot;INDENT_FILE_NO&quot; WHERE (&quot;INDENT_ID&quot; = :INDENT_ID)" 
            UpdateCommand="UPDATE &quot;INDENT_FILE_NO&quot; SET &quot;INDENT_ID&quot; = :INDENT_ID, &quot;FILE_NO&quot; = :FILE_NO, &quot;FILE_DESCRIPTION&quot; = :FILE_DESCRIPTION, &quot;INDENTER&quot; = :INDENTER WHERE &quot;FILE_NO_ID&quot; = :original_FILE_NO_ID AND ((&quot;INDENT_ID&quot; = :original_INDENT_ID) OR (&quot;INDENT_ID&quot; IS NULL AND :original_INDENT_ID IS NULL)) AND ((&quot;FILE_NO&quot; = :original_FILE_NO) OR (&quot;FILE_NO&quot; IS NULL AND :original_FILE_NO IS NULL)) AND ((&quot;FILE_DESCRIPTION&quot; = :original_FILE_DESCRIPTION) OR (&quot;FILE_DESCRIPTION&quot; IS NULL AND :original_FILE_DESCRIPTION IS NULL)) AND ((&quot;INDENTER&quot; = :original_INDENTER) OR (&quot;INDENTER&quot; IS NULL AND :original_INDENTER IS NULL))" ConflictDetection="CompareAllValues">

        <UpdateParameters>
            <asp:Parameter Name="INDENT_ID" Type="Decimal" />
            <asp:Parameter Name="FILE_NO" Type="String" />
            <asp:Parameter Name="FILE_DESCRIPTION" Type="String" />
            <asp:Parameter Name="INDENTER" Type="String" />
            <asp:Parameter Name="original_FILE_NO_ID" Type="Decimal" />
            <asp:Parameter Name="original_INDENT_ID" Type="Decimal" />
            <asp:Parameter Name="original_FILE_NO" Type="String" />
            <asp:Parameter Name="original_FILE_DESCRIPTION" Type="String" />
            <asp:Parameter Name="original_INDENTER" Type="String" />
        </UpdateParameters>

详细信息视图:

<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" DataKeyNames="FILE_NO_ID" DataSourceID="SqlDataSource2" Height="50px" Width="287px" >
                        <Fields>
                            <asp:BoundField DataField="FILE_NO_ID" HeaderText="FILE_NO_ID" ReadOnly="True"  SortExpression="FILE_NO_ID" Visible="False" />
                            <asp:BoundField DataField="INDENT_ID" HeaderText="INDENT_ID" SortExpression="INDENT_ID" Visible="False" />
                            <asp:BoundField DataField="FILE_NO" HeaderText="FILE_NO" SortExpression="FILE_NO" />
                            <asp:BoundField DataField="FILE_DESCRIPTION" HeaderText="FILE_DESCRIPTION" SortExpression="FILE_DESCRIPTION" />
                            <asp:BoundField DataField="INDENTER" HeaderText="INDENTER" SortExpression="INDENTER" />
                            <asp:CommandField ShowEditButton="True" ButtonType="Button" />
                        </Fields>
                    </asp:DetailsView>

但现在,当我按下编辑按钮并编辑字段然后更新它们时,该字段的数据保持不变。意味着不采取行动。