需要有关sql的帮助

时间:2015-10-26 12:04:37

标签: mysql sql foreign-keys foreign-key-relationship mariadb

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

所以,我的问题是

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")

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

这可能会修复第一个错误。其余的错误已经与之相关联。

同时,不要使用INT(6); (6)没有任何意义。如果您建议它有一些最大尺寸,请查看使用MEDIUMINT UNSIGNED