DDL(Data Definition Language) | 数据定义语言,用来定义数据库对象:数据库,表,列等 |
DML(Data Manipulation Language) | 数据操作语言,用来对数据库中表的数据进行增删改 |
DQL(Data Query Language) | 数据查询语言,用来查询数据库中表的记录(数据) |
DCL(Data Control Language) | 数据控制语言,用来定义数据库的访问权限和安全级别,及创建用户 |
•DDL:操作数据库,表等
•DML:对表中的数据进行增删改
•DQL:对表中的数据进行查询
•DCL:对数据库进行权限控制
show databases; 查询数据库
creat database if not exists DB1; 创建数据库(如果DB1不存在的话)
drop database if exists DB1; 删除数据库(如果DB1存在的话)
select database(); 查询当前数据库
use DB1; 进入数据库DB1
show tables; 查询所有表
desc student; 查询表student,以变量名,数据类型展示
creat table student(name varchar(20), age int(4), gender varchar(1)); 创建表student(列名, 数据类型)
drop table if exists student; 删除表student(如果表存在的话)
alter table student rename to stu; 重命名student为stu
alter table student add address varchar(32); 添加表student中的一列address,数据类型为varchar(32)
alter table student modify age decimal 改变student中age列的数据类型为decimal
alter table student change sex gender varchar(2) ; 改变student中sex列名字为gender 数据类型为varchar(2)
alter table student drop address; 删除student中的address列
decimal(10,0) (将表student中的age列的数据类型改为decimal(10,2)表示整数和小数部分加起来为10,小数后最多有2位数字--如果超长数据库报错,短了用0补齐)
alter table student change sex gender varchar(2)
alter table student drop address;
insert into student(id,name,gender,tel...) values(1,'张三','男','17688888888'...); //选值插入
insert into student values(1,'张三','男','17688888888'); //全值插入
insert into student(1,'张三','男','17688888888'...),(1,'张三','男','17688888888'...),(1,'张三','男','17688888888'...),... //多行插入
update student set name = '李四' [where id=1]; //修改
delete from student where name ='张三' ; //删除
select id,name from student; //单表查询选定字段
select distinct id from student; //去重查询
select id as 学号 from student; //给列id起别名,列名显示为学号
select id from student where name like '_花%'; //模糊查询,前1后任意
select id from student order by age desc, id asc; //按照年龄降序,年龄相同时id升序
select count(*) from student; //查询总记录数量
select max(age) from student; //函数运算,查询年龄最大值
select min(math) from student; //函数运算,查询数学成绩最小值
select sex,count(),avg(math), from student where age between 20 and 50 group by sex having count()> 3; //查询年龄在20到50岁之间,根据性别分组的人数大于三的学生表 性别,数量,数学均值
select * from limit 3,3; //从第三条开始查三条,一般用于分页查询
show grants for 表名; //查看用户权限
grant 数据库权限 on 数据库对象 to 用户名@用户地址 identified by 用户口令; //给用户权限赋值
drop user 用户名@用户地址; //删除用户