不能CREATE TABLE?

时间:2015-10-23 19:03:49

标签: mysql

我正在工作的图书馆项目。我想创建一个数据库和表,但我不知道为什么我的代码不起作用。我检查所有创建表语法没有错误。 你能教我如何修理它们吗?

DROP DATABASE IF EXISTS LIBRARYS;
CREATE DATABASE LIBRARYS;
USE LIBRARYS; 

DROP TABLE IF EXISTS BOOK;

CREATE TABLE BOOK
(
 title VARCHAR(50) NOT NULL,
 author VARCHAR(30),
 BookId INT auto_increment,
 ISBN INT,
 Edition VARCHAR(50),
 YearBought INT,
 Category VARCHAR (30),
 LibraryBranchID INT auto_increment,

 PRIMARY KEY(BookId),
 FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)
 );

DROP TABLE IF EXISTS Person;
CREATE TABLE Person
(PersonId INT AUTO_INCREMENT,
 uNAME VARCHAR(30) NOT NULL,
 age INT,
 UserType VARCHAR (30),
 PreferredBranch INT,
 updatedOn timestamp not null on update current_timestamp,
 PRIMARY KEY (PersonId),
 FOREIGN KEY ( PreferredBranch) references LibraryBranch(LibraryBranchID)
) ;
ALTER table Person AUTO_INCREMENT = 1001;


DROP TABLE IF EXISTS LOAN;
CREATE TABLE LOAN
(LoanId INT auto_increment,
 Pid INT,
 Bid INT,
 loanDate DATE DEFAULT '0000-00-00',
 overdue BOOLEAN DEFAULT FALSE,
 PRIMARY KEY(LoanId),
 FOREIGN KEY (Pid) references Person (PersonId),
 FOREIGN KEY (Bid) references Book(BookId) 
) ;

DROP TABLE IF EXISTS LibraryBranch ;
CREATE TABLE LibraryBranch
(uID INT AUTO_INCREMENT,
 LibraryBranchID INT auto_increment,
 BranchName INT,
 updatedOn timestamp not null on update current_timestamp,
 PRIMARY KEY (LibraryBranchID)
) ;

DROP TABLE IF EXISTS Rating;
CREATE TABLE Rating
(RatingId INT ,
 RatingDate INT,
 BookId INT,
 PersonId INT,
 Stars BOOLEAN DEFAULT FALSE,
 PRIMARY KEY(RatingId),
 FOREIGN KEY (PersonId) references Person (PersonId),
 FOREIGN KEY (BookId) references BOOK(BookId) 
) ;


LOAD DATA LOCAL INFILE '' INTO TABLE BOOK;
LOAD DATA LOCAL INFILE '' INTO TABLE Person;
LOAD DATA LOCAL INFILE '' INTO TABLE LOAN;

我在这里得到了那些结果

1 row(s) affected

0 row(s) affected

0 row(s) affected, 1 warning(s): 1051 Unknown table 'librarys.book'

Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key

3 个答案:

答案 0 :(得分:1)

CREATE TABLE LibraryBranch应该在您致电FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)

之前
CREATE TABLE BOOK
(
 ...
 BookId INT auto_increment,
 ... 
 LibraryBranchID INT auto_increment,
 ...
 PRIMARY KEY(BookId),
 FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)
 );

如果FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)LibraryBranchID无法 auto_increment

调用的

FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)错误字段 BranchName 应为 LibraryBranchID

 FOREIGN KEY ( LibraryBranchID) references LibraryBranch(LibraryBranchID)

,因为您刚刚创建了数据库,所以不需要DROP TABLE IF EXISTS每个表

更新 http://sqlfiddle.com/#!9/86bb96

CREATE TABLE LibraryBranch
(uID INT,
 LibraryBranchID INT auto_increment,
 BranchName INT,
 updatedOn timestamp not null on update current_timestamp,
 PRIMARY KEY (LibraryBranchID)
) ;

CREATE TABLE BOOK
(
 title VARCHAR(50) NOT NULL,
 author VARCHAR(30),
 BookId INT auto_increment,
 ISBN INT,
 Edition VARCHAR(50),
 YearBought INT,
 Category VARCHAR (30),
 LibraryBranchID INT ,
 PRIMARY KEY(BookId),
 FOREIGN KEY ( LibraryBranchID) references LibraryBranch(LibraryBranchID)
 );

CREATE TABLE Person
(PersonId INT AUTO_INCREMENT,
 uNAME VARCHAR(30) NOT NULL,
 age INT,
 UserType VARCHAR (30),
 PreferredBranch INT,
 updatedOn timestamp not null on update current_timestamp,
 PRIMARY KEY (PersonId),
 FOREIGN KEY ( PreferredBranch) references LibraryBranch(LibraryBranchID)
) ;
ALTER table Person AUTO_INCREMENT = 1001;

CREATE TABLE LOAN
(LoanId INT auto_increment,
 Pid INT,
 Bid INT,
 loanDate DATE DEFAULT '0000-00-00',
 overdue BOOLEAN DEFAULT FALSE,
 PRIMARY KEY(LoanId),
 FOREIGN KEY (Pid) references Person (PersonId),
 FOREIGN KEY (Bid) references Book(BookId) 
) ;

CREATE TABLE Rating
(RatingId INT ,
 RatingDate INT,
 BookId INT,
 PersonId INT,
 Stars BOOLEAN DEFAULT FALSE,
 PRIMARY KEY(RatingId),
 FOREIGN KEY (PersonId) references Person (PersonId),
 FOREIGN KEY (BookId) references BOOK(BookId) 
) ;

答案 1 :(得分:0)

在此代码中:

CREATE TABLE LibraryBranch
(uID INT AUTO_INCREMENT,
 LibraryBranchID INT auto_increment,
 BranchName INT,
 updatedOn timestamp not null on update current_timestamp,
 PRIMARY KEY (LibraryBranchID)
) ;

您将uID设为自动列,但LibraryBranchID是PK。

答案 2 :(得分:0)

你不能在mysql innodb表中有两个auto_increment字段。 如果希望自动递增,可以使用第二个字段的触发器。