CREATE DATABASE 学生信息管理数据库
CREATE TABLE Student
(
Sno char(10)primary key,
Sname char(10)not null,
Ssex char check(Ssex='男' OR Ssex='女'),
Sgrade char(10)not null,
Sclass char(10)not null,
Smajor char(20)default'计算机科学与技术', 
Sdept char(20)default'信息工程学院',
)
CREATE TABLE SC
(
Sno char(10)foreign key(Sno)references Student(Sno),
Sname char(10)foreign key(Sname)references Student(Sname),
Scourse char(20)not null,
Grade tinyint check(Grade>=0 and Grade<=100)
)
显示的错误时:在被引用表 'Student' 中没有与外键 'FK__SC__Sname__192BAC54' 的引用列的列表匹配的主键或候选键。

解决方案 »

  1.   

    你学生表的Sname char(10)not null,这个字段又不是主键,你凭什么在表SC
    Sname char(10)foreign key(Sname)references Student(Sname)字段创建外键去引用他?
    主都没有,引用什么??
      

  2.   

    CREATE TABLE SC
    (
    Sno char(10)foreign key(Sno)references Student(Sno),
    Sname char(10)foreign key(Sname)references Student(Sname),  --通常引用了主表中的主键列就可以建立两表间的关系。如果要限定名称应在主表中建立唯一性约束
    Scourse char(20)not null,
    Grade tinyint check(Grade>=0 and Grade<=100)
    )
      

  3.   

    去掉:
    Sname char(10)foreign key(Sname)references Student(Sname),
    这个作为外键就可以了,
    不然就要:
    Sno char(10)primary key,
    Sname char(10)primary key,
      

  4.   

    Sname char(10) 去掉后面的引用.设置外键引用的列在另一个表中需要是主键。而你的sname在学生表中不时主键
      

  5.   

    当给一个表设置外键时,首先要设置外键的这个属性必须是另外一个表的主键,这样才是主外键约束,你的Student表里Sname不是Student表的主键,所以在SC表中不能把它设置为外键。如果想设置外键的话在Student表中把Sname也设置为主键。
    但是也没必要你只要把Sno设置为外键就可以了。