求助:如何设计"一对多"关系表?? 另加一个行业内公司对照表即可:行业公司表:T (id pk 自增,T_id fk,c_id fk) , 这里,t_id和c_id都可重复。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 传统的方法是这样实体表:实体的ID NAME TEL.....等等公司表:实体ID,行业ID,公司在此行业的一些其他属性行业分类表:行业ID,NAME....比较极端的做法公司表(companyinfo):C_id, C_name, C_tel, .... ,行业分类ID的字符串列表,用固定字符分隔。 行业分类(trade): T_id, T_name, T_count(存有几家公司) 搜索某行业内的公司:select c_namefrom trade a join (t join companyinfo b on t.c_id=b.c_Id) on a.t_id=t.t_id where t_name='要查询的行业名'另,trade表中的t_count字段应该删去。 --同意楼上,个人建议这样:行业公司表:T (T_id fk,c_id fk) --T_id + c_id 做复合主键 victorycyz(中海), zjcxc(邹建): 为什么要把 t_count 字段删除?有什么影响吗? 我加上这个字段的本意是用来加快搜索速度。因为这样就不用 SELECT COUNT(c_Id) FROM ... 了。 哦,多谢。可这条语句我不是很懂。select c_namefrom trade a join (t join companyinfo b on t.c_id=b.c_Id) on a.t_id=t.t_id where t_name='要查询的行业名' 按CtrL键,选中两个字段,再按"钥匙" --这样应该容易理解了吧?select 公司名=c_namefrom 公司行业关系表 ab join 公司表 a on ab.C_id=a.C_id join 行业分类 b on ab.T_id=b.T_idwhere b.T_name='要搜索的行业' --或者这样写select 公司名=c_namefrom 公司行业关系表 ab,公司表 a,行业分类 bwhere ab.C_id=a.C_id and ab.T_id=b.T_id and b.T_name='要搜索的行业' 还是提示"不能插入重复键"。请问两个id是不是 FOREIGN KEY ??,创建不出来。 --建表示例(在查询分析器中执行,如果看不明白,建好后,再到企业管理器中去看)--公司表create table companyinfo(C_id int identity(1,1) primary key,C_name nvarchar(50) not null,C_tel nvarchar(20))--行业分类create table trade(T_id int identity(1,1) primary key,T_name nvarchar(50) not null,T_count int)--公司行业关系表create table companyinfo_trade(C_id int not null constraint FK_companyinfo_trade_companyinfo foreign key references companyinfo(C_id) on delete cascade,T_id int not null constraint FK_companyinfo_trade_trade foreign key references trade(T_id) on delete cascade,constraint PK_companyinfo_trade primary key(C_id,T_id)) create table companyinfo_trade(C_id int not null constraint FK_companyinfo_trade_companyinfo foreign key references companyinfo(C_id) on delete cascade,--与companyinfo的外键关系T_id int not null constraint FK_companyinfo_trade_trade foreign key references trade(T_id) on delete cascade, --与trade的外键关系constraint PK_companyinfo_trade primary key(C_id,T_id) --复合主键:C_id+T_id) 跨机器取数据 怎么做性能好 sql语句问题 急!!!!!! 多表查询第3问还没有解决,高手们到哪里去了? 求一时间段查询算法 有答案就决贴----如何取表中指定行 表之间的数据复制 100分求帮改个存储过程.. 批量处理数据 sql server2000全文索引 如何实现中英文混合搜索? 请教高手: SQLserver7.0中有没有类似inStr(“x,x”, ",")定位字符位置的函数, 如果没有我们怎幺分割字符: 例如下面的示例: 不知道有没有人写过这样的sql,应该是会者不难,难者不会的问题... 关于数据插入问题
实体表:实体的ID NAME TEL.....等等
公司表:实体ID,行业ID,公司在此行业的一些其他属性
行业分类表:行业ID,NAME....比较极端的做法
公司表(companyinfo):C_id, C_name, C_tel, .... ,行业分类ID的字符串列表,用固定字符分隔。
行业分类(trade): T_id, T_name, T_count(存有几家公司)
搜索某行业内的公司:select c_name
from trade a join (t join companyinfo b on t.c_id=b.c_Id) on a.t_id=t.t_id
where t_name='要查询的行业名'
另,trade表中的t_count字段应该删去。
为什么要把 t_count 字段删除?有什么影响吗? 我加上这个字段的本意是用来加快搜索速度。因为这样就不用 SELECT COUNT(c_Id) FROM ... 了。
from trade a join (t join companyinfo b on t.c_id=b.c_Id) on a.t_id=t.t_id
where t_name='要查询的行业名'
from 公司行业关系表 ab
join 公司表 a on ab.C_id=a.C_id
join 行业分类 b on ab.T_id=b.T_id
where b.T_name='要搜索的行业'
from 公司行业关系表 ab,公司表 a,行业分类 b
where ab.C_id=a.C_id
and ab.T_id=b.T_id
and b.T_name='要搜索的行业'
create table companyinfo(
C_id int identity(1,1) primary key,
C_name nvarchar(50) not null,
C_tel nvarchar(20))--行业分类
create table trade(
T_id int identity(1,1) primary key,
T_name nvarchar(50) not null,
T_count int)--公司行业关系表
create table companyinfo_trade(
C_id int not null constraint FK_companyinfo_trade_companyinfo foreign key
references companyinfo(C_id) on delete cascade,
T_id int not null constraint FK_companyinfo_trade_trade foreign key
references trade(T_id) on delete cascade,
constraint PK_companyinfo_trade primary key(C_id,T_id))
C_id int not null constraint FK_companyinfo_trade_companyinfo foreign key
references companyinfo(C_id) on delete cascade,--与companyinfo的外键关系
T_id int not null constraint FK_companyinfo_trade_trade foreign key
references trade(T_id) on delete cascade, --与trade的外键关系
constraint PK_companyinfo_trade primary key(C_id,T_id) --复合主键:C_id+T_id
)