1. root 登陆
mysql -u root -p
2.创建数据库
create database books DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
3.创建用户
create user bookorama@localhost identified by 'password';
4.给用户赋权限
grant select, insert, update, delete, index, alter, create, drop on books.* to bookorama identified by 'password';
flush privileges;
5.创建表
mysql -h localhost -u bookorama -D books -p < bookorama.sql
5.新用户登陆
mysql -u bookorama -p
6.查询表
use books;
show tables;
左关联:即使左边表的字段在右边表中没有匹配上,左边表的内容也会出现在查询结果中,而右边会以 NULL 值来填充;
右关联:即使右边表的字段在左边表中没有匹配上,右边表的内容也会出现在查询结果中,而左边会以 NULL 值来填充。
mysql> select * from customers;
+------------+-----------------+--------------------+--------------+
| customerid | name | address | city |
+------------+-----------------+--------------------+--------------+
| 3 | Julie Smith | 25 Oak Street | Airport West |
| 4 | Alan Wong | 1/47 Haines Avenue | Box Hill |
| 5 | Michelle Arthur | 357 North Road | Yarraville |
+------------+-----------------+--------------------+--------------+
3 rows in set (0.00 sec)
mysql> select * from orders;
+---------+------------+--------+------------+
| orderid | customerid | amount | date |
+---------+------------+--------+------------+
| 1 | 3 | 69.98 | 2007-04-02 |
| 2 | 1 | 49.99 | 2007-04-15 |
| 3 | 2 | 74.98 | 2007-04-19 |
| 4 | 3 | 24.99 | 2007-05-01 |
| 5 | 2 | 69.98 | 2008-06-18 |
+---------+------------+--------+------------+
5 rows in set (0.00 sec)
mysql> select customers.customerid, customers.name, orders.orderid from customers left join orders on customers.customerid = orders.customerid;
+------------+-----------------+---------+
| customerid | name | orderid |
+------------+-----------------+---------+
| 3 | Julie Smith | 1 |
| 3 | Julie Smith | 4 |
| 4 | Alan Wong | NULL |
| 5 | Michelle Arthur | NULL |
+------------+-----------------+---------+
4 rows in set (0.00 sec)
mysql>