另加一个行业内公司对照表即可:行业公司表:T (id pk 自增,T_id fk,c_id fk) , 这里,t_id和c_id都可重复。

解决方案 »

  1.   

    传统的方法是这样
    实体表:实体的ID NAME TEL.....等等
    公司表:实体ID,行业ID,公司在此行业的一些其他属性
    行业分类表:行业ID,NAME....比较极端的做法
    公司表(companyinfo):C_id, C_name, C_tel, .... ,行业分类ID的字符串列表,用固定字符分隔。
       行业分类(trade): T_id, T_name, T_count(存有几家公司)
      

  2.   


    搜索某行业内的公司: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字段应该删去。
      

  3.   

    --同意楼上,个人建议这样:行业公司表:T (T_id fk,c_id fk)  --T_id + c_id 做复合主键
      

  4.   

    victorycyz(中海), zjcxc(邹建):
      为什么要把 t_count 字段删除?有什么影响吗?  我加上这个字段的本意是用来加快搜索速度。因为这样就不用 SELECT COUNT(c_Id) FROM ... 了。
      

  5.   

    哦,多谢。可这条语句我不是很懂。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='要查询的行业名'
      

  6.   

    按CtrL键,选中两个字段,再按"钥匙"
      

  7.   

    --这样应该容易理解了吧?select 公司名=c_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='要搜索的行业'
      

  8.   

    --或者这样写select 公司名=c_name
    from 公司行业关系表 ab,公司表 a,行业分类 b
    where ab.C_id=a.C_id
    and ab.T_id=b.T_id
    and b.T_name='要搜索的行业'
      

  9.   

    还是提示"不能插入重复键"。请问两个id是不是 FOREIGN KEY ??,创建不出来。
      

  10.   

    --建表示例(在查询分析器中执行,如果看不明白,建好后,再到企业管理器中去看)--公司表
    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))
      

  11.   

    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
    )