mysql存储过程不接受外键约束

时间:2012-06-25 08:00:12

标签: mysql stored-procedures

我在mysql中使用StoredProcedure创建了一个表,当我尝试应用外键约束时,我也应用了主键约束。日出错误'无法创建表dbname.tablename'..这里是我的Simple StoredProcedure < / p>

delimiter |
CREATE PROCEDURE SP_CREATE_TABLE_SAMP()
BEGIN
CREATE TABLE  anan_user  (
   user_id  bigint(15) NOT NULL AUTO_INCREMENT,
   name  varchar(70) DEFAULT NULL,
   last_name  varchar(70) DEFAULT NULL,
   gender  varchar(10) DEFAULT NULL,
   username  varchar(40) DEFAULT NULL,
   password  varchar(40) DEFAULT NULL,
   dateofbirth  date DEFAULT NULL,
   email  varchar(100) DEFAULT NULL,
   phone  varchar(25) DEFAULT NULL,
   mobile  varchar(25) DEFAULT NULL,
   address  varchar(250) DEFAULT NULL,
   pincode  varchar(15) DEFAULT NULL,
   state_id  bigint(15) DEFAULT NULL,
   district_id  bigint(15) DEFAULT NULL,
   constituency_id  bigint(15) DEFAULT NULL,
   profile_Img  varchar(100) DEFAULT NULL,
   is_pwd_changed  varchar(10) DEFAULT 'false',
   registered_time  timestamp NULL DEFAULT NULL,
   updated_date  timestamp NULL DEFAULT NULL,
  PRIMARY KEY ( user_id )

  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
END;

如何在存储过程中给出外键约束?

1 个答案:

答案 0 :(得分:2)

检查the MySQL reference on foreign key constraints的语法。发布您尝试的内容以及如何设置外键约束也很有帮助。

示例约束定义是:

CREATE TABLE parent (id INT NOT NULL,
                 PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
                INDEX par_ind (parent_id),
                FOREIGN KEY (parent_id) REFERENCES parent(id)
                  ON DELETE CASCADE
) ENGINE=INNODB;

相关语法为FOREIGN KEY (parent_id) REFERENCES parent(id)