我有2个表 
a(a1,a2)
b(b1,b2)
表里的属性a1,a2,b1是主键,然后添加关系a1对应b1为什么添加不了呢 老提示表的主键或者unique约束不匹配呢

解决方案 »

  1.   

    a1 和 a2 是复合主键,单独a1是可以重复的。
    b1是主键,是唯一的。
      

  2.   

    alter tb add constraint fk_tb_col foreign key(fkcol) references tb(fkcol)
    可能是你有一个表里有不唯一的值.
      

  3.   

    只能在a表里添加外键关系,使a1对应b1,不能在b表里添加外键关系。
      

  4.   

    没有重复呀 我直接拿我写的 大家帮我看看吧
    if exists (select * from sysdatabases where name='school')
    drop database school
    go
    create database school
    on
    (
    name='school_data',
    filename='e:\data\school_data.mdf',
    size=3mb,
    filegrowth=15%
    )
    log on
    (
    name='school_log',
    filename='e:\data\school_log.ldf',
    size=1mb,
    filegrowth=15%
    )
    go
    use school
    go
    if exists(select * from sysobjects where name='stu')
    drop table stu
    go
    create table stu
    (
    SNo int 
    constraint PK_Sno primary key,
    SName nvarchar(10) not null,
    Sex nvarchar(5) not null
    constraint CK_Sex check(Sex in('男','女')),
    Age int
    constraint CK_Age check(Age between 0 and 100),
    Native nvarchar(10) 
    )
    go
    if exists(select * from sysobjects where name='teacher')
    drop table teacher
    go
    create table teacher
    (
    TNO int
    constraint PK_TNO primary key,
    TName nvarchar(10) not null,
    Post varchar(5),
    Sex varchar(5)
    constraint CK_Sex1 check(Sex in('男','女')),
    Age int
    constraint CK_Age1 check(Age between 0 and 100)
    )
    go
    if exists(select * from sysobjects where name='course')
    drop table course
    go
    create table course
    (
    Cno int,
    CName nvarchar(10),
    Tno int
    constraint FK_Tno foreign key(Tno) references teacher(Tno),
    Period float,
    primary key(Cno,Tno)
    )
    go
    if exists(select * from sysobjects where name='study')
    drop table study
    go
    create table study
    (
    SNo int
    constraint FK_SNo2 foreign key(SNo) references stu(SNo), 
    CNo int
    constraint FK_CNo foreign key(CNo) references course(Tno),
    Grade int
    constraint CK_Grade check(grade between 0 and 100),
    primary key(Sno,Cno)
    )
    go
      

  5.   

        CNo int
        constraint FK_CNo foreign key(CNo) references course(Tno),
    这里去掉约束就不报错 加上就报错
      

  6.   

    CNo int
      constraint FK_CNo foreign key(CNo) references course(Cno)
      

  7.   


    ......我的意思是把
    CNo int
      constraint FK_CNo foreign key(CNo) references course(Tno),
    里的Tno改为Cno
      

  8.   

    你的数据库设计上有点小问题.
    将课程表中的课程编号与教师编号合并设置为主键本身没什么问题,但如果将该表的Cno与Study表中的单一列Cno建立主从关系就不行了,因为Cno不是主键.
    如果将Study表的Cno与课程表中的这个主键建立连接也不行,因为Cno只有一个列,而主键是两列.
    解决的办法有两个:
    一是课程表中只用Cno作主键,不同教师教授的课程分作不同的课程编号,可同一课程编号与可容纳几名教师.这样,可以将学习表中的课程号与该主键建立主从关系;
    二是在学习表中同样建立Tno列,将Cno,Tno两列与课程表中的由两列组成的主键建立主从关系,这样处理,课程表维持原样,学习表作改动,从理论上讲更符合你的要求.从数据库的角度看,既然你要与课程表建立连接,而课程表中决定课程的是课程编号和教师编号两个属性,那你的外键当然出得用同样的两个属性,否则外键是不正确的.
    如果用多个列作外键,约束必须定义为表级约束.
    create table study
    (
        SNo int
        constraint FK_SNo2 foreign key(SNo) references stu(SNo), 
        CNo int,
    Tno int,
        Grade int
        constraint CK_Grade check(grade between 0 and 100),
        primary key(Sno,Cno),
        constraint FK_CNo foreign key(CNo,Tno) references course(Cno,Tno),
    )
    go
      

  9.   

    这句话:
    一是课程表中只用Cno作主键,不同教师教授的课程分作不同的课程编号,可同一课程编号与可容纳几名教师.这样,可以将学习表中的课程号与该主键建立主从关系;
    中的"可"是笔误,应为:一是课程表中只用Cno作主键,不同教师教授的课程分作不同的课程编号,也可在同一课程编号下容纳几名教师.这样,可以将学习表中的课程号与该主键建立主从关系;
    抱歉.
      

  10.   

    CNO那个表不能是主键呢 我不明白了 首先课程表CNO和TNO要是主键吧,study表这张表如果没有CNO做主键,那一个学生只能考一门课程,因为SNO是主键呢 这样设计显然不合理呀
      

  11.   

    用您的话说,这样建立course和study表的主外键约束是不能被允许的对吗?
      

  12.   

    既然课程表以CNO和TNO合并组成了主键,那就意味着,不同教师即使讲同一课程,也属于不同的课程.因此,在学习表中,如果要对课程进行甑别,就必须利用课程表的主键,即在学习表中必须包含CNO和TNO两个列,以该两列对课程表的主键(亦为两列)作外键.
    这样的主外键关系才是被允许的.