二.MySQL知识点全面总结2:mysql语句的执行(DDL DML 完整性约束)
1.对数据库的操作(DDL)
2.对数据表的操作(DDL)
3.对数据表字段的操作(DDL)
4.对数据表内容的增删改(DML)
5.对数据表内容的查找(DML)
6.数据类型约束设置
未更新
二.MySQL知识点全面总结2:mysql语句的执行(DDL DML 完整性约束)
1.对数据库的操作(DDL)
①增
- create database if not exists mytest character set utf8
②删
- drop database if exists mytest
③改
- alter database mytest character set gbk
④查
- show databases(查所有数据库)
- show create database mytesh(查一个数据库)
- show tables from mytest(查一个数据库中所有表)
2.对数据表的操作(DDL)
①增
- create table if not exists student(id Int,age Int,class Int)
②删
- drop table student(删除表)
- truncate table student(清空表)
③改
- rename table student to student
④查
- show create table student
3.对数据表字段的操作(DDL)
①增
- alter table student add age Double(10,2)
②删
- alter table student drop column age
③改
- alter table student modify age Double(10,2)
④查
- desc student
4.对数据表内容的增删改(DML)
①增
- insert into student(id,age,class) values(1,10,1)
②删
- delete from student where id=1
③改
- update student set id = 3 where age=15;
5.对数据表内容的查找(DML)
①执行顺序
- from-where/join on-group by-having-select-order by -limit
所以selet中的别名不能再group by中使用 - 举例说明
问题:
查出各个班大于0岁且班级人数大于5人的平均年龄,并按平均年龄升序排列?
语句:
SELECT st.class,AVG(st.age)
FROM student AS st
WHERE st.age>0
GROUP BY st.class HAVING AVG(st.age)>5
ORDER BY AVG(st.age)
LIMIT 0,2;
表格:
结果:
6.数据类型约束设置
①用户自定义完整性
- 非空 + 默认值
alter table student modify class int default 1
②实体完整性
- 主键约束+唯一约束
create table student(id Int primary key auto_increment,
age int not null,
class int default 1);
③参照完整性
- foreign key