【问题标题】:Need help about sql需要关于 sql 的帮助
【发布时间】:2015-11-03 11:50:37
【问题描述】:
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")

我该如何解决这个问题?

【问题讨论】:

  • 编辑这个我们可以阅读它。
  • 为什么seller_name为pk? Seller_No 似乎是一个更合理的选择。
  • @EdwinJarosiński 抱歉,我很难阅读,这是我第一次在这个网站上提问,感谢您编辑我的问题。
  • @PaulF 好的,我会看到这些问题。

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


【解决方案1】:

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

这可能会解决第一个错误。其余的错误都由此产生。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-11
    • 2019-06-26
    • 2014-09-25
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2017-08-05
    • 2023-04-06
    相关资源
    最近更新 更多