日期:2014-05-16  浏览次数:20815 次

建表问题求教
SQL code
create database exam;
use exam;

create table Teacher
(T_id int primary key,
T_title varchar(255) not null,
T_pwd varchar(255) not null
);

create table Radio
(r_id int primary key,
T_id int foreign key (T_id) references Teacher(T_id) on update cascade on delete cascade,
r_desc varchar(255) not null,
option_A varchar(255) not null,
option_B varchar(255) not null,
option_C varchar(255) not null,
option_D varchar(255) not null,
r_true char not null
);


为什么这个在SQL08下就能顺利执行,在MYSQL下执行会报错呢?
求解。

------解决方案--------------------

SQL code

create table Radio
(r_id int primary key,
T_id int ,
r_desc varchar(255) not null,
option_A varchar(255) not null,
option_B varchar(255) not null,
option_C varchar(255) not null,
option_D varchar(255) not null,
r_true char not null,
foreign key (T_id) references Teacher(T_id) on update cascade on delete cascade
);

------解决方案--------------------
SQL code

CREATE TABLE `Radio` (
  `r_id` int(11) NOT NULL,
  `T_id` int(11) DEFAULT NULL,
  `r_desc` varchar(255) NOT NULL,
  `option_A` varchar(255) NOT NULL,
  `option_B` varchar(255) NOT NULL,
  `option_C` varchar(255) NOT NULL,
  `option_D` varchar(255) NOT NULL,
  `r_true` char(1) NOT NULL,
  PRIMARY KEY (`r_id`),
  KEY `T_id` (`T_id`),
  CONSTRAINT `Radio_ibfk_1` FOREIGN KEY (`T_id`) REFERENCES `Teacher` (`T_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;