1、在数据库窗口中,单击“创建”中“表格”中的“表设计”,打开表设计窗口。
2、按照需要设计表“1”的表结构,完成后关闭表设计, 命名表1为“1”按需求确定字段大小以及是否允许空填。
3、重复上述 *** 作,建立表“2”。
4、单击“数据库工具”选项卡中“关系”中的“关系按钮”,打开“显示表对话框”。
5、在显示的两个表中,将“2”中的“数据”拖到“1”中数据字段上,在d出的编辑关系对话框中单击创建。建立关系完成,关闭“关系”窗口即可。
就是两个表只能单独查询。
只用在查询的时候用两个表的查询就可以了,一般表的id都是自动生成的,你在查询的时候可以用一个中间表或者视图来实现,将所需要的信息写到里面去,一般是不建议把第一个表的id直接写到另外一个里面去,那样就有两个查询的条件了,也就是你的id和name,那样是没用的,不利于表之间的优化的,最好是在第一次存取的时候就将表的字段生成,只进行行内的修改和整体的查询输出。
用updatex2setuid=(selectidfromx1wherex1.name=z2.name),来实现就好了,其实吧,我建议你把一个表里面的name干掉就留一个表的,然后用id做他们之间的主键来实现他们之间的关联,这个在数据库方面是通用的,现在数据库都需要优化,尤其是在多表查询的时候,所以你还是用心的研究一下,最好不要重复多个字段在表之间的重复查询,结果容易重复,所以我还是建议你把表优化,而不是考虑把值加进去,做到用什么就从表之间查什么就好了。
create table node_tree( id int not null auto_increment primary key, node_name varchar(128) not null default '', up_node_id int, node_level char(1) )ENGINE=InnoDB default charset=utf8 collate=utf8_swedish_ci
insert into node_tree(node_name,up_node_id,node_level) values('jx',null,'1'),('jx.webserver',1,'2'),('jx.webserver.nginx1', 2, '3'), ('jx.logserver', 1, '2')
select
node_tree1.id as 主表ID,
node_tree1.name as 主表名字,
node_tree2.name as 从表名字,
node_tree2.up_id as 从表上级ID
from node_tree1, node_tree2
where node_tree1.name='jx'
select
node_tree1.id as 主表ID,
node_tree1.node_name as 主表名字,
node_tree2.node_name as 从表名字,
node_tree2.up_node_id as 从表上级ID
from node_tree as node_tree1, node_tree as node_tree2
where node_tree1.node_name='jx'




