create table t_class(
   id int not null  primary key,
   name varchar(250)
);create table t_stu (
    id int not null primary key,
    name varchar(250),
    cid int not null,
    foreign key(cid) references t_class(id)
);   表结构。
t_class表结构
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   | PRI |         |       |
| name  | varchar(250) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
============================================================
t_stu表结构
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   | PRI |         |       |
| name  | varchar(250) | YES  |     | NULL    |       |
  cid   | int(11)      |MUL   |     |         |       |
+-------+--------------+------+-----+---------+-------+为什么关系没有创建起来,我在插入insert into t_stu(name,cid) values("admin",1); 竟然没有报错。
  why?   

解决方案 »

  1.   

      插入没有报错。我是在mysql里面创建的。
      

  2.   

    "为什么关系没有创建起来,我在插入insert into t_stu(name,cid) values("admin",1); 竟然没有报错。
    why? "
    对您的问题不理解
      

  3.   

       我的意思就是在t_class 表里面存入数据,我直接先在t_st表里面插入,在插入cid这个值不是就会报错吗?
      

  4.   


    我这个建的不是主外键约束吗?  在t_class 表里面id 字段如果没有值,在我插入t_stu表中的cid不就是会报错吗?我这样理解错了吗?
      

  5.   

    提问时最好注明你用的什么数据库,否则大家会浪费很多时间来猜。楼主用的是MYSQL吧!MYSQL中的MYISAM存储引擎不支持外键。你需要使用 innodb 存储引擎。
      

  6.   

    super_bohua (super_bohua)
      '截至2010-03-30 13:12:09  用户结帖率0.00% 当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html