嵌套查询
为什么要使用它?
商品信息 | 厂家 | 总量 | 销售表 |
---|---|---|---|
衣服 | 浙江 | 100 | xxx |
连衣裙 | 上海 | 200 | xxx |
比如当我们需要存储一个商品
不可能将一张表上存储所有的信息,这样管理起来很困难。
我们肯定会将销售表和商品表分离开,这个时候就需要对两个表合并起来查询。
两个都都必须有个唯一标示这样才能让两个表有关联性。
定义:将内层的查询结果作为外层的查询条件
语法格式:
select 字段名称 from 库.表 where 字段 算法符号(sql查询命令)
例子:
select username,uid from db3.usertab where uid>(select avg(uid) from db3.usertab);
多表查询
命令格式:
select 字段名称 from 库.表,库.表 where 字段 算法符号(sql查询命令)
查询表的设置:
create table t3 select username,uid,shell,homedir from db3.usertab limit 3;
create table t4 select username,uid,git from db3.usertab limit 5;
查询:
select * from t3,t4 where t3.uid= t4.uid; #两个表中UID是唯一的作为标识符号,显示所有
select t3.username,t4.username from t3,t4 where t3.uid=t4.uid and t3.username is not null ;
连接查询
左连接查询:(当条件成立时候,以左边表为主显示查询结果)
select 字段列表 from 表A left join 表B on 匹配条件;
右连接查询:(当条件成立时候,以右边表为主显示查询结果)
select 字段列表 from 表A right join 表B on 匹配条件;
例子:
create table t5 select username,uid,git,shell from db3.usertab where uid>100 and uid<=500;
create table t6 select username,uid,git,shell from db3.usertab where uid>100 and uid<=500 limit 3;
查询:
mysql> select t5.username,t6.username from t6 right join t5 on t6.uid=t5.uid;
+-----------------+-----------------+
| username | username |
+-----------------+-----------------+
| systemd-network | systemd-network |
| abrt | abrt |
| rtkit | rtkit |
| usbmuxd | NULL |
| qemu | NULL |
| NULL | NULL |
+-----------------+-----------------+
mysql> select t5.username,t6.username from t6 left join t5 on t6.uid=t5.uid; #查询表中的相同项
+-----------------+-----------------+
| username | username |
+-----------------+-----------------+
| systemd-network | systemd-network |
| abrt | abrt |
| rtkit | rtkit |
+-----------------+-----------------+
安装数据库图形管理工具phpmyadmin
管理数据库的方式有那些?
- 命令行
- 图形界面
- web
安装配置环境:
LAMP或LNMP
yum -y install httpd php php-mysql #启动服务器
用户的授权和撤销
什么是用户授权?
- 在数据库服务器上添加连接用户,添加可以设置用户的访问权限和连接的密码
- 默认只允许数据库管理员root用户在本机器登入
- 数据库的管理员授权只有在本机才拥有
授权的语法格式:
mysql> grant 权限列表 on 数据库 to 用户名@客户端地址 identified by '密码' [with grant option];
#with grant option 支持授权选项
撤销用户授权命令的语法格式
mysql> revoke 权限列表 on 数据库名 from 用户名@"客户端地址";
数据库服务器使用授权
mysql ->库为授权库,记录授权的信息
user #存储授权用户的名字和访问的权限
db #存储授权用户对库的访问权限
tables_priv #存储授权用户对表的访问权限
clomoun_priv #存储授权用户对字段的访问权限
恢复数据库的密码
system stop mysqld
vim /etc/my.cnf
skip-grant-tables- where 嵌套查询
#注释掉多与的控制信息
#set global validate_password_policy=0; 政策定义
#set global validate_password_length=1; 定义默认长度(系统默认是八位)
system start mysqld
mysql > desc mysql.user
authentication_string 字段为当前授权的密码
mysql> select user,host,authentication_string from mysql.user;
mysql> update mysql.user set authentication_string=password("123456") where host="localhost" and user="root";
#通过内置的加密函数将新的密码加密后修改
mysql> flush privileges;
修改当前等入的数据库密码
mysqladmin -u 用户名 -p password "新的密码";
Enter password 当前登入密码
mysql> show user(); #显示连接的用户名和客户端地址
mysql> select @@hostname #当前所在的位置
mysql> show grant #登入用户查看自己的权限
information_schema 虚拟库不占用物理存储信息
查看已有用户的访问权限
mysql> show grants for 被授权的用户@"%";
撤销用户权限命令的语法格式
reoke 权限列表 on 数据库 from 被授权的用户@IP ;
通过修改表记录的方式撤销用户的表权限
update mysql.user set Select_pri="N" where user="root" and host="IP"
You are my inhalation , I possess few blogs and very sporadically run out from to post .
As I site possessor I believe the content material here is rattling fantastic , appreciate it for your hard work. You should keep it up forever! Good Luck.