简体   繁体   中英

setting up primary and foreign key

i've tried to set the primary and foreign key using the method that i learn at http://fellowtuts.com/php/setting-up-foreign-key-in-phpmyadmin/ but an error came up stating that

#1025 - Error on rename of '.\\sistem_akaun\\#sql-1b70_7d' to '.\\sistem_akaun\\detail_akaun' (errno: 150 - Foreign key constraint is incorrectly formed)

can i know what's the problem here?sorry if this question sounds stupid,just a newbie

Check to make sure that the Primary Key you are referencing exists. If, in your main table, you have id_main = 0 on your main table, where id_main is a foreign key referencing id_ref (which is the primary key of the other table) but you have reference ref_id = 1 and no 0 value, you will get an error.

Check to make sure your foreign key is the primary key of the other table.

Check to make sure they are the same data type, length, unsigned status. Sometimes these matter sometimes not.

Sometimes I've had trouble where both Foreign Key and Primary Key are both named "id". This can be a problem depending on what software/methods you are using.

@itsfawwaz, You can also do this by below way. Check below example.

Example : (Table Orders)

CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)

Above is sample example, you can use your own table fields !

Let me know if still you have any issues.

You may find it easier to specify a foreign key manually, in SQL.

ALTER TABLE Table
ADD FOREIGN KEY (Column) 
REFERENCES TableToReference (ColumnToReference) 

(Where Table is the table you wish to add a foreign key to)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM