<legend id='5e4M2'><style id='5e4M2'><dir id='5e4M2'><q id='5e4M2'></q></dir></style></legend>

      <tfoot id='5e4M2'></tfoot>

      <small id='5e4M2'></small><noframes id='5e4M2'>

    1. <i id='5e4M2'><tr id='5e4M2'><dt id='5e4M2'><q id='5e4M2'><span id='5e4M2'><b id='5e4M2'><form id='5e4M2'><ins id='5e4M2'></ins><ul id='5e4M2'></ul><sub id='5e4M2'></sub></form><legend id='5e4M2'></legend><bdo id='5e4M2'><pre id='5e4M2'><center id='5e4M2'></center></pre></bdo></b><th id='5e4M2'></th></span></q></dt></tr></i><div id='5e4M2'><tfoot id='5e4M2'></tfoot><dl id='5e4M2'><fieldset id='5e4M2'></fieldset></dl></div>

        <bdo id='5e4M2'></bdo><ul id='5e4M2'></ul>

      1. 使用复合主键作为外键

        Use Composite Primary Key as Foreign Key(使用复合主键作为外键)
        <legend id='hl7dW'><style id='hl7dW'><dir id='hl7dW'><q id='hl7dW'></q></dir></style></legend>

          <tbody id='hl7dW'></tbody>

              <bdo id='hl7dW'></bdo><ul id='hl7dW'></ul>

            • <i id='hl7dW'><tr id='hl7dW'><dt id='hl7dW'><q id='hl7dW'><span id='hl7dW'><b id='hl7dW'><form id='hl7dW'><ins id='hl7dW'></ins><ul id='hl7dW'></ul><sub id='hl7dW'></sub></form><legend id='hl7dW'></legend><bdo id='hl7dW'><pre id='hl7dW'><center id='hl7dW'></center></pre></bdo></b><th id='hl7dW'></th></span></q></dt></tr></i><div id='hl7dW'><tfoot id='hl7dW'></tfoot><dl id='hl7dW'><fieldset id='hl7dW'></fieldset></dl></div>
              • <tfoot id='hl7dW'></tfoot>

                <small id='hl7dW'></small><noframes id='hl7dW'>

                1. 本文介绍了使用复合主键作为外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何使用复合主键作为外键?看起来我的尝试不起作用.

                  How can I use a composite primary key as a foreign key? It looks like my attempt does not work.

                  create table student
                  (
                  student_id varchar (25) not null ,
                  student_name varchar (50) not null ,
                  student_pone int ,
                  student_CNIC varchar (50),
                  students_Email varchar (50),
                  srudents_address varchar(250),
                  dept_id varchar(6),
                  batch_id varchar(4),
                  FOREIGN KEY (dept_id) REFERENCES department(dept_id),
                  FOREIGN KEY (batch_id) REFERENCES batch(batch_id),
                  CONSTRAINT pk_studentID PRIMARY KEY (batch_id,dept_id,student_id) )
                  

                  <小时>

                  create table files
                  (
                  files_name varchar(50) not null ,
                  files_path varchar(50),
                  files_data varchar(max),
                  files_bookmarks xml ,
                  FOREIGN KEY (pk_studentID ) REFERENCES student(pk_studentID ),
                  CONSTRAINT pk_filesName PRIMARY KEY (files_name) )
                  

                  推荐答案

                  行:

                  FOREIGN KEY (pk_studentID ) REFERENCES student(pk_studentID ),
                  

                  错了.您不能像那样使用 pk_studentID,这只是父表中 PK 约束的名称.要将复合主键用作外键,您必须将具有相同数据类型的相同数量的列(组成 PK)添加到子表中,然后在 FOREIGN KEY<中使用这些列的组合/code> 定义:

                  is wrong. You can't use pk_studentID like that, this is just the name of the PK constraint in the parent table. To use a compound Primary Key as Foreign Key, you'll have to add the same number of columns (that compose the PK) with same datatypes to the child table and then use the combination of these columns in the FOREIGN KEY definition:

                  CREATE TABLE files
                  (
                    files_name varchar(50) NOT NULL, 
                  
                    batch_id varchar(4) NOT NULL,         --- added, these 3 should not
                    dept_id varchar(6) NOT NULL,          --- necessarily be NOT NULL
                    student_id varchar (25) NOT NULL,     --- 
                  
                    files_path varchar(50),
                    files_data varchar(max),              --- varchar(max) ??   
                    files_bookmarks xml,                  --- xml ??
                                                          --- your question is tagged MySQL, 
                                                          --- and not SQL-Server
                  
                    CONSTRAINT pk_filesName 
                      PRIMARY KEY (files_name),
                  
                    CONSTRAINT fk_student_files                     --- constraint name (optional)
                      FOREIGN KEY (batch_id, dept_id, student_id)  
                        REFERENCES student (batch_id, dept_id, student_id)
                  ) ENGINE = InnoDB ;
                  

                  这篇关于使用复合主键作为外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)
                  • <bdo id='a0Cg7'></bdo><ul id='a0Cg7'></ul>

                    <small id='a0Cg7'></small><noframes id='a0Cg7'>

                        <tbody id='a0Cg7'></tbody>
                      <tfoot id='a0Cg7'></tfoot>

                        <legend id='a0Cg7'><style id='a0Cg7'><dir id='a0Cg7'><q id='a0Cg7'></q></dir></style></legend>

                          • <i id='a0Cg7'><tr id='a0Cg7'><dt id='a0Cg7'><q id='a0Cg7'><span id='a0Cg7'><b id='a0Cg7'><form id='a0Cg7'><ins id='a0Cg7'></ins><ul id='a0Cg7'></ul><sub id='a0Cg7'></sub></form><legend id='a0Cg7'></legend><bdo id='a0Cg7'><pre id='a0Cg7'><center id='a0Cg7'></center></pre></bdo></b><th id='a0Cg7'></th></span></q></dt></tr></i><div id='a0Cg7'><tfoot id='a0Cg7'></tfoot><dl id='a0Cg7'><fieldset id='a0Cg7'></fieldset></dl></div>