Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
z
create table customer(
CMobile_no       int ,
Customer_id       int,
Customer_Address  varchar(20),
Fname        varchar(20),
Minit        varchar(20),
Lname        varchar(20),
Customer_id2    int,
constraint customer_pk
primary key (Customer_id2),

);

create table Buysfrom(
Cid int,
  
  Bid int,

 Hrs  int,
 CONSTRAINT buys_pk
 primary key (Cid,Bid),

 Constraint customer_Buys_pk
 foreign key (Cid) references customer(Customer_id2),
 constraint Branch_Buys_fk
 foreign key (Bid)references Branch(Branch_id),


);


error is in customer_id2 in buys from



The error message is as follow:
foreign key customer buys pk referenced invalid coloumn customer_id2 in refrenced table customer

What I have tried:

change customer_id2 in table customer but it doesnot affect any thing
Posted
Updated 6-Dec-22 19:54pm

Sigh. Just read the error message. It's telling you the column "customer_id2" doesn't exist in the "customer" table. What you do about that depends on what you called the primary key column in the "customer" table.
 
Share this answer
 
If I run your two queries in the order you present them unchanged, I get an error related to the Branch table, which doesn't exist.

So your error indicates that you ran the second code first, and the first code second - so the table referenced in the second code didn't exist yet.

SQL will not let you "forward reference" tables: it doesn't "know" that you will create the table later and so complains that the data integrity cannot be maintained.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900