mysql> create table dept(dept_id char(4) primary key,col_id char(2) not null,dep
t_name varchar(30),constraint dept_fk foreigh key(colid) references college(coli
d));ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'forei
gh key(colid) references college(colid))' at line 1问题出在哪?
怎么改正?谢谢

解决方案 »

  1.   

    为什么不看一下语法?! MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
      

  2.   

    [CONSTRAINT [symbol]] FOREIGN KEY
            [index_name] (index_col_name,...) [reference_definition]
      

  3.   

    mysql> create table dept(dept_id char(4) primary key,dept_name varchar(30),col_i
    d char(2) not null references college(col_id));
    Query OK, 0 rows affected (0.08 sec)这样貌似就ok了
      

  4.   

    语句说明上的格式是这样。
    [CONSTRAINT [symbol]] FOREIGN KEY
            [index_name] (index_col_name,...) [reference_definition]
    mysql> create table dept(
        ->  dept_id char(4) primary key,
        ->  col_id char(2) not null,
        ->  dept_name varchar(30),
        ->  constraint dept_fk FOREIGN KEY(col_id) references college(colid)
        -> );
    Query OK, 0 rows affected (0.16 sec)mysql>
    1.  foreigh key 显然拼写错误!
    2.   foreigh key(colid)  你的表中哪有这个字段?
    3. 建议找本数据库的教材抽时间浏览一下。当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖