查看实例中的多个数据库
实例——数据库——表——记录(实体)——字段(属性)
系统装好之后会有默认的数据库
进数据库:use 数据库名
查看表的结构
field 字段 type 数据类型 NUll 是否为空 KEY 主键 primary(PRI) default 默认 extra 扩展(会显示标识符列,第一个是种子、第二个是增量,标识符列不可以人为修改)
种子为1,增量为1,则第一个就是1,依次是23456
种子为1,增量为2,则第一个就是1,依次是3579
char 字符类型,16个长度
primary key 针对name设置主键
打开一台客户机
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
客户终端工具
右键mysql,点击打开连接
若是出现1045错误,是反馈你没有权限在此客户端登陆的权限,提权即可
grant all privileges on *.* to 'root'@'我电脑的ip地址' identified by '密码';
右键数据库,可以管理数据库,打开关闭
这个是纯字符,此时cmd中是不识别mysql命令的
基本上一直点下一步即可,带选择的我重点截出来
下一步就行
ODBC是无法安装的,不过没关系
安装完之后记得设置环境变量
这个是本地的数据库密码
,设置一个,然后下一步
这时就可以使用三叶草链接本地的
复制路径C:\Program Files\MySQL\MySQL Server 5.7\bin,添加环境变量
英文分号;将其隔开
此时在cmd中再次输入命令,可以使用字符界面使用了
链接本地的,不需要指定-h
定义表结构
field 字段名称
char 字符 int 整形字符 varchar 可变长字符
右击打开
values 值
若是写空的值的话,‘’,即只需要写两个单引号即可
where 后面是条件,指定对象,若是不跟条件,则将所有删除
*表示所有字段
来源地址写%代表所有IP地址
权限列表 写all 代表放开所有权限
表名写* 代表数据库下所有表
如果用户名已存在,则更改用户密码,如果用户不存在,则直接创建用户
grant 提权之意
revoke 收回权限之意
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 Source distribution
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>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> create database school;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql>
右击数据库,查看表
mysql> use school;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table info (
-> id int(4) not null,
-> name char(10) not null,
-> address varchar(50) default '南京',
-> score decimal,
-> primary key (id));
Query OK, 0 rows affected (0.01 sec)
mysql> describe info;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id | int(4) | NO | PRI | NULL | |
| name | char(10) | NO | | NULL | |
| address | varchar(50) | YES | | 南京 | |
| score | decimal(10,0) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.13 sec)
mysql>
此时在三叶草内刷新,再次查看,多了一份表格
修改name的类型,从char改为varchar,保存
mysql> describe info;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id | int(4) | NO | PRI | NULL | |
| name | varchar(10) | NO | | NULL | |
| address | varchar(50) | YES | | 南京 | |
| score | decimal(10,0) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
字符界面处也被修改
向表内添加数据
mysql> insert into info (id,name,address,score) values (1,'stu01','北京',88);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',88)' at line 1
mysql> insert into info (id,name,address,score) values (1,'stu01','北京',88);
Query OK, 1 row affected (0.01 sec)
mysql> insert into info (id,name,address,score) values (1,'stu02','上海',98);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into info (id,name,address,score) values (2,'stu02','上海',98);
Query OK, 1 row affected (0.01 sec)
mysql> insert into info (id,name,address,score) values (3,'stu03',default,90);
Query OK, 1 row affected (0.01 sec)
mysql> insert into info (id,name,address,score) values (4,'stu04','',60);
Query OK, 1 row affected (0.01 sec)
mysql> select * from info;
+----+-------+---------+-------+
| id | name | address | score |
+----+-------+---------+-------+
| 1 | stu01 | 北京 | 88 |
| 2 | stu02 | 上海 | 98 |
| 3 | stu03 | 南京 | 90 |
| 4 | stu04 | | 60 |
+----+-------+---------+-------+
4 rows in set (0.00 sec)
mysql>
更新数据
mysql> update info set address='杭州' where id=4 and name='stu04'
-> ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from info;
+----+-------+---------+-------+
| id | name | address | score |
+----+-------+---------+-------+
| 1 | stu01 | 北京 | 88 |
| 2 | stu02 | 上海 | 98 |
| 3 | stu03 | 南京 | 90 |
| 4 | stu04 | 杭州 | 60 |
+----+-------+---------+-------+
4 rows in set (0.00 sec)
mysql>
删除表内的一条记录
mysql> delete from info where name='stu04';
Query OK, 1 row affected (0.01 sec)
mysql> select * from info;
+----+-------+---------+-------+
| id | name | address | score |
+----+-------+---------+-------+
| 1 | stu01 | 北京 | 88 |
| 2 | stu02 | 上海 | 98 |
| 3 | stu03 | 南京 | 90 |
+----+-------+---------+-------+
3 rows in set (0.00 sec)
mysql>
删除表
mysql> drop table school.info
-> ;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from info;
ERROR 1146 (42S02): Table 'school.info' doesn't exist
mysql> show tables
-> ;
Empty set (0.01 sec)
mysql>
删除库
mysql> drop database school;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
查看权限
mysql> show grants for 'root'@'%';
ERROR 1141 (42000): There is no such grant defined for user 'root' on host '%'
mysql> show grants for 'bbsuser'@'%';
+--------------------------------------------------+
| Grants for bbsuser@% |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'bbsuser'@'%' |
| GRANT ALL PRIVILEGES ON "bbs".* TO 'bbsuser'@'%' |
+--------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mysql> show grants for root@localhost;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
剥夺权限
mysql> revoke all on *.* from 'root'@'%';
GRANT OPTION |
| GRANT PROXY ON ‘’@’’ TO ‘root’@‘localhost’ WITH GRANT OPTION |
±--------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
剥夺权限
mysql> revoke all on . from ‘root’@’%’;
# 七: 案例:建立公司IT运营部工资表
## 7.1 需求描述
- 为公司建立员工工资数据库 imployee_salary
- 在imployee_salary数据库中,建立IT_salary数据表,以保存IT运营部员工的工资信息,如下表所示
[外链图片转存中...(img-2QZWWAgI-1577977236705)]
## 7.2 建立数据库imployee_salary
[外链图片转存中...(img-ou6OVmTE-1577977236705)]
## 7.3 建立数据表IT_salary
[外链图片转存中...(img-pw7PRfj5-1577977236706)]