1

我创建了以下脚本来设置我的 MySQL 数据库:

CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;

CREATE TABLE people (
  personID INT NOT NULL AUTO_INCREMENT,
  firstName VARCHAR(45) NOT NULL,
  lastName VARCHAR(45),
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (personID)
);

CREATE TABLE categories (
  categoryID INT NOT NULL AUTO_INCREMENT,
  categoryName VARCHAR(45) NOT NULL,
  description TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (categoryID)
);

CREATE TABLE homes (
  homeID INT NOT NULL AUTO_INCREMENT,
  homeName VARCHAR(45) NOT NULL,
  notes TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (homeID)
};

CREATE TABLE items (
  itemID INT NOT NULL AUTO_INCREMENT,
  itemName VARCHAR(100) NOT NULL,
  identifier VARCHAR(100),
  quantity INT NOT NULL,
  categoryID INT NOT NULL,
  homeID INT NOT NULL,
  itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
  description TEXT,
  image VARCHAR(45),
  notes TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (itemID),
  FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
  FOREIGN KEY (homeID) REFERENCES homes(homeID)
};

CREATE TABLE projects (
  projectID INT NOT NULL AUTO_INCREMENT,
  projectName VARCHAR(45) NOT NULL,
  description TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (projectID)
);

CREATE TABLE checkouts (
  checkoutID INT NOT NULL AUTO_INCREMENT,
  itemID INT NOT NULL,
  personID INT NOT NULL,
  projectID INT,
  quantity INT NOT NULL,
  outDateTime DATETIME,
  inDateTime DATETIME,
  outNotes TEXT,
  inNotes TEXT,
  checkedIn BOOL,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (checkoutID),
  FOREIGN KEY (itemID) REFERENCES items(itemID),
  FOREIGN KEY (personID) REFERENCES people(personID),
  FOREIGN KEY (projectID) REFERENCES projects(projectID)
);

但是,当我运行此脚本时,我收到以下错误消息:

ERROR 1064 (42000): 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 '}' at line 8

ERROR 1064 (42000): 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 '}' at line 17

ERROR 1215 (HY000): Cannot add foreign key constraint

我怀疑这与使用 bool 数据类型有关,因为这是第 8 行和第 17 行发生的事情;但是我无法在网上找到任何解决此问题的方法。有人能够看到可能导致问题的原因吗?我在本地 Windows 机器上运行 MySQL 5.6.11。

谢谢!

4

4 回答 4

7

字符必须相同。两者都使用括号

CREATE TABLE homes (

};

在这种情况下:它在错误消息中向您显示不正确的字符:

check the manual that corresponds to your MySQL server version for the right
syntax to use near '}' at line 8
于 2013-08-27T05:03:35.620 回答
0

摆脱花括号并替换为(

效果很好:http ://sqlfiddle.com/#!8/4ad37

于 2013-08-27T05:07:22.087 回答
0

试试这个,我测试了它,它成功地创建了数据库。这些问题是由于开始和结束括号不匹配而引起的。

CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;

CREATE TABLE people (
  personID INT NOT NULL AUTO_INCREMENT,
  firstName VARCHAR(45) NOT NULL,
  lastName VARCHAR(45),
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (personID)
);

CREATE TABLE categories (
  categoryID INT NOT NULL AUTO_INCREMENT,
  categoryName VARCHAR(45) NOT NULL,
  description TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (categoryID)
);

CREATE TABLE homes (
  homeID INT NOT NULL AUTO_INCREMENT,
  homeName VARCHAR(45) NOT NULL,
  notes TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (homeID)
);

CREATE TABLE items (
  itemID INT NOT NULL AUTO_INCREMENT,
  itemName VARCHAR(100) NOT NULL,
  identifier VARCHAR(100),
  quantity INT NOT NULL,
  categoryID INT NOT NULL,
  homeID INT NOT NULL,
  itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
  description TEXT,
  image VARCHAR(45),
  notes TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (itemID),
  FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
  FOREIGN KEY (homeID) REFERENCES homes(homeID)
);

CREATE TABLE projects (
  projectID INT NOT NULL AUTO_INCREMENT,
  projectName VARCHAR(45) NOT NULL,
  description TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (projectID)
);

CREATE TABLE checkouts (
  checkoutID INT NOT NULL AUTO_INCREMENT,
  itemID INT NOT NULL,
  personID INT NOT NULL,
  projectID INT,
  quantity INT NOT NULL,
  outDateTime DATETIME,
  inDateTime DATETIME,
  outNotes TEXT,
  inNotes TEXT,
  checkedIn BOOL,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (checkoutID),
  FOREIGN KEY (itemID) REFERENCES items(itemID),
  FOREIGN KEY (personID) REFERENCES people(personID),
  FOREIGN KEY (projectID) REFERENCES projects(projectID)
);
于 2013-08-27T05:07:30.463 回答
0

您有 2 个语法错误,如下所示...并且由于未创建下表,因此您面临外键错误.. 在 sqls 下运行,您的问题将得到解决:)

CREATE TABLE homes (
  homeID INT NOT NULL AUTO_INCREMENT,
  homeName VARCHAR(45) NOT NULL,
  notes TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (homeID)
); /* previously }; */

CREATE TABLE items (
  itemID INT NOT NULL AUTO_INCREMENT,
  itemName VARCHAR(100) NOT NULL,
  identifier VARCHAR(100),
  quantity INT NOT NULL,
  categoryID INT NOT NULL,
  homeID INT NOT NULL,
  itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
  description TEXT,
  image VARCHAR(45),
  notes TEXT,
  archived BOOL NOT NULL DEFAULT '0',

  PRIMARY KEY (itemID),
  FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
  FOREIGN KEY (homeID) REFERENCES homes(homeID)
); /* previously }; */
于 2013-08-27T05:07:37.333 回答