Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 外键约束建议_Sql - Fatal编程技术网

Sql 外键约束建议

Sql 外键约束建议,sql,Sql,我的问题是,为了更新我的数据库,我必须在每次由于约束键而希望更新时重新选择我的时区下拉框 我在处理两张桌子 城市表约束外键约束,即IDTimeZone 时区表包含主键IDTimeZone 我想弄清楚的是,我的插件基本上不依赖于用户是否选择了时区来更新目的地。不幸的是,我无法控制IDTimeZone是否是一个约束 // city timezone $time_zone = isset($_POST['tz']) ? $_POST['tz'] : 0; if ($time_zone <>

我的问题是,为了更新我的数据库,我必须在每次由于约束键而希望更新时重新选择我的时区下拉框

我在处理两张桌子

城市表约束外键约束,即IDTimeZone

时区表包含主键IDTimeZone

我想弄清楚的是,我的插件基本上不依赖于用户是否选择了时区来更新目的地。不幸的是,我无法控制IDTimeZone是否是一个约束

// city timezone
$time_zone = isset($_POST['tz']) ? $_POST['tz'] : 0;
if ($time_zone <> 0) {
$errorcode = 0;
$strmsg = "";
$sql="SELECT * from time_zone ORDER BY NAME;";
$result=mysql_query($sql);
$cont=mysql_num_rows($result);
if(mysql_num_rows($result)){
    $chtml = '<select name="tz" id="tz"><option value="0">--Select time zone--   </option>';
    while($row = mysql_fetch_array($result)){
        $chtml .= '<option value="'.$row['IDTimeZone'].'">'.$row['name'].'</option>';
    }
    $chtml .= '</select>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$chtml));
}else{
    $errorcode = 1;
    $strmsg = '<font style="color:#F00;">No States available</font>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$strmsg));
}
}

在Sql Server世界中,我确信可以轻松转换为其他Sql环境的一个技巧是在更新中测试值,如果为null,则只使用现有值

UPDATE city
SET
Population = @population --sql server syntax for parameters,
...--other columns
IDTimeZone = Case When @IDTimeZone IS NULL Then IDTimeZone Else @IDTImeZone End,
...--other columns
WHERE
IDCity = @IDCity

它的要点很简单…有价值吗?如果是,则设置它……否则,只需将其设置为表中的上一个值,实际上不会更改该列的值。

我的第一个问题是:你不能将时区数据缓存到某个位置,而不是每次都查询它吗

好的,顺便说一下,如果我理解你的问题,你想发布更新,尽管用户可能没有选择时区。就数据库而言,CITY表的外键字段将接受NULL值,除非数据库设计者故意将该字段定义为NOTNULL——这是可能的

但是…您正在执行Update语句,因此城市记录必须已经存在。我想问你的第二个问题是:你为什么不在现有的记录中读取并预加载时区下拉列表,其中包含已经存在的值(以及日出、日落和所有其他内容)?因此,无论用户更改它还是不处理它,您都可以

唯一的问题是插入新的城市记录时,用户未选择时区,并且时区字段已声明为非空。好的,然后告诉用户时区是必需的信息,并拒绝继续,直到他们选择的东西

我错过什么了吗

UPDATE city
SET
Population = @population --sql server syntax for parameters,
...--other columns
IDTimeZone = Case When @IDTimeZone IS NULL Then IDTimeZone Else @IDTImeZone End,
...--other columns
WHERE
IDCity = @IDCity