douxin20081125 2013-10-23 07:08 采纳率: 0%
浏览 94
已采纳

用于创建多个表的MySQL语法

I am having a some problem that i don’t understand and trying to create tables from a php file , so i created a variable and i added all the create statements in it so i can run a mysql_query on it , i keep getting an error that there is a syntax problem , so i echo the content of the variable and past it in the sql section at phpmyadmin and it word fine but if i try it from php it gives me an error as :

Error creating table:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS Materials ( Id int(11) NOT NULL AUTO_INCREMENT,' at line 9

here is the string in the variable that i run the query on :

   // Create Tables Variable
$Tables = "";

// Create Students Table
$Tables = $Tables . "CREATE TABLE IF NOT EXISTS `Students` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `FirstName` varchar(50) DEFAULT NULL,
  `LastName` varchar(50) DEFAULT NULL,
  `Age` int(3) DEFAULT NULL,
  `Major` varchar(50) DEFAULT NULL,
  `Image` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`Id`)
);";

// Create Materials Table
$Tables = $Tables . "CREATE TABLE IF NOT EXISTS `Materials` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(50) DEFAULT NULL,
  `LastName` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`Id`)
);";

// Create Materials Students intersectons table for many to many relation
$Tables = $Tables . "CREATE TABLE `Material_Student`(
    `Student_Id` INT NOT NULL,  
    `Material_id` INT NOT NULL,    
    FOREIGN KEY (`Student_Id`) REFERENCES Students(`id`) ON UPDATE CASCADE,  
    FOREIGN KEY (`Material_id`) REFERENCES Materials(`id`) ON UPDATE CASCADE
);";

// Create Pages Table
$Tables = $Tables . "CREATE TABLE `Pages`(
    `Id` INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(`Id`),
    `Name` varchar(50),
    `Conent` varchar(5000)

);";

// Create News Table
$Tables = $Tables . "CREATE TABLE `News`(
    `Id` INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(`Id`),
    `Title` varchar(50),
    `Conent` varchar(5000),
    `Image` varchar(100)
);";

// Create Notifications Table
$Tables = $Tables . "CREATE TABLE `Notifications`(
    `Id` INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(`Id`),
    `Title` varchar(50),
    `Conent` varchar(5000),
    `Student_Id` INT NOT NULL,  
    FOREIGN KEY (`Student_Id`) REFERENCES Students(`id`) ON UPDATE CASCADE
);";

so what am I missing here ??

  • 写回答

2条回答 默认 最新

  • douya8978 2013-10-23 07:19
    关注

    In usual way you cannot execute many queries, I would recommend to execute all of them like different queries, however MySQLi has an option to execute many queries at once, documentation and examples you can find here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错