create table USERS(
ID int(8) not null primary key,
USERNAME varchar(40),
PASSWORD varchar(40)
);create table EMAIL(
USERID int(8),
EMAIL varchar(200),
foreign key (USERID) references USERS (ID)
);可以看得出来USERS跟EMAIL表有主外键关联的关系。平时我们在建表的同时总有个习惯就是drop table if exists  表名
于是我就有了定向思维,分别在两张表前加了一句。变成了这样:
drop table if exists USERS
create table USERS(
ID int(8) not null primary key,
USERNAME varchar(40),
PASSWORD varchar(40)
);
drop table if exists EMAIL
create table EMAIL(
USERID int(8),
EMAIL varchar(200),
foreign key (USERID) references USERS (ID)
);然后就报错了哪位达人能告诉我应该怎么写能达到预期的效果???

解决方案 »

  1.   

    set @@foreign_key_checks=off;mysql> set @@foreign_key_checks=off;
    Query OK, 0 rows affected (0.00 sec)mysql> drop table if exists USERS;
    Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> create table USERS(
        -> ID int(8) not null primary key,
        -> USERNAME varchar(40),
        -> PASSWORD varchar(40)
        -> );
    Query OK, 0 rows affected (0.08 sec)mysql> drop table if exists EMAIL;
    Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> create table EMAIL(
        -> USERID int(8),
        -> EMAIL varchar(200),
        -> foreign key (USERID) references USERS (ID)
        -> );
    Query OK, 0 rows affected (0.05 sec)mysql> drop table if exists USERS;
    Query OK, 0 rows affected (0.05 sec)mysql> create table USERS(
        -> ID int(8) not null primary key,
        -> USERNAME varchar(40),
        -> PASSWORD varchar(40)
        -> );
    Query OK, 0 rows affected (0.08 sec)mysql> drop table if exists EMAIL;
    Query OK, 0 rows affected (0.02 sec)mysql> create table EMAIL(
        -> USERID int(8),
        -> EMAIL varchar(200),
        -> foreign key (USERID) references USERS (ID)
        -> );
    Query OK, 0 rows affected (0.09 sec)mysql> set @@foreign_key_checks=on;
    Query OK, 0 rows affected (0.00 sec)mysql>
      

  2.   

    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html