2015-10-26 226 views
-1
CREATE TABLE IF NOT EXISTS `customer` (
`UserNo` int(3) NOT NULL AUTO_INCREMENT, 
`UserID` int(4) NOT NULL, 
`Username` text NOT NULL, 
`password` int(6) NOT NULL, 
`AccountNo` int(10) NOT NULL, 
`FirstName` text NOT NULL, 
`LastName` text NOT NULL, 
`DateOfBirth` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
`Gender` text, 
`Tel` int(10), 
`Account_type_No` int(2) NOT NULL, 
`Address` text NOT NULL, 
PRIMARY KEY (`UserNo`,`AccountNo`) 
); 

CREATE TABLE IF NOT EXISTS `account_type` (
`Account_type_No` int(2) NOT NULL AUTO_INCREMENT, 
`Account_type_Name` text NOT NULL, 
PRIMARY KEY (`Account_type_No`) 
); 

CREATE TABLE IF NOT EXISTS `seller` (
`Seller_No` int(4) NOT NULL, 
`Seller_Name` text NOT NULL, 
PRIMARY KEY (`Seller_Name`(254)) 
); 

CREATE TABLE IF NOT EXISTS `product_and_service` (
`Product_and_service_No` int(6) NOT NULL AUTO_INCREMENT, 
`Product_and_service_Name` text NOT NULL, 
`Seller_Name` text NOT NULL, 
FOREIGN KEY (Seller_Name) REFERENCES seller(Seller_Name), 
PRIMARY KEY (`Product_and_service_No`) 
); 


CREATE TABLE IF NOT EXISTS `customer_purchase` (
`Purchase_No` int(6) NOT NULL AUTO_INCREMENT, 
`User_No` int(10) NOT NULL, 
`Product_and_service_No` int(6) NOT NULL, 
FOREIGN KEY (User_No) REFERENCES customer(User_No), 
PRIMARY KEY (`Purchase_No`) 
); 

CREATE TABLE IF NOT EXISTS `balance` (
`AccountNo` int(10) NOT NULL, 
`Current_balance` int(10) NOT NULL, 
`Date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
FOREIGN KEY (AccountNo) REFERENCES customer(AccountNo), 
FOREIGN KEY (Product_and_service_No) REFERENCES product_and_service(Product_and_service_No) 
); 

CREATE TABLE IF NOT EXISTS `transaction` (
`Transaction_No` int(8) NOT NULL AUTO_INCREMENT, 
`AccountNo` int(10) NOT NULL, 
`Purchase_No` int(6) NOT NULL, 
`Total` int(10) NOT NULL, 
FOREIGN KEY (Purchase_No) REFERENCES customer(Purchase_No), 
FOREIGN KEY (AccountNo) REFERENCES customer(AccountNo), 
PRIMARY KEY (`Transaction_No`) 
); 

所以,我的问题是需要帮助有关SQL

MariaDB [onlinebankingsystem]> CREATE TABLE IF NOT EXISTS `product_and_service` (
    -> `Product_and_service_No` int(6) NOT NULL AUTO_INCREMENT, 
    -> `Product_and_service_Name` text NOT NULL, 
    -> `Seller_Name` text NOT NULL, 
    -> FOREIGN KEY (Seller_Name) REFERENCES seller(Seller_Name), 
    -> PRIMARY KEY (`Product_and_service_No`) 
    ->); 
ERROR 1170 (42000): BLOB/TEXT column 'Seller_Name' used in key specification without a key length 
MariaDB [onlinebankingsystem]> 
MariaDB [onlinebankingsystem]> 
MariaDB [onlinebankingsystem]> CREATE TABLE IF NOT EXISTS `customer_purchase` (
    -> `Purchase_No` int(6) NOT NULL AUTO_INCREMENT, 
    -> `User_No` int(10) NOT NULL, 
    -> `Product_and_service_No` int(6) NOT NULL, 
    -> FOREIGN KEY (User_No) REFERENCES customer(User_No), 
    -> PRIMARY KEY (`Purchase_No`) 
    ->); 
ERROR 1005 (HY000): Can't create table `onlinebankingsystem`.`customer_purchase` (errno: 150 "Foreign key constraint is incorrectly formed") 
MariaDB [onlinebankingsystem]> 
MariaDB [onlinebankingsystem]> CREATE TABLE IF NOT EXISTS `balance` (
    -> `AccountNo` int(10) NOT NULL, 
    -> `Current_balance` int(10) NOT NULL, 
    -> `Date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    -> FOREIGN KEY (AccountNo) REFERENCES customer(AccountNo), 
    -> FOREIGN KEY (Product_and_service_No) REFERENCES product_and_service(Product_and_service_No) 
    ->); 
ERROR 1072 (42000): Key column 'Product_and_service_No' doesn't exist in table 
MariaDB [onlinebankingsystem]> 
MariaDB [onlinebankingsystem]> CREATE TABLE IF NOT EXISTS `transaction` (
    -> `Transaction_No` int(8) NOT NULL AUTO_INCREMENT, 
    -> `AccountNo` int(10) NOT NULL, 
    -> `Purchase_No` int(6) NOT NULL, 
    -> `Total` int(10) NOT NULL, 
    -> FOREIGN KEY (Purchase_No) REFERENCES customer(Purchase_No), 
    -> FOREIGN KEY (AccountNo) REFERENCES customer(AccountNo), 
    -> PRIMARY KEY (`Transaction_No`) 
    ->); 
ERROR 1005 (HY000): Can't create table `onlinebankingsystem`.`transaction` (errno: 150 "Foreign key constraint is incorrectly formed") 

我怎样才能解决这个问题?

+2

编辑这个,我们可以阅读它。 –

+1

请参阅:http://stackoverflow.com/questions/1827063/mysql-error-key-specification-without-a-key-length – PaulF

+0

为什么seller_name为pk? Seller_No似乎是一个更合理的选择。 – jarlh

回答

1

您不能索引TEXT字段。 Seller_Name需要大于VARCHAR(255)?如果不是,则改为使用该数据类型。更改任何其他TEXT列,不需要非常大。

这可能会解决第一个错误。其余的错误都是从这个层面上级联起来的。

同时,请勿使用INT(6); (6)并不意味着什么。如果您建议使用最大尺寸,请使用MEDIUMINT UNSIGNED