因为我是root,阿里云又给我安装好了MySQL,所以我进入MySQL比较容易。没有那么多yak shaving,不管怎么样先混进去再说,后面会有创建用户的操作。如何登陆MySQL,我还是花了时间的。
显示数据库
mysql> show databases;
创建数据库应该这样:
mysql> create database firstSQL;
Query OK, 1 row affected (0.00 sec)
当我试图创建数据库的时候却:
Access denied for user ''@'localhost' to database '
发现是因为mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候是虽然用的是root,但实际是匿名登录的,通过错误提示里的''@'localhost'可以看出来,办法如下:
1.关闭mysql
# service mysqld stop
2.屏蔽权限
# mysqld_safe --skip-grant-table
屏幕出现: Starting demo from .....
3.新开起一个终端输入
# mysql -u root mysql
mysql> delete from user where USER='';
mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
mysql> \q
于是,查看数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| firstSQL |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
数据库创建成功了,然后我要创建示例用的表:
mysql> use firstSQL
Database changed
mysql> show tables;
Empty set (0.00 sec)
可见我的数据库里面并没有表,于是我试图按照书的附录文件创建一个表Customers:
mysql> INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
-> VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com');
ERROR 1146 (42S02): Table 'firstSQL.Customers' doesn't exist
往一个还不存在的表里面插入数值实不可取。应该这样创建表书的链接有下载文件,文件中有创建表的语句:
mysql> CREATE TABLE Customers (
-> cust_id INT NOT NULL AUTO_INCREMENT,
-> cust_name VARCHAR(20) NOT NULL,
-> cust_address VARCHAR(20) NOT NULL,
-> cust_city VARCHAR(20) NOT NULL,
-> cust_state VARCHAR(20) NOT NULL,
-> cust_zip VARCHAR(20) NOT NULL,
-> cust_country VARCHAR(20) NOT NULL,
-> cust_contact VARCHAR(40) NOT NULL,
-> cust_email VARCHAR(60) NOT NULL,
->PRIMARY KEY (cust_id) --指定主键
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)
然后,我逐条insert:
mysql> INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
-> VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com');
Query OK, 1 row affected (0.00 sec)
就这样,我创建了第一个表,Customers
mysql> desc Customers;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| cust_id | int(11) | NO | PRI | NULL | auto_increment |
| cust_name | varchar(20) | NO | | NULL | |
| cust_address | varchar(20) | NO | | NULL | |
| cust_city | varchar(20) | NO | | NULL | |
| cust_state | varchar(20) | NO | | NULL | |
| cust_zip | varchar(20) | NO | | NULL | |
| cust_country | varchar(20) | NO | | NULL | |
| cust_contact | varchar(40) | NO | | NULL | |
| cust_email | varchar(60) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
我的第一个表Customers 不是按照说明建的怕影响以后示例时报错,所以我要把它删掉,重新建;
mysql> DROP TABLE Customers ;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE Customers
-> (
-> cust_id char(10) NOT NULL ,
-> cust_name char(50) NOT NULL ,
-> cust_address char(50) NULL ,
-> cust_city char(50) NULL ,
-> cust_state char(5) NULL ,
-> cust_zip char(10) NULL ,
-> cust_country char(50) NULL ,
-> cust_contact char(50) NULL ,
-> cust_email char(255) NULL
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
-> VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com');
Query OK, 1 row affected (0.00 sec)
我想看看我建了几个表:
mysql> show tables;
+--------------------+
| Tables_in_firstSQL |
+--------------------+
| Customers |
| OrderItems |
| Orders |
| Products |
| Vendors |
+--------------------+
5 rows in set (0.00 sec)
到这这本书的基本数据就准备好了,然后就是各种select啦。