如何检查列是否已经是外键?

时间:2011-02-21 14:42:22

标签: c# sql mysql

我的表名为Person,列名为ID 如何检查ID是否已经是FOREIGN KEY因为我想用这段代码来实现:

ALTER TABLE Person ADD FOREIGN KEY(ID) REFERENCES Job(ID)
ON DELETE CASCADE ON UPDATE CASCADE

但是如果ID已经是FOREIGN KEY,它会给我以下错误“可能导致循环或多个级联路径”,因为有两个级联的条件...如何检查此字段是否为FOREIGN KEY以避免此错误?

2 个答案:

答案 0 :(得分:6)

您想要查看INFORMATION SCHEMA次观看

虽然它没有那么完整。这是您想要的最终查询:

SELECT 
     KCU1.CONSTRAINT_NAME AS 'FK_CONSTRAINT_NAME'
   , KCU1.TABLE_NAME AS 'FK_TABLE_NAME'
   , KCU1.COLUMN_NAME AS 'FK_COLUMN_NAME'
   , KCU1.ORDINAL_POSITION AS 'FK_ORDINAL_POSITION'
   , KCU2.CONSTRAINT_NAME AS 'UQ_CONSTRAINT_NAME'
   , KCU2.TABLE_NAME AS 'UQ_TABLE_NAME'
   , KCU2.COLUMN_NAME AS 'UQ_COLUMN_NAME'
   , KCU2.ORDINAL_POSITION AS 'UQ_ORDINAL_POSITION'
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU1
ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG 
   AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
   AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2
ON KCU2.CONSTRAINT_CATALOG = 
RC.UNIQUE_CONSTRAINT_CATALOG 
   AND KCU2.CONSTRAINT_SCHEMA = 
RC.UNIQUE_CONSTRAINT_SCHEMA
   AND KCU2.CONSTRAINT_NAME = 
RC.UNIQUE_CONSTRAINT_NAME
   AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION

请点击此处了解更多信息

http://msdn.microsoft.com/en-us/library/aa175805(v=sql.80).aspx

答案 1 :(得分:0)

Here is a simple little version

package booke.vfj.angour.ir.baftani_davood;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;


import org.json.JSONObject;

import java.util.ArrayList;

public class G extends Application {

    public static Context               context;
    public static LayoutInflater        inflater;
    public static Activity              currentActivity;
    public static ArrayList<StructTest> Recordsfehrset = new ArrayList<StructTest>();
    public static ArrayList<StructTest2> recordjoziat = new ArrayList<StructTest2>();
    public static ArrayList<StructTest> fov = new ArrayList<StructTest>();

    @Override
    public void onCreate()
    {
        super.onCreate();
        context = getApplicationContext();
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        OneSignal.startInit(this).init();
        OneSignal.startInit(this).setNotificationOpenedHandler(new NotificationOpenedHandler()).init();
    }

    private class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        @Override
        public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
            try {
                String messageTitle;
                AlertDialog.Builder builder = null;

                if (additionalData != null) {
                    if (additionalData.has("discount"))
                        messageTitle = "Discount!";
                    else if (additionalData.has("bonusCredits"))
                        messageTitle = "Bonus Credits!";
                    else
                        messageTitle = "Other Extra Data";

                    builder = new AlertDialog.Builder(getApplicationContext()).setTitle(messageTitle).setMessage(message + "\n\n" + additionalData.toString());
                }
                else if (isActive) // If a push notification is received when the app is being used it does not display in the notification bar so display in the app.
                    builder = new AlertDialog.Builder(getApplicationContext())
                            .setTitle("OneSignal Message")
                            .setMessage(message);

                // Add your app logic around this so the user is not interrupted during gameplay.
                if (builder != null)
                    builder.setCancelable(true)
                            .setPositiveButton("OK",null)
                            .create().show();

            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
}

You can easily add table name and DB name in the where clause as well

相关问题