mysql 未能启用约束,未能启用约束。一行或多行中包含违反非空,唯一,或外键约束...

I make an outer join and executed successfully in the informix database but I get the following exception in my code:

DataTable dt = TeachingLoadDAL.GetCoursesWithEvalState(i, bat);

Failed to enable constraints. One or more rows contain values

violating non-null, unique, or foreign-key constraints.

I know the problem, but I don't know how to fix it.

The second table I make the outer join on contains a composite primary key which are null in the previous outer join query.

EDIT:

SELECT UNIQUE a.crs_e, a.crs_e || '/ ' || a.crst crs_name, b.period,

b.crscls, c.crsday, c.from_lect, c.to_lect,

c.to_lect - c.from_lect + 1 Subtraction, c.lect_kind, e.eval, e.batch_no,

e.crsnum, e.lect_code, e.prof_course

FROM rlm1course a, rfc14crsgrp b, ckj1table c, mnltablelectev d,

OUTER(cc1assiscrseval e)

WHERE a.crsnum = b.crsnum

AND b.crsnum = c.crsnum

AND b.crscls = c.crscls

AND b.batch_no = c.batch_no

AND c.serial_key = d.serial_key

AND c.crsnum = e.crsnum

AND c.batch_no = e.batch_no

AND d.lect_code= e.lect_code

AND d.lect_code = ....

AND b.batch_no = ....

The problem happens with the table cc1assiscrseval. The primary key is (batch_no, crsnum, lect_code).

How to fix this problem?

EDIT:

According to @PaulStock advice:

I do what he said, and i get:

? dt.GetErrors()[0] {System.Data.DataRow} HasErrors: true ItemArray:

{object[10]} RowError: "Column 'eval' does not allow DBNull.Value."

So I solve my problem by replacing e.eval to ,NVL (e.eval,'') eval.and this solves my problem.

Thanks a lot.

解决方案

This problem is usually caused by one of the following

null values being returned for columns not set to AllowDBNull

duplicate rows being returned with the same primary key.

a mismatch in column definition (e.g. size of char fields) between the database and the dataset

Try running your query natively and look at the results, if the resultset is not too large. If you've eliminated null values, then my guess is that the primary key columns is being duplicated.

Or, to see the exact error, you can manually add a Try/Catch block to the generated code like so and then breaking when the exception is raised:

HQrHa.png

Then within the command window, call GetErrors method on the table getting the error.

For C#, the command would be ? dataTable.GetErrors()

For VB, the command is ? dataTable.GetErrors

ekNb5.png

This will show you all datarows which have an error. You can get then look at the RowError for each of these, which should tell you the column that's invalid along with the problem. So, to see the error of the first datarow in error the command is:

? dataTable.GetErrors(0).RowError

or in C# it would be ? dataTable.GetErrors()[0].RowError

Ynmlf.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值