根据列值创建条件索引

时间:2019-05-09 17:58:05

标签: oracle

echo "<table>";
    echo "<tr>
        <th>Match_Id</th>
        <th>Home_Team</th> 
        <th>Away_Team</th>
        <th>Result</th>
        <th>season</th> 
        <th>notes</th>
        <th>Goals_Sum</th>
      </tr> ";


     $sql = "SELECT * FROM PremierLeague";
  $result = sqlsrv_query($conn, $sql);

 while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
            $id = $row['id'];
          $Home = $row['Home'];
          $Away = $row['Away'];     
        $result = $row['result'];
       $season = $row['season'];
       $notes = $row['notes'];
     $home_goals= $row['home_goals'];
     $away_goals= $row['away_goals'];
     $GoalSum = $home_goals+$away_goals;



 echo $output.="<tr>
                 <td>$id</td>
                 <td>$Home</td>
                 <td>$Away</td>
                 <td>$result</td>
                 <td>$season</td>
                 <td>$notes</td>
                 <td>$GoalSum</td>
                </tr>";
 }
  echo "</table>";

User_ID和Category_ID存在现有约束。我需要添加一个约束以确保只有一个用户 级别Table1 ___________ User_ID Location_ID Type_ID Level_ID Levels _________ 1111 Manager 2222 Staff 的级别可以添加到每种类型的位置。

如何在Location_ID,Type_ID和Level_ID上创建唯一索引,其中level_id = mypackage.get_level_id('Manager')?

我正在尝试类似的事情:

Manager

CREATE UNIQUE INDEX idx_mgr on Table1 (CASE WHEN Level_id=mypackage.get_level_id('Manager') 
                    THEN (Location_ID,Type_ID)
                    ELSE NULL
                END);

我该如何工作?

1 个答案:

答案 0 :(得分:1)

仅定义管理级别的功能索引(直接引用常量,而不是通过包),您将获得所需的行为:

create unique index ix1 on table1
 (case when level_id = 1111 then Location_ID end, case when level_id = 1111 then Type_ID end);

您可以插入任意数量的具有相同位置和类型的内容,但只能插入一个管理员:

insert into table1(User_ID,Location_ID, Type_ID, Level_ID) values(1,1,1,2222); 
insert into table1(User_ID,Location_ID, Type_ID, Level_ID) values(2,1,1,2222);
insert into table1(User_ID,Location_ID, Type_ID, Level_ID) values(3,1,1,1111);
insert into table1(User_ID,Location_ID, Type_ID, Level_ID) values(4,1,1,1111);
-- fails with ORA-00001: Unique Constraint violated