Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

mysql - Creating A Join In SQL Xampp

I am trying to create a join between my database tables customers and staff. However getting an error: Can't create table staff_db.customer_orders (errno: 121 "Duplicate key on write or update").

CREATE TABLE staff(
staff_id int(12) not null,
staff_name varchar(20),
staff_address varchar(32),
staff_department varchar(20),
PRIMARY KEY (staff_id)
);
CREATE TABLE customers(
customer_id int(12) not null,
customer_name varchar(20),
customer_address varchar(32),
customer_product varchar(25),
staff_id int(12) not null,
FOREIGN KEY staff_fk(staff_id) REFERENCES staff(staff_id),
PRIMARY KEY (customer_id)
);
CREATE TABLE customer_orders(

customer_id int(12) not null,
staff_id int(12) not null,
FOREIGN KEY customer_fk(customer_id) REFERENCES customers(customer_id),
FOREIGN KEY staff_fk(staff_id) REFERENCES staff(staff_id),
PRIMARY KEY (customer_id,staff_id)
);
question from:https://stackoverflow.com/questions/65871425/creating-a-join-in-sql-xampp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your customer_orders table, you named the foreign key " FOREIGN KEY staff_fk (staff_id) REFERENCES staff (staff_id) " like the one in your customers table " FOREIGN KEY staff_fk (staff_id) REFERENCES staff (staff_id) ". Names must be unique. Change the name to one of the two and your problem will normally be solved.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...