mysql - 编程错误 : 1064 (42000): You have an error in your SQL syntax; Unable to create table using foreign key

标签 mysql python-2.7 mysql-python

我是 mysql-python 的新手,引用 this . 以下是我的查询-

TABLES['doctor_details'] = (
                                "CREATE TABLE `doctor_details` ("
                                 " `dr_id` bigint NOT NULL AUTO_INCREMENT,"
                                 " `doctor_link` varchar(40),"
                                 " `doctor_name` varchar(40) NOT NULL,"
                                 " `doctor_exp_years` float,"
                                 " `doctor_qualification` varchar(40),"
                                 " `doctor_phone_no` varchar(15),"
                                 " `doctor_city` varchar(40),"
                                 " `doctor_state` varchar(40),"
                                 " `doctor_country` varchar(40),"
                                 " `doctor_speciality` varchar(40),"
                                 " `doctor_website_link` varchar(40),"
                                 " `status` int NOT NULL,"
                                 " PRIMARY KEY (`dr_id`)"
                                ") ENGINE=InnoDB")

        TABLES['hospital_details'] = (
                                "CREATE TABLE `hospital_details` ("
                                "  `hospital_id` bigint NOT NULL AUTO_INCREMENT,"
                                "  `dr_id` bigint NOT NULL,"
                                "  `hospital_link` varchar(40),"
                                "  `hospital_name` varchar(40),"
                                "  `hospital_address` varchar(40),"
                                "  `hospital_city` varchar(40),"
                                 "  `hospital_state` varchar(40),"
                                 "  `hospital_country` varchar(40),"
                                 "  `hospital_phone_no` varchar(15),"   
                                 "  `hospital_speciality` varchar(40),"
                                 "  `doctor_link` varchar(40),"
                                 "  `hospital_website_link` varchar(40),"
                                "   PRIMARY KEY (`hospital_id`)"
                                "   CONSTRAINT `dr_idfk` FOREIGN KEY (`dr_id`)"
                                "    REFERENCES `doctor_details` (`dr_id`) ON DELETE NO ACTION" 
                                ") ENGINE=InnoDB")
        self.cursor.execute( TABLES['doctor_details'])
        self.cursor.execute( TABLES['hospital_details'])

它给了我以下错误-

ProgrammingError: 1064 (42000): 你的 SQL 语法有错误;查看与您的 MySQL 服务器版本对应的手册,了解在 'CONSTRAINT dr_idfk FOREIGN KEY (dr_id) REFERENCES doctor_details (`第 1 行的 dr_i'

如果我犯了一些愚蠢的错误,我深表歉意,但欢迎以任何形式提供指导/帮助。

非常感谢。

最佳答案

你在这行后面少了一个逗号

PRIMARY KEY (`hospital_id`)"

应该是这样

PRIMARY KEY (`hospital_id`),"

话又说回来,我不学 Python,所以对我来说这一切看起来都很希腊。

关于mysql - 编程错误 : 1064 (42000): You have an error in your SQL syntax; Unable to create table using foreign key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148751/

相关文章:

python - Raspberry Pi 3B 和 RFID RC522 - Python TypeError

python - 使用特定版本的 Python 创建 Windows Python virtualenv

xcode - Mac OS X Lion : can not install mysql-python 上缺少 llvm-gcc

php - mysql db 对所有主机连接开放

php - 在 MySQL 中按发件人收件人或收件人发件人分组

python - 在 python 中使用 str.format 时的引号

python - 使用 mysqyl-python 连接器时出现 SSL 连接错误

MySQL按id在每个组中获取倒数第二行

mysql - 如何过滤特殊产品订单的行?

django - 为 Django 安装 MySQL-python