2015-10-23 105 views
0

我正在圖書館項目。我想創建一個數據庫和表,但我不知道爲什麼我的代碼不起作用。我檢查所有創建表語法沒有錯誤。 你能教我如何解決它們嗎?無法創建表?

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 
+1

'LibraryBranch'有兩個'auto_increment'領域。你不能那樣做(就像錯誤說的那樣)。 –

+0

檢查我的更新。 – Alex

回答

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) 
) ; 
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。

+0

我刪除了uID,但它仍然有錯誤 –

+0

什麼是新的錯誤信息? –

+0

這是相同的錯誤 –

0

mysql innodb表中不能有兩個auto_increment字段。 如果您希望自動增量,則可以對第二個字段使用觸發器。

+0

我刪除了所有其他的auto_increment,並且我在人員表上留下了一個。錯誤代碼仍然存在:1215.無法添加外鍵約束 –

+0

重新發送您正在使用的模式。從消息看來你錯誤地用外鍵約束創建表 – Sharath