SQL Server根据两个表中的组合键自动删除FK记录?

时间:2015-10-08 09:44:19

标签: sql sql-server

我想要一个表来存储数据库中任何数据表中任何记录的注释,并在删除相关数据记录时删除注释记录。

我可以在音符表,table-id表和数据表之间设置组合的FK键关系并使用ON DELETE CASCADE或者我是否需要在数据表上使用ON DELETE触发器,或者什么是最好的接近?

E.g。有三张桌子,水果,建筑物,衣服;

REFTABLE_ID     TABLENAME
-------------------------                   
21              FRUITS                      
22              BUILDINGS                       
23              CLOTHES                     

我定义了一个表ids表;

NOTES_ID    REFTABLE_ID  REFTABLE_RECORD_ID  NOTETEXT       
-------------------------------------------------------------   
1           21           1                   Make a pie             
2           21           2                   Try lemonade               
3           22           2                   Handles emergencies                
4           23           1                   Can be waterproof              

并且notes表使用该表中的表id和记录id;

public class Payment05TowMachineEntryFragment extends Fragment {

    public static MainActivity lastCreated;

    private TextView DateDisplay;
    private ImageButton PickDate;
    private Calendar Date;

    static final int DATE_DIALOG_ID = 999;

    private TextView activeDateDisplay;
    private Calendar activeDate;

    public Payment05TowMachineEntryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.payment05_towmachine_entry_fragment, container, false);

        return rootView;
    }

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        DateDisplay = (TextView) (getView().findViewById(R.id.selectDate));
        PickDate = (ImageButton) (getView().findViewById(R.id.btncalender));

        /* get the current date */
        Date = Calendar.getInstance();

        /* add a click listener to the button   */
        PickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDateDialog(DateDisplay, Date);
            }
        });

        /* display the current date (this method is below)  */
        updateDisplay(DateDisplay, Date);

    }

    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
            //unregisterDateDisplay();
        }
    };

    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(getActivity(), dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }


    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
                break;
        }
    }

    private void showDateDialog(TextView dateDisplay, Calendar date) {
        activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }

    private void showDialog(int dateDialogId) {


    }

    private void updateDisplay(TextView dateDisplay, Calendar date) {
        DateDisplay.setText(
                new StringBuilder()
                        .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                        .append(date.get(Calendar.MONTH) + 1).append("-")
                        .append(date.get(Calendar.YEAR)).append(" "));
    }

}

所以在BUILDINGS(REFTABLE_ID 22)中记录'HOSPITAL'(BUILDINGS_ID 2)的notetext是'处理紧急情况'(NOTES_ID 3)

删除BUILDINGS记录ID 2时,“自动”删除记录记录ID 3的最佳方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您使用的是SQL Server 2008或更高版本,我认为您可以使用MERGE语句。 此外,如果您在所有三个表FRUITS,BUILDINGS和CLOTHES中添加其他列“TABLE_ID”,那么我认为您可以像下面这样做(假设您的表的名称与表ID是“TABLE_IDS”):

MERGE NOTES AS Target
USING (SELECT TBL.REFTABLE_ID, B.BUILDINGS_ID, F.FRUITS_ID, C.CLOTHES_ID 
FROM 
BUILDINGS [B] JOIN TABLE_IDS [TBL] ON B.TABLE_ID=TBL.TABLE_ID JOIN FRUITS [F] 
ON F.TABLE_ID=TBL.TABLE_ID JOIN CLOTHES [C] ON C.TABLE_ID=TBL.TABLE_ID) 
As Source
ON ((Target.REFTABLE_RECORD_ID = Source.BUILDINGS_ID  AND Target.REFTABLE_ID 
= Source.REFTABLE_ID) 
OR (Target.REFTABLE_RECORD_ID =Source.FRUITS_ID AND Target.REFTABLE_ID = 
Source.REFTABLE_ID) 
OR (Target.REFTABLE_RECORD_ID =Source.CLOTHES_ID AND Target.REFTABLE_ID = 
Source.REFTABLE_ID))
WHEN MATCHED 
THEN DELETE
OUTPUT Deleted.*;