#查看数据库 show databases; #创建数据库:CREATE DATABASE数据库名charSetutf8; create database wordpress; #删除数据库 drop database wordpress; #切换数据库 use mysql #查看表 show tables; #创建表,一会再说 #删除表 drop table 表名; #查看表结构 desc 表名 #查看所有数据 select * from user; #查询指定列的数据 select user,host,Password from user; #查询指定列的数据的头3h Select user,host,Password from user limit 3 ; #授权,默认情况下mysql和mariadb都是不允许root用户远程连接登录的。 #grant操作(增删改查,all) on 库名.表名 to 用户名@'%' identified by '密码' all代表所有权限(select,delete,create) grant all on *.* to root@'10.0.0.%' identified by '123456'; grant a11 on wordpress.* to wordpress@'10.0.0.%' identified by '123456′; grant a11 on wordpress.menu to jaden@'10.0.0.%' identified by '123456';#jaden用户只能对menu表进行操作 #网站代码中连接数据库的时候使用的是哪个用户,那个用户有什么权限,那么网站代码就能对数据库做什么操作。 #使用普通用户登录 mysql -u wordpress -p123456-h 10.0.0.7 #默认的数据文件存储位置是 /var/1ib/mysql/ #/root/.mysql_history 记录了我们做的历史sql指令 #修改用户密码 #安全初始化,可以修改root用户的密码:mysql_secure_installation #但是这个动作做完之后,还需要给root设置远程登录权限才能远程用root用户登录
方式2: 格式:mysql> set password for 用户名@localhost=password('新密码'); 例子:mysql> set password for root@localhost=password('123');
Mysql数据类型
1 2 3 4 5 6 7
int 整形 数字 适合存储:年龄,加减运算 float 浮点型 适合存储:余额,加减运算 char 字符串 适合存储:不做加减运算身份号码密码,单行信息 text 文本 适合存储:适合多行信息,小说,商品描述 enum 枚举 适合存储:固定选项,多选一 date 日期类型 适合存储:时间,一般存储的是unix时间戳,从1970.1.1 0:0:0到现在过了多少秒,这个时间戳是可以转化为具体的时间日期的。 boolen 布尔类型 true/false对应数字就是0/非0
#修改字段的长度 alter table s2 modify name char(10); #查看创表语句 show create table s2; #增加字段 alter table s2 add age int(3); #删除字段 alter table s2 drop age; #ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件] FIRST;#添加这个字段的时候,把它放到第一个字段位置 #ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件] AFTER 字段名;#after是放到后的这个字段的后面去了,我们通过一个first和一个after就可以将新添加的字段放到表的任意字段位置了。 #修改表的字符集 alter table 表名 charset=utf8mb4; #使用where条件删除单条数据 delete from t5 where name='zhangsan'; #删除所有数据 delete from t5;、 #单条件修改: update t5 set password='123' where name='wangwu'; #单条件修改多列: update t5 set password='123',name='xxx' where name='wangwu'; #多条件修改 update t5 set password='123' where name='wangwu' and id=1; update t5 set password='123' where name='wangwu' or id=1; #修改所有数据 update t5 set password='123456';
添加用户、修改用户权限
1 2 3 4 5 6
create user wang@'%' identified by '123' #添加新用户@后面%表示所有的域名都可以访问 show grants for wang@'%' #查看wang这一用户的MySQL权限 # USAGE表示无权限 ALL PRIVILEGES表示所有的权限
grant all on wordpress(库名).* to wang@'%' #给予权限 revoke select on wordpress.* from wang@'%' #回收权限
查询中国的城市数量? select count(name) as 中国城市总数 from city where countrycode='cHN'; #查询世界的国家数量? select count(name) from country; #查询中国的总人口? select sum(population) from city where countrycode='chn'; #把多行合并成一行 select group_concat(name) from city where countrycode='chn' and district='hebei'; #把多列合并成一列 select concat(Name,"#",CountryCode,"#",District) from city where countrycode='chn'and district='hebei' #增加索引 alter table 表名 add primary key(id);
#创建表的时候,指定索引 create table zhu2(id int(8) primary key AUTO_INCREMENT ,name char(10),passwd char(10)); #createtable 表名9(id int,name char(10),primarykey(id)); #增加主键索引(要求结果唯一,而且必须要有一个主键列) alter table t10Ow add PRIMARY KEY(id);#创建表之后再添加主键索引 #创建普通索引 alter table t1oow add index num_index(num);#num_index索引名字,num是列名 #创建联合索引 alter table t100w add index 1ianhe(k1,k2); #唯一索引 create table t2(id int,title char(10) unique); #查看索引 #desc t100w; #MUL普通索引、PRI主键索引 show index from t100w; #删除普通索引 alter table t100w drop index 1ianhe; #删除主键索引 alter table t100w drop PRIMARY key;
UNION联合查询
1 2 3 4 5 6 7
#可以将两张表中的内容放到一起 select * from t2 union select * from t1; select * from t2 union select user(),version(),database(); #注意:union要求前后查询数据的列数必须相同才可以,不然会报错 #sql注入漏洞监测中经常会使用 select * from t1 union select 1,2,3,user();