windows安装MySQL时需要注意:
1.检查本机主机名是否为英文,不能为中文(若不是修改后,重启计算机)
2.若已经安装了mysql,则:
(1).检查是否安装了历史mysql,要求在系统中卸载
(2).删除磁盘中所有mysql命名文件夹
(3).清除注册表
3.关闭所有安全类软件:360全家桶 杀毒(火绒) 管家
4.准备好运行库(可选)
5.找到运行服务界面
6.找到环境变量设置界面(添加MySQL的安装路径)
7.找到命令控制台或powershell (注意管理员权限运行)
安装完成之后若登录不进去需要考虑:密码是否有误、MySQL服务是否启动,终端是否为管理员权限运行的……
创建数据库
mysql> create database mydb6_product;
Query OK, 1 row affected (0.01 sec)
#查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb2_stuinfo |
| mydb3_employee |
| mydb4_product |
| mydb5_sales |
| mydb6_product |
| mydb_temp1 |
| mydbl_test |
| mysql |
| performance_schema |
| sys |
| temp1 |
+--------------------+
12 rows in set (0.00 sec)
根据题目要求是需要在库中创建三张表,需要查看所新建的库是否为当前使用,若不是则需要选择使用它作为当前库
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> use mydb6_product
Database changed
mysql> select database();
+---------------+
| database() |
+---------------+
| mydb6_product |
+---------------+
1 row in set (0.00 sec)
创建三张表命令如下:
mysql> create table employees(
-> id int primary key,
-> name varchar(50) not null,
-> age int not null,
-> gender varchar(10) default "unknown",
-> salary float(5,2) not null);
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> desc employees;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | int | NO | | NULL | |
| gender | varchar(10) | YES | | unknown | |
| salary | float(5,2) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> create table orders(
-> id int primary key,
-> name varchar(100) not null,
-> price float(5,2) not null,
-> quantity int not null,
-> category varchar(50) not null);
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> desc orders;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| price | float(5,2) | NO | | NULL | |
| quantity | int | NO | | NULL | |
| category | varchar(50) | NO | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> create table invoices(
-> number int primary key auto_increment,
-> order_id int not null,
-> in_date date not null,
-> total_amount float(5,2) check(total_amount>0),
-> foreign key(order_id) references orders(id));
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> desc invoices;
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| number | int | NO | PRI | NULL | auto_increment |
| order_id | int | NO | MUL | NULL | |
| in_date | date | NO | | NULL | |
| total_amount | float(5,2) | YES | | NULL | |
+--------------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
#查看当前数据库中的表,也可以作为检验三张表是否创建完成,是否在指定的数据库中
mysql> show tables;
+-------------------------+
| Tables_in_mydb6_product |
+-------------------------+
| employees |
| invoices |
| orders |
+-------------------------+
3 rows in set (0.00 sec)