设计数据库books的数据库模式:
1.登录MySQL
mysql -u root -p
2.创建数据库books和用户bookorama
mysql> create database books;
Query OK, 1 row affected (0.03 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| books |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant select, insert, update, delete, index, alter, create, drop
-> on books.*
-> to bookorama identified by 'bookorama123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
3.载入创建表的SQL脚本文件
[root@mail /]# mysql -u bookorama -D books -p < /bookorama.sql
Enter password:
[root@mail /]# mysql -u bookorama -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use books;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------+
| Tables_in_books |
+-----------------+
| book_reviews |
| books |
| customers |
| order_items |
| orders |
+-----------------+
5 rows in set (0.00 sec)
mysql>
bookorama.sql 内容:
create table customers
( customerid int unsigned not null auto_increment primary key,
name char(50) not null,
address char(100) not null,
city char(30) not null
);
create table orders
( orderid int unsigned not null auto_increment primary key,
customerid int unsigned not null,
amount float(6,2),
date date not null
);
create table books
( isbn char(13) not null primary key,
author char(50),
title char(100),
price float(4,2)
);
create table order_items
( orderid int unsigned not null,
isbn char(13) not null,
quantity tinyint unsigned,
primary key (orderid, isbn)
);
create table book_reviews
(
isbn char(13) not null primary key,
review text
);
4.数据库books表中插入数据
[root@mail /]# mysql -u bookorama -D books -p </book_insert.sql
book_insert.sql 内容:
use books;
insert into customers values
(3, "Julie Smith", "25 Oak Street", "Airport West"),
(4, "Alan Wong", "1/47 Haines Avenue", "Box Hill"),
(5, "Michelle Arthur", "357 North Road", "Yarraville");
insert into orders values
(NULL, 3, 69.98, "2007-04-02"),
(NULL, 1, 49.99, "2007-04-15"),
(NULL, 2, 74.98, "2007-04-19"),
(NULL, 3, 24.99, "2007-05-01");
insert into books values
("0-672-31697-8", "Michael Morgan", "Java 2 for Professional Developers", 34.99),
("0-672-31745-1", "Thomas Down", "Installing Debian GNU/Linux", 24.99),
("0-672-31509-2", "Pruitt, et al.", "Teach Yourself GIMP in 24 Hours", 24.99),
("0-672-31769-9", "Thomas Schenk", "Caldera OpenLinux System Administration Unleashed", 49.99);
insert into order_items values
(1, "0-672-31697-8", 2),
(2, "0-672-31769-9", 1),
(3, "0-672-31769-9", 1),
(3, "0-672-31509-2", 1),
(4, "0-672-31745-1", 3);
insert into book_reviews values
("0-672-31697-8", "Morgan's book is clearly written and goes well beyond most of the basic Java books out there.");
5. 查看表中数据
mysql> select * from book_reviews
-> ;
+---------------+-----------------------------------------------------------------------------------------------+
| isbn | review |
+---------------+-----------------------------------------------------------------------------------------------+
| 0-672-31697-8 | Morgan's book is clearly written and goes well beyond most of the basic Java books out there. |
+---------------+-----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from books;
+---------------+----------------+---------------------------------------------------+-------+
| isbn | author | title | price |
+---------------+----------------+---------------------------------------------------+-------+
| 0-672-31509-2 | Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
| 0-672-31697-8 | Michael Morgan | Java 2 for Professional Developers | 34.99 |
| 0-672-31745-1 | Thomas Down | Installing Debian GNU/Linux | 24.99 |
| 0-672-31769-9 | Thomas Schenk | Caldera OpenLinux System Administration Unleashed | 49.99 |
+---------------+----------------+---------------------------------------------------+-------+
4 rows in set (0.00 sec)
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 order_items;
+---------+---------------+----------+
| orderid | isbn | quantity |
+---------+---------------+----------+
| 1 | 0-672-31697-8 | 2 |
| 2 | 0-672-31769-9 | 1 |
| 3 | 0-672-31509-2 | 1 |
| 3 | 0-672-31769-9 | 1 |
| 4 | 0-672-31745-1 | 3 |
+---------+---------------+----------+
5 rows in set (0.00 sec)
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)