2

这是我第一次做 MySQL。我正在使用 MySQL 5.6 命令行客户端并尝试显示表格,但它显示为空集。这是我要执行的脚本语句:

CREATE DATABASE ACMEOnline;

CREATE TABLE ITEM(Item_Number Integer DEFAULT 0, Item_Name Varchar(35) DEFAULT 'No Data', Model_Num Varchar(15) DEFAULT 'No Data',
 ->Description Varchar(255) DEFAULT 'No Data', Price DOUBLE(8,2) NOT NULL DEFAULT 0.00, Category_Name Varchar(35), 
 ->CONSTRAINT item_item_number_pk PRIMARY KEY(Item_Number), 
 ->CONSTRAINT item_category_name_fk FOREIGN KEY(Category_Name) REFERENCES CATEGORY(Category_Name));

CREATE TABLE CATEGORY(Category_Name Varchar(35), ShippingPerPound DOUBLE(4,2), DiscountsAllowed Char(1), 
 ->CONSTRAINT category_category_name_pk PRIMARY KEY(Category_Name));

CREATE TABLE LINE_ITEM(Quantity TINYINT(2), Shipping_Amount DOUBLE(6,2), OrderID Integer, Item_Number Integer,
 ->CONSTRAINT line_item_orderid_fk FOREIGN KEY(OrderID) REFERENCES ORDER_ITEM(OrderID),
 ->CONSTRAINT line_item_item_number_fk FOREIGN KEY(Item_Number) REFERENCES ORDER_ITEM(Item_Number));

CREATE TABLE ORDER_ITEM(OrderID Integer, Item_Number Integer, PRIMARY KEY(OrderID, Item_Number));

CREATE TABLE ORDER(OrderID Integer, Total DOUBLE(10,2), CONSTRAINT order_orderid_pk PRIMARY KEY(OrderID), CustomerID Integer, OfferCode Varchar(15),
  ->CONSTRAINT customer_offer_customerid_fk FOREIGN KEY(CustomerID) REFERENCES CUSTOMER(CustomerID),
  ->CONSTRAINT customer_offer_offercode_fk FOREIGN KEY(OfferCode) REFERENCES OFFER(OfferCode));

 CREATE TABLE CUSTOMER_OFFER(CustomerID Integer, OfferCode Varchar(15), PRIMARY KEY(CustomerID, OfferCode));

 CREATE TABLE OFFER(OfferCode Varchar(15), MinAmount DOUBLE(4,2) NOT NULL, Discount Varchar(35), ExpirationDate Char(8) NOT NULL, 
  ->CONSTRAINT offer_offercode_pk PRIMARY KEY(OfferCode));

 CREATE TABLE CUSTOMER(CustomerID Integer, CustomerName Varchar(50), Address Varchar(150), Email Varchar(80), 
  ->CONSTRAINT customer_customerid_pk PRIMARY KEY(CustomerID));

 CREATE TABLE BUSINESS(PurchaseTerms Varchar(50), CustomerID Integer, CONSTRAINT business_customerid_pk PRIMARY KEY(CustomerID),
  ->FOREIGN KEY(CustomerID) REFERENCES CUSTOMER(CustomerID));

 CREATE TABLE HOME(CreditCardNum BIGINT(16) NOT NULL, CardExpiration Char(7) NOT NULL, CONSTRAINT home_customerid_pk PRIMARY KEY(CustomerID),
  ->FOREIGN KEY(CustomerID) REFERENCES CUSTOMER(CustomerID));

 CREATE TABLE CONTACT(ContactName Varchar(50), ContactPhone Char(12) NOT NULL, CONSTRAINT contact_contactname_pk PRIMARY KEY(ContactName),
  ->FOREIGN KEY(Customer ID) REFERENCES BUSINESS(CustomerID));

 SHOW TABLES;
 DESCRIBE ITEM;
 DESCRIBE CATEGORY;
 DESCRIBE LINE_ITEM;
 DESCRIBLE ORDER_ITEM;
 DESCRIBE ORDER;
 DESCRIBE CUSTOMER_OFFER;
 DESCRIBE OFFER;
 DESCRIBE CUSTOMER;
 DESCRIBE BUSINESS;
 DESCRIBE HOME;
 DESCRIBE CONTACT;

在 MySQL 5.6 命令行客户端中:

 mysql> use acmeonline;
 Database changed
 mysql> show tables;
 Empty set (0.00 sec)
 mysql> describe item; //the rest of the table names repeated.
 ERROR 1146 (42S02): Table 'acmeonline.item' doesn't exist

我不知道最后一行是什么意思,即使我在名为acmeonline. 也许我错过了一些重要的东西,但我不知道是什么。您的帮助将不胜感激!

4

2 回答 2

3

不再使用 show tables 命令。了解如何使用information_schema视图。我怀疑您在错误的数据库中创建了表。

执行以下命令

SELECT * FROM information_schema.tables

在那里寻找你的桌子。information_schema 视图对于任何开发人员/dba 理解都是必不可少的。

于 2013-07-15T05:37:03.880 回答
1

您应该在创建数据库后更改它。现在,您的所有表都驻留在一个数据库中,该数据库在您连接到 mysql 或最后一个您在执行CREATE TABLE语句之前手动更改的数据库时使用过。

mysql> 创建数据库 ACMEOnline;
查询正常,1 行受影响(0.00 秒)

mysql> 使用 ACMEOnline;
数据库已更改

然后创建所有表

mysql> 创建表测试(id int);
查询正常,0 行受影响(0.02 秒)

mysql> 显示表;
+------------------------+
| Tables_in_acmeonline |
+------------------------+
| 测试 |
+------------------------+
一组中的 1 行(0.00 秒)

确保在正确的数据库中创建表的另一种方法是使用点表示法<database_name>.<table_name>

mysql> 使用 mysql;
数据库已更改

mysql> 创建表 ACMEOnline.test1(id int);
查询正常,0 行受影响(0.01 秒)

mysql> 使用 ACMEOnline;
数据库已更改

mysql> 显示表;
+------------------------+
| Tables_in_acmeonline |
+------------------------+
| 测试 |
| 测试1 |
+------------------------+
2 行(0.00 秒)
于 2013-07-15T05:35:35.367 回答