更新导致重复值时使用ON UPDATE CASCADE

时间:2014-05-11 16:27:54

标签: c# sql database postgresql constraints

在PostgreSQL中,我有一个主表ICD9,用于保存cicd9和cdesc的唯一组合,其约束条件为:

CONSTRAINT constraint_cdesc UNIQUE (cicd9, cdesc)

我有多个子表引用ICD9表:

CONSTRAINT fk_icd9 FOREIGN KEY (cicd9, cdesc)
REFERENCES icd9 (cicd9, cdesc) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED

我可以更改我需要的任何约束,但我的目标是使用主表来纠正子表的所有拼写错误。我没有在互联网上找到好的(或任何)答案。问题是更新cdesc中的错误拼写或cicd9中的错误代码会导致主表中出现重复的条目 - 因此会抛出错误。

这样做的最佳方法是什么(我认为)相对常见的问题?

注意:我在一个单独的程序中使用C#(Net 4.0)来处理PostgreSQL 9.1数据库服务器(我是管理员)。

2 个答案:

答案 0 :(得分:0)

如果updating icd9中的条目导致unique constraint违规行为,那么在我看来,在这种情况下您实际想做的事情是deleteicd9输入,而不是更新。

如果deleting该条目会在其他表中导致FK constraint违规,那么我建议首先通过更新子表中的条目来引用该问题,以引用导致的条目unique constraint违规(即已存在的'正确'值),然后对'不正确'值执行delete

如果你想在某种程度上自动化这个,你可能会创建一个pl/pgSQL函数:

  1. 检查是否会违反任何唯一约束
  2. 如果是,这是删除候选人
  3. 然后,检查子表以查看是否有任何引用删除候选项的条目
  4. 如果是,update这些指向“正确”的行 - 即导致唯一约束违规的行
  5. 然后执行delete
  6. 中行的icd9

    注意:您不希望在约束DELETE中拥有CASCADE,因为您不想删除引用它的行,而是更新它们指向icd9中的另一行。

    修改 要以编程方式确定表具有的FK(或其他)约束,您可以查询属于pg_constraint的{​​{1}}表。有关详细信息,请参阅http://www.postgresql.org/docs/9.3/static/catalog-pg-constraint.html

    您可能希望将其与catalog一起加入,以便从原始表名开始。有关详细信息,请参阅http://www.postgresql.org/docs/9.3/static/catalog-pg-class.html

答案 1 :(得分:0)

我建议将此作为一种可能的解决方案。如果有人感兴趣的话,我最感兴趣的是这个的优化版本。谢谢@Ken的指点。 (我不知道如何最好地格式化这个,抱歉)。

internal static void UpdateDxLibraryTx(PhysicalExam.DX newdx, PhysicalExam.DX olddx)
    {
        String Unique_Violation = "23505";

        // nothing to change.
        if (olddx.description == null) return;  

        PhysicalExam.DX oldDx = (PhysicalExam.DX)olddx;

        // the server structure was created in MainWindow on initialization.
        string connect = server.connection_string;

        string update = " update icd9 set cicd9='" + newdx.icd9 + "', cdesc= '" + newdx.description.ToLower().Trim() + "'" +
                " ,chronic ='" + newdx.chronic + "' ,modified='" + DateTime.Now + "'" +
                " where cicd9 ='" + oldDx.icd9 + "'" +
                " and trim(cdesc) ='" + oldDx.description.ToLower().Trim() + "' and chronic ='" + oldDx.chronic + "';";

        string getchildtables = " select confrelid::regclass, af.attname as fcol,   "+ 
                                " conrelid::regclass, a.attname as col              "+
                                " from pg_attribute af, pg_attribute a,             "+
                                " (select conrelid,confrelid,conkey[i] as conkey, confkey[i] as confkey "+
                                " from (select conrelid,confrelid,conkey,confkey,   "+
                                " generate_series(1,array_upper(conkey,1)) as i     "+
                                " from pg_constraint where contype = 'f') ss) ss2   "+
                                " where af.attnum = confkey and af.attrelid = confrelid and             "+ 
                                " a.attnum = conkey and a.attrelid = conrelid "+
                                " AND confrelid::regclass = 'ICD9'::regclass AND ( af.attname = 'cicd9' OR af.attname ='cdesc');";

        string sf = " update {0} set cicd9= '"+ newdx.icd9 +"' ,cdesc= '"+ newdx.description.ToLower().Trim() +"',"   +
                        " chronic = '"+newdx.chronic+"', modified= '"+ DateTime.Now +"'"                    +
                        " where cicd9 = '"+ oldDx.icd9 +"' and trim(cdesc)= '"+ oldDx.description.ToLower().Trim() +"' and chronic ='"+ oldDx.chronic +"';";

        string delete = "Delete from icd9 where trim(cicd9) = '" + oldDx.icd9.Trim() + "'" +
                        " and trim(cdesc)='" + oldDx.description.Trim() + "' and chronic = '"+ oldDx.chronic +"';";


        // NpsqlConnection is unmanaged code...should use a using() block.
        using (NpgsqlConnection pgConnection = new NpgsqlConnection(connect))
        {
            try
            {
                pgConnection.Open();
                // Connection successful

                // Create a new transaction
                using (NpgsqlTransaction pgTransaction = (NpgsqlTransaction)pgConnection.BeginTransaction())
                {
                    try
                    {
                        using (NpgsqlCommand updateCMD = new NpgsqlCommand(update, pgConnection, pgTransaction))
                        {
                            updateCMD.ExecuteNonQuery();
                        }

                        //Commit transaction. Hope it works!  Will fail for unique key violation.
                        pgTransaction.Commit();

                    }
                    catch (NpgsqlException ex)
                    {
                        pgTransaction.Rollback();

                        if (ex.Code == Unique_Violation)            
                        {                                
                            try
                            {
                                using (NpgsqlDataAdapter children = new NpgsqlDataAdapter(getchildtables,pgConnection))   
                                {
                                    DataSet ds = new DataSet();
                                    children.Fill(ds);

                                    DataView dv = new DataView();
                                    dv.Table = ds.Tables[0];

                                    foreach (DataRowView drv in dv)
                                    {
                                        string childtable = (string)drv[2];
                                        string updatechild = String.Format(sf, childtable);

                                        NpgsqlCommand updateCMD = new NpgsqlCommand(updatechild, pgConnection);
                                        int k = updateCMD.ExecuteNonQuery();
                                    }
                                    /* all tables now point to new value. remove old value from parent table. */

                                    NpgsqlCommand deleteCMD = new NpgsqlCommand(delete, pgConnection);
                                    int j = deleteCMD.ExecuteNonQuery();
                                }
                            }
                            catch (NpgsqlException ex1)
                            {
                                string error = ex1.Message;
                                throw;
                            }

                        }
                        else
                            throw;
                    }
                }
            }
            catch (NpgsqlException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                throw;
            }
        }
    }