这是我第一次做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
的数据库中创建了一个项目表。也许我错过了一些重要的东西,但我不知道是什么。非常感谢您的帮助!
答案 0 :(得分:2)
不要再使用show tables命令了。了解如何使用information_schema
观看次数。我怀疑你在错误的数据库中创建了表。
执行以下命令
SELECT * FROM information_schema.tables
在那里寻找你的桌子。 information_schema视图对于任何开发人员/ dba都是必不可少的。
答案 1 :(得分:1)
您应该在创建数据库后更改数据库。现在,所有表都驻留在连接到mysql的最后一个数据库中,或者在执行CREATE TABLE
语句之前手动更改的最后一个数据库。
mysql> CREATE DATABASE ACMEOnline; Query OK, 1 row affected (0.00 sec) mysql> USE ACMEOnline; Database changed
然后创建所有表格
mysql> CREATE TABLE test(id int); Query OK, 0 rows affected (0.02 sec) mysql> SHOW TABLES; +----------------------+ | Tables_in_acmeonline | +----------------------+ | test | +----------------------+ 1 row in set (0.00 sec)
确保在正确的数据库中创建表的另一种方法是使用点表示法<database_name>.<table_name>
mysql> USE mysql; Database changed mysql> CREATE TABLE ACMEOnline.test1(id int); Query OK, 0 rows affected (0.01 sec) mysql> USE ACMEOnline; Database changed mysql> SHOW TABLES; +----------------------+ | Tables_in_acmeonline | +----------------------+ | test | | test1 | +----------------------+ 2 rows in set (0.00 sec)