简体   繁体   English

设置主键和外键

[英]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 我尝试使用我在http://fellowtuts.com/php/setting-up-foreign-key-in-phpmyadmin/中学到的方法来设置主键和外键,但是出现了一个错误,指出

#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. 如果在主表中主表上的id_main = 0,其中id_main是引用id_ref的外键(这是另一个表的主键),但您的引用ref_id = 1且没有0值,则将得到一个错误。

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". 有时我在将外键和主键都命名为“ id”时遇到了麻烦。 This can be a problem depending on what software/methods you are using. 根据您使用的软件/方法,这可能是一个问题。

@itsfawwaz, You can also do this by below way. @itsfawwaz,您也可以按照以下方式进行操作。 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. 您可能会发现在SQL中手动指定外键会更容易。

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

(Where Table is the table you wish to add a foreign key to) (“表”是您要向其中添加外键的表)

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

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