简体   繁体   English

用于创建多个表的MySQL语法

[英]MySQL syntax for creating multiple tables

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 : 我遇到了一些我不理解的问题,试图从php文件创建表,所以我创建了一个变量,并在其中添加了所有create语句,以便可以在其上运行mysql_query,但我不断收到错误消息那是一个语法问题,所以我在phpmyadmin的sql部分中回显了变量的内容并将其过去,它的字词还不错,但是如果我从php尝试它,则会给我一个错误:

Error creating table: 创建表时出错:

You have an error in your SQL syntax; 您的SQL语法有误; 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 检查对应于你的MySQL服务器版本使用附近的正确语法手册“CREATE TABLE IF NOT EXISTS MaterialsId INT(11)NOT NULL AUTO_INCREMENT,”第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 ?? 所以我在这里想念什么??

通常,您不能执行许多查询,我建议像执行不同的查询一样执行所有查询,但是MySQLi可以选择一次执行许多查询,您可以在此处找到文档和示例。

Your issue seems to be passing a multiple-query into Php's single query function. 您的问题似乎是将多重查询传递给Php的单一查询功能。

See this link for explanation. 请参阅此链接以获取解释。 http://php.net/manual/en/mysqli.quickstart.multiple-statement.php http://php.net/manual/en/mysqli.quickstart.multiple-statement.php

I suggest you either break down your statements and execute one at a time, or use multi_query function instead. 我建议您要么分解您的语句并一次执行一个语句,要么改用multi_query函数。

http://www.php.net/manual/en/mysqli.multi-query.php http://www.php.net/manual/en/mysqli.multi-query.php

$mysqli = new mysqli("localhost", "yourusername", "yourpassword", "test");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->multi_query($Tables)) {
    echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

...

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM