SQL Server CHECK对未明确引用的列的值进行约束

时间:2016-09-28 18:04:50

标签: sql sql-server database tsql relational-database

此处已经提出了类似的问题:constraint check against values of foreign key,此处Having data from another table put in into check constraint,但这并不是我的意思(或者除了函数之外还有其他方式)。

我的问题是。不是引用复合FOREIGN KEY,然后通过CHECK约束检查引用列的值,我可以在Table A中引用来自Table B的代理PK并检查值Table ATable A中的列的唯一标识,来自[test_table_a_id]的代理PK,例如CHECK CONSTRAINT

换句话说。我是否可以在Table BTable A显式引用特定列,而是引用行的ID并在-- =============================== -- ---------- TABLE A ------------ CREATE TABLE [r_Test_Table_A]( [test_table_a_id] [int] IDENTITY(1,1) CONSTRAINT [PK_r_test_table_a] PRIMARY KEY CLUSTERED ([test_table_a_id] ASC) , [a_attribute_1] [nvarchar] (50) NOT NULL , [a_attribute_2] [nvarchar] (50) NOT NULL , CONSTRAINT [ak_r_Test_Table_A_a_attribute1_a_attribute_2] UNIQUE ([a_attribute_1], [a_attribute_2]) ) -- =============================== -- ---------- TABLE B ------------ CREATE TABLE [r_Test_Table_B]( [test_table_b_id] [int] IDENTITY(1,1) CONSTRAINT [PK_r_test_table_b] PRIMARY KEY CLUSTERED ([test_table_b_id] ASC) , [b_attribute_1] [nvarchar] (50) NOT NULL , [a_attribute_1] [nvarchar] (50) NOT NULL CONSTRAINT [ch_Test_Table_A_a_attribute_1] CHECK ([a_attribute_1] = 'Cookie') , [a_attribute_2] [nvarchar] (50) NOT NULL , CONSTRAINT [fk_comp_r_Test_Table_B_r_Test_Table_A] FOREIGN KEY ([a_attribute_1], [a_attribute_2]) REFERENCES [r_Test_Table_A]([a_attribute_1], [a_attribute_2]) ) 中查找值。 (希望我对问题描述不太感兴趣)

    public class Recipe extends ActionBarActivity  {
        TextView tex;
        Button but;
        String URL ="http://10.0.2.2/my_prog/www/mango/android/publico.php";
        HttpClient Client = new DefaultHttpClient();
        InputStream is =null;
        int response;
        @Override
        protected void onCreate(Bundle savedInstanceState){ 

         super.onCreate(savedInstanceState);
         setContentView(R.layout.recipe);

         but=(Button) findViewById(R.id.button1);
         tex=(TextView) findViewById(R.id.textView1);
         but.setOnClickListener(new OnClickListener(){
            @Override
             public void onClick(View arg0) {
                    int len = 500; 
                    try{
                        String Url = "http://10.0.2.2/my_prog/www/mango/android/publico.php";                                        

                        URL url = new URL(Url);
                        HttpURLConnection conn =(HttpURLConnection) url.openConnection();
                        conn.setReadTimeout(10000);
                        conn.setConnectTimeout(15000);
                        conn.setDoInput(true);
                        conn.connect();
                        response = conn.getResponseCode();
                        is = conn.getInputStream();

                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        StringBuffer sb =new StringBuffer();
                        String line="";
                        while ((line =br.readLine()) != null){
                            sb.append(line);                                
                        }
                        String data = sb.toString();
                        tex.setText(data);

                    }
catch(Exception ex){
                        tex.setText("problema di connetion" + response);    
                    }                       
                }

            private String readIt(InputStream is, int len) {
                Reader reader= null;
                try{
                reader =new InputStreamReader(is, "UTF-8");
                char[] buffer =new char[len];
                reader.read(buffer);
                return new String(buffer);
                }
                catch(Exception ex){
                    return new String("problem on convert");    
                }
            }            
            });  
        }
    }

0 个答案:

没有答案