简体   繁体   English

用主键和2个外键创建表并出错

[英]creating table with primary key and 2 foreign keys with error

I'm trying to create a table that has a composite PK constraint using Rep_ID, Store_ID, and Quarter and I'm trying to create a FK constraint on Rep_ID and Store_ID 我正在尝试使用Rep_ID,Store_ID和Quarter创建具有复合PK约束的表,并且正在尝试在Rep_ID和Store_ID上创建FK约束

This is my statement: 这是我的声明:

CREATE TABLE REP_CONTRACTS( Store_ID INT(8), Name INT(5), Quarter CHAR(3), Rep_ID INT(5), PRIMARY KEY (Rep_ID, Store_ID, Quarter), Rep_ID INT REFERENCES BOOK_STORES(Rep_ID), Store_ID INT REFERENCES BOOK_STORES(Store_ID) );

These are my tables: 这些是我的表:

Book Stores: 书店:

Column Name Datatype Constraint Comments Store_ID INT(8) PRIMARY KEY column Name VARCHAR(30) Should be UNIQUE and NOT NULL Contact VARCHAR(20) Rep_ID INT(5)

Rep Contracts 销售合同

Column Name DataType Store_ID INT(8) Name INT(5) Quarter CHAR(3) Rep_ID INT(5)

I have already created the book store table, I'm trying to create the rep contracts table 我已经创建了书店表,我正在尝试创建销售代表合同表

I also get the error Duplicate column name 'Rep_ID'. Add a differentiating column alias. 我也收到错误Duplicate column name 'Rep_ID'. Add a differentiating column alias. Duplicate column name 'Rep_ID'. Add a differentiating column alias. when running this query 运行此查询时

you are declaring REPID twice in the table, which is why you are getting the duplication error. 您在表中两次声明了REPID,这就是为什么您收到重复错误的原因。 You may also want to create the column "Store ID" before using it in the Primary Key Statement. 您可能还需要在“主键语句”中使用“ Store ID”列之前将其创建。

CREATE TABLE REP_CONTRACTS(
Store_ID INT(8),
Name INT(5),
Quarter CHAR(3),
Rep_ID INT(5) REFERENCES BOOK_STORES(Rep_ID),
Store_ID INT REFERENCES BOOK_STORES(Store_ID),
PRIMARY KEY (Rep_ID, Store_ID, Quarter)
);

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

相关问题 使用2个外键作为主键创建表时出现MySQL语法错误 - MySQL syntax error when creating a table with 2 foreign keys as primary key 创建具有引用两个外键的复合主键的表 - Creating a table with composite primary key referencing two foreign keys MySQL:创建一个带有外键的表,该表引用由外键组成的主键 - MySQL: Creating a table that with a foreign key that references a primary key made up of foreign keys 如何在作为主键和候选键的表中引用多个外键? - How to reference multiple foreign keys in a table that is a primary key and candidate keys? MySQL使用外键将数据插入表中,外键不是参考表的主键 - MySQL insert data into table with foreign keys that are no the primary key of the reference table MySQL:没有主键的表(索引,外键)优化 - MySQL: optimization of table (indexing, foreign key) with no primary keys 当主键在不同的表中时,可以引用两个外键吗? - Is it ok to reference two foreign keys when the primary key is in a different table? 自动创建由其主键和2个其他外键组成的表 - Create automatically table that is composed by its primary key and 2 other foreign keys mysql表中的多个外键指向相同的主键 - mysql Multiple Foreign Keys in a Table to the Same Primary Key 具有链接到同一主键的多个外键的表 - Table with multiple foreign keys linking to the same primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM