请教:数据库中,表A中的一个列既是主键又是外键,而且外键是在另外一个表B中是自动标识增长的主键,这样做可以吗?为什么?有什么好处或者坏处?请指点

解决方案 »

  1.   


    create table tb1
    (
    id1 int identity(1,1),
    val1 varchar(10)
    )
    create table tb2
    (
    id1 int,
    val2 varchar(10)
    )alter table tb1 add constraint pk_tb1 primary key(id1);alter table tb2 alter column id1 int not null;
    alter table tb2 add constraint pk_tb2 primary key(id1);alter table tb2 add constraint fk_tb2 foreign key(id1) references tb1(id1) ON UPDATE CASCADE ; 
    是可以的,但是为什么要这么做呢?一般都是组合主键才会创建外键
    这样这两个表好像没有必要啊,除非只是为了备份需要
      

  2.   

    --TRY 就知道了if object_id('tb')is not null drop table tb
    go
    if object_id('ta')is not null drop table ta
    go
    create table ta(ID int identity primary key ,[name] varchar(10))
    go
    create table tb(id int primary key foreign key references ta(ID) on update cascade,[test] varchar(10))
      

  3.   

    我初学,不知道这样做是不是合理,应用背景是这样的:
      node表:nodeID,fk,自增
      store表:storeID,fk,pk(refrence node(nodeID))我想知道这样在Store进行插入删除等数据库操作的时候是不是可行,因为有人说不行,却没说为什么,我很疑惑,想请各位高手能帮我解释一下
      

  4.   

     更正一下
      node表:nodeID,pk,自增 
      store表:storeID,pk,fk(refrence node(nodeID))
      

  5.   

    on cascade delete
     on cascade update
      

  6.   

    primary key 确保实体完整性,foreign key 确保引用完整性。在 store 表上执行插入/更新操作时,需要保证插入行的 storeID 的值是 node 表的 nodeID 列值中的一个,并且在 store 表中是唯一的。而在 store 表上执行删除操作时,没有限制。