简体   繁体   English

想将外键定义为mysql数据库表中主键的一部分

[英]want to define foreign key as a part of primary key in mysql database table

there is my queries.有我的疑问。 why i am getting error?为什么我收到错误?

CREATE TABLE researcher(
    RID INT AUTO_INCREMENT,
    FIRST_NAME VARCHAR(30),
    LAST_NAME VARCHAR(30),
    COUNTRY VARCHAR(30),
    WORKING_YEAR INT,
    Experience INT DEFAULT 0,
    CURRENT_WORK VARCHAR(30) DEFAULT "no entry",
    CONSTRAINT con1 PRIMARY KEY(RID)
)

then然后

CREATE TABLE IF NOT EXISTS reviews
(
    RID INT,
    REVIEW_ID INT AUTO_INCREMENT,
    REVIEW_TIME TIMESTAMP DEFAULT "0000-00-00 00:00:00",
    CONSTRAINT for3 FOREIGN KEY(RID) REFERENCES researcher(RID),
    CONSTRAINT con5 PRIMARY KEY(RID, REVIEW_ID)
 )

then MySQL said:然后 MySQL 说:

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

where is my fault?我的错在哪里?

If use AUTO_INCREMENT , it need to provide primary key on it如果使用 AUTO_INCREMENT ,则需要在其上提供主键

example例子

CREATE TABLE IF NOT EXISTS reviews
(
    RID INT,
    REVIEW_ID INT AUTO_INCREMENT,
    REVIEW_TIME TIMESTAMP DEFAULT "0000-00-00 00:00:00",
    CONSTRAINT for3 FOREIGN KEY(RID) REFERENCES researcher(RID),
    CONSTRAINT con5 PRIMARY KEY(REVIEW_ID)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有主键而是外键的MySQL表 - Mysql table with no primary key but a foreign key mysql外键仅引用复合主键的一部分 - mysql Foreign key referencing only part of composite primary key MySql我可以使用外键作为主键的一部分吗? - MySql Can i use a foreign key as PART of my primary key? 组合外键由父表中的主键的一部分组成 - Composite foreign key made up of part of primary key in parent table 外键是否可以成为另一个表的复合主键的一部分? - Can a Foreign Key be part of a Composite Primary Key for another table? 外键作为mysql中的主键 - Foreign key as primary key in mysql mysql连接表,其中外键是同一表的主键 - mysql join table where foreign key is primary key of same table MySQL Workbench:如何为具有3个主键(A,B,C)的表定义2个外键关系(A,B)和(B,C) - MySQL Workbench: How to define 2 foreign key relationships (A,B) and (B,C) for a table with 3 primary keys (A,B,C) 如果 FK 表具有复合主键(FK 不是组合的一部分),则外键作为主键不起作用 - Foreign key as primary key doesn't work if FK table has composite primary key (FK is not part of the composite) MySQL-外键受同一表中的主键约束,错误#1452 - MySQL - foreign key constrained by primary key in same table, error #1452
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM