sql问题(查询和关系) [英] sql problem (query and relationship)

查看:81
本文介绍了sql问题(查询和关系)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的sql中建立关系和查询????

how to make relationship and query in my sql?????

推荐答案

请检查以下内容:

http://dev.mysql.com/tech-resources/articles/mysql- enforcing-foreign-keys.html [ ^ ]
Please check the following:

http://dev.mysql.com/tech-resources/articles/mysql-enforcing-foreign-keys.html[^]


要在表之间创建关系,可以使用PRIMARY KEY和FOREIGN KEY.
依靠正在运行的引擎,您可能需要对查询进行非常细微的调整.

For creating a relation between the tables you can make use of PRIMARY KEY and FOREIGN KEY.
Depeding on which engine is being run you you might have to make a very minute adjustment to your query.

CREATE TABLE temp1(
       slNumber INT(4) NOT NULL AUTO_INCREMENT,
       .....
       PRIMARY KEY(slNumber)
)





CREATE TABLE temp2(
       slNumber INT(4) NOT NULL AUTO_INCREMENT,
       idNumber INT(4) NOT NULL AUTO_INCREMENT,
       .....
       PRIMARY KEY(idNumber),
       FOREIGN KEY(slNumber) REFERENCES temp1(slNumber)
)ENGINE=INNODB



指定



Specifying the

ENGINE=INNODB

是必需的,因为MYISAM引擎不支持FOREIGN KEY.

为了获得诸如一对一,一对多或多对多的关系详细信息,我建议您使用 MySql Workbench [ ^ ]因为它为您提供了一个GUI,可用于获取关系的详细信息.


在使用FOREIGN KEY之前,您还需要注意一些其他事项.
创建带有FOREIGN KEY的表后,始终建议使用

is necessary as MYISAM engine does not support the FOREIGN KEY.

For getting the relationship details like One-One, One-Many or Many-Many, i would suggest you make use of MySql Workbench[^] As it provides you with a GUI that can be used to get the details of relationship.


There are few more things that you need to be careful about before you make use of the FOREIGN KEY.
Its always advisable to make use of

ON DELETE CASCADE 

ON UPDATE CASCADE

.
并确保FOREIGN KEY的大小与PRIMATY KEY的大小匹配.


BR//
Harsha Narayana

after the table with the FOREIGN KEY is created.
And make sure that the size of the FOREIGN KEY matches the size of the PRIMATY KEY.


BR//
Harsha Narayana


这篇关于sql问题(查询和关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆