上次那两语句我真是受教了。不过我发现自己一点变通能力都没有。这次是3张表关联,于是我模仿写成这样:(经检验这样写是错误的)
set @@foreign_key_checks=off;
drop table if exists PRODUCT
create table PRODUCT(
ID int(8) not null primary key auto_increment,
PRICE double,
DESCRIPTION varchar(1000)
);
drop table if exists ORDERS
create table ORDERS(
ID int(8) not null primary key auto_increment,
TOTAL double,
REALNAME varchar(20),
ADDRESS varchar(20),
POSTCODE varchar(20),
PHONE varchar(20)
); 
drop table if exists ORDERITEM
create table ORDERITEM(
ORDER_ID int(8) not null,
PRODUCT_ID int(8) not null,
foreign key (ORDER_ID) references ORDERS (ID),
foreign key (PRODUCT_ID) references PRODUCT (ID)
);
set @@foreign_key_checks=on;
帮忙纠正一下
还有如果两张表共享主键又应该如何添加什么语句(在保留drop table if exists 表名的前提下)
drop table if exists USERS
create table USERS(
ID int(8) not null primary key auto_increment,
USERNAME varchar(40),
PASSWORD varchar(40)
);drop table if exists PROFILE
create table PROFILE(
ID int(8) not null primary key auto_increment,
EMAIL varchar(100),
ADDRESS varchar(200),
POSTCODE varchar(8),
MOBILE varchar(11),
PHONE varchar(16)
);

解决方案 »

  1.   

    你的语句测试没有任何问题啊。 除了加了几个分号。
    mysql> set @@foreign_key_checks=off;
    Query OK, 0 rows affected (0.03 sec)mysql> drop table if exists PRODUCT;
    Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> create table PRODUCT(
        -> ID int(8) not null primary key auto_increment,
        -> PRICE double,
        -> DESCRIPTION varchar(1000)
        -> );
    Query OK, 0 rows affected (0.06 sec)mysql> drop table if exists ORDERS;
    Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> create table ORDERS(
        -> ID int(8) not null primary key auto_increment,
        -> TOTAL double,
        -> REALNAME varchar(20),
        -> ADDRESS varchar(20),
        -> POSTCODE varchar(20),
        -> PHONE varchar(20)
        -> );
    Query OK, 0 rows affected (0.08 sec)mysql> drop table if exists ORDERITEM;
    Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> create table ORDERITEM(
        -> ORDER_ID int(8) not null,
        -> PRODUCT_ID int(8) not null,
        -> foreign key (ORDER_ID) references ORDERS (ID),
        -> foreign key (PRODUCT_ID) references PRODUCT (ID)
        -> );
    Query OK, 0 rows affected (0.08 sec)mysql> set @@foreign_key_checks=on;
    Query OK, 0 rows affected (0.00 sec)mysql> set @@foreign_key_checks=off;
    Query OK, 0 rows affected (0.00 sec)mysql> drop table if exists PRODUCT;
    Query OK, 0 rows affected (0.06 sec)mysql> create table PRODUCT(
        -> ID int(8) not null primary key auto_increment,
        -> PRICE double,
        -> DESCRIPTION varchar(1000)
        -> );
    Query OK, 0 rows affected (0.06 sec)mysql> drop table if exists ORDERS;
    Query OK, 0 rows affected (0.03 sec)mysql> create table ORDERS(
        -> ID int(8) not null primary key auto_increment,
        -> TOTAL double,
        -> REALNAME varchar(20),
        -> ADDRESS varchar(20),
        -> POSTCODE varchar(20),
        -> PHONE varchar(20)
        -> );
    Query OK, 0 rows affected (0.06 sec)mysql> drop table if exists ORDERITEM;
    Query OK, 0 rows affected (0.03 sec)mysql> create table ORDERITEM(
        -> ORDER_ID int(8) not null,
        -> PRODUCT_ID int(8) not null,
        -> foreign key (ORDER_ID) references ORDERS (ID),
        -> foreign key (PRODUCT_ID) references PRODUCT (ID)
        -> );
    Query OK, 0 rows affected (0.08 sec)mysql> set @@foreign_key_checks=on;
    Query OK, 0 rows affected (0.00 sec)mysql>
      

  2.   

    下面的共享主键的两张表也可以这么操作吗??就是USERS,和PROFILE的那两张?我也是刚刚接触,太不了解MYSQL了。哎学了个Hibernate都要用Myeclipse的sql透视图运行。为了点的方便每次都加上drop table if exists 表名,可是运用起来有不少错误,自己还不知道晕哈。又不想让Hibernatte自动建表,不然自己写映射文件得累死。Annotation没掌握,只能靠建表,用逆向工程了哎能推荐本MYSQL的书吗??权威一点的哈。多谢了
      

  3.   

    数据库中没有“共享主键”这个概念。先读N遍《数据库系统概论(第四版)》 王珊 萨师煊   高等教育出版社 (掌握基础知识和概念) 然后再粗略浏览一遍MYSQL的官方手册。(方便以后查找,避免类似于考试的时候,给你本政治书也不知道答案在第几章,第几页)MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html