一.关于用户管理操作
1.创建用户
create user '用户名'@‘ip地址’ identified by '密码' ;
2.删除用户
drop user '用户名'@‘ip地址’;
3.修改用户
rename user '用户名'@‘ip地址’; to '新用户名'@‘新ip地址’;;
4.修改密码
set password for '用户名'@'ip地址' = password('新密码');
注意:其中ip地址中的%表示任意数字,如‘%’,‘192.168.%’。
二.用户权限管理操作
1.查看用户权限
show grant for '用户'@'ip地址'
2.授权
grant 权限 on 数据库.表 to ‘用户名’@‘ip地址’;
3.取消权限
revoke 权限 on 数据库.表 from '用户名'@'ip'地址
注意:1.数据库.表可以写成*.*表示授予所有数据库的权限相关权限
权限的种类:
all privileges 除grant外的所有权限
select 仅查权限
select,insert 查和插入权限
...更多权限可以查看手册
三.显示,创建,使用数据库
1.显示数据库
show databases;
2.使用数据库(打开文件夹)
use 数据库名称;
3.创建数据库
create databases db default charset UTF-8;
创建一个数据库,并将其编码格式设置为UTF-8
四.表的操作
1.创建表
create table 表名(
列名 类型 是否可以为空,
列名 类型 是否可以为空,
)engine=innodb default charset=UTF-8
举例:
create table t1(
列名 类型 null,
列明 类型 not null,
列名 类型 not null default 1,
列名 类型 not null(null) auto_increment primary key,
id int,
num decimal(10,5) //前面是小数点前后的总位数,后面代表小数点后面的数
name char[10]) engine=innodb default charset = UTF-8;
注意:null表示空,not null表示非空。 default 2表示设置默认值
(1)主键
定义:一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一
例如:
定义语句: name char(10),id int
设置主键: id int primary key 一个属性作为主键
primary key(nid,num)两个属性组合起来作为主键
(2)外键
定义:如果公共关键字在一个关系中是主关键字,那么这个公共关键字被称为另一个关系的外键。
定义语句:
constraint 名称 foreign key (color_id) references color(nid)
color_id为外键,nid为color表中的主键,color为被参照表。
(3)自增
定义:如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
id int not null auto_increment primary key,
注意:设置为自增的列必须为主键
2.删除表
drop table 表名;
3.清空表
delete table 表名 //不清空自增,添加元组时,自增列的值接清空前
truncate table 表名 //同时清空自增列
4.修改表
添加列:alter table 表名 add 列名 类型
删除列:alter table 表名 drop column 列名
修改列:alter table 表名 change 原列名 新列名 类型;
五.内容操作(增删改查)
1.增
insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表
2.删
delete from 表 where id=007 and name='嘿嘿嘿'
3.改
update 表 set name = 'lihua' where age<18
4.查
select nid,name,gender as gg from 表 where id > 1
select * form 表 //表示查询该表中的所有数据
六.数据类型
1.数字类型
整数类型:
1.tinyint 2.int 3.bigint 4.decimal
浮点数类型:
1.float 2.double
字符类型:
1.char 2.varchar 3.text
枚举类型:
1.enum
集合类型:
1.set
时间类型:
1.Date 年月日 2.time 时分秒 3.year 年
1.num decimal(10,5) //前面是小数点前后的总位数,后面代表小数点后面的数
2.char(10) 不管长度等不等于10 都会占10个长度
3.varchar(10) 根据实际情况分配,最大为10,节省空间,速度没有char快
4.size ENUM(‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’)插入数据时只能选择ENUM中的一项
5.col SET(‘a’, ‘b’, ‘c’, ‘d’); 查数据时可以是SET中的任意组合
补充操作:
条件操作:
select * from 表 where id >1 and name !='alex' and num = 12;
select * from 表 where between 5 and 16;
select * from 表 where id in (22,12,1);
select * from 表 where id not in (11,2,1);
select * from 表 where id in (select nid from 表)
通配符:
select * from 表 where name like '%' 其中%代表多个字符
select * from 表 where name llike '_' 其中_代表一个字符
限制(主要用于分页):
select * from 表 limit 5;
select * from 表 limit 10,10 表示从第10行开始的后面10行;
select * from 表 limit 10 offset 10 效果同上
排序
select * from 表 order by 列 asc 根据列从小到大排序
select * from 表 order by 列1 asc,列2 asc 根据列1从小到大排序,如果有相同,再根据列2从小到大排序
asc:从小到大,desc:从大到小
分组:
select * from 表 group by xx;
select * from 表 group by xx having xx>xx;
分组常与聚集函数组合使用,并且group by 必须放在where之后,group by 之前
连表:
左右连表:join(自动去重)
方法一:
select A.name,B.name from A,B where A.num = B.num;
方法二:
1.inner join
select A.name,B.num from A inner join B on A.num = B.num;
当A.num与B.num无对应关系则不显示
2.left join
select A.name,B.num from A left join B on A.num = B.num;
A表全部显示,A表存在,但B表不存在的显示空
3.right join
select A.name,B.num from A right join B on A.num = B.num;
B表全部显示,B表存在,但A表不存在的显示为空
上下连表:union(自动去重) union all(不去重)