MySQL学习笔记总结(小白必看+基础篇)

DDL:数据库模式定义语言DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言。
DML:数据操纵语言DML(Data Manipulation Language),用户通过它可以实现对数据库的基本操作。
DCL:数据控制语言 (Data Control Language) 在SQL语言中,是一种可对数据访问权进行控制的指令,它可以控制特定用户账户对数据表、查看表、存储程序、用户自定义函数等数据库对象的控制权。由 GRANT 和 REVOKE 两个指令组成。
DQL:Data QueryLanguage 数据查询语言。

DQL:数据库查询语言。关键字:SELECT … FROM … WHERE。
DDL :数据库模式定义语言(数据库、数据表)。关键字:CREATE,DROP,ALTER。
DML:数据操纵语言(数据)。关键字:INSERT、UPDATE、DELETE。
DCL:数据控制语言(权限) 。关键字:GRANT、REVOKE。
TCL:事务控制语言。关键字:COMMIT、ROLLBACK、SAVEPOINT。
DDL,DML,DCL,DQL,TCL共同组成数据库的完整语言。


登录数据库服务器:
mysql -u账号 -p密码 //-u账号之间无空格,密码同样

数据库操作:

创建数据库:
create database 数据库名字;

创建数据库的时候,指定字符集:
create database 数据库名字 character set 字符集;
例: create database 数据库名字 character set utf8 ;

create database 数据库名字 character set 字符集 collate 校对规则;
例: create database 数据库名字 character set 字符集 collate utf8_bin ;

查看数据库:
show databases ;
(information_schem , performance_schema ,mysql 三个库不要动!)

查看数据库的定义:
show create database 库名 ;

修改数据库:
alter database 数据库名字 character set 字符集 ;

删除数据库:
drop database 数据库名字 ;

切换数据库:
use 数据库名字 ;

查看当前正在使用的数据库:
select database() ;


表的操作:

列的类型: int, char, varchar, float, double, boolean, date …
列的约束:
主键约束:primary key
唯一约束:unique
非空约束:not null

创建表:
create table 表名(列名1 列的类型 约束,列名2 列的类型 约束) ;
例:
creat table table1(
sid int primary key,
sname varchar(16),
sex int,
age int,
class int,
score float
) ; //注意标点

查看表:
show tables ;

查看表的定义:
show create table 表名 ;

查看表结构:
desc 表名 ;
例: desc student ;

修改表:
添加列(add):
alter table 表名 add 列名 列的类型 列的约束 ;
例: alter table student add score int not null ;

修改列(modify):
alter table 表名 modify 列名 列的类型 ;
例: alter table student modify sex varchar(2);

修改列名(change):
alter table 表名 change 旧列名 新列名 列的约束 ;
例:alter table student sex gender varcher(3) ;

删除列(drop):
alter table 表名 drop score ;

修改表名(rename):
rename tabel 旧表名 to 新表名 ;
例: rename table student to stu ;

修改表字符集:
alter table 表名 character set 字符集;
alter table stu character set gbk ;

删除表:
drop table 表名 ;
例: drop table stu ;


表中数据的操作:

表中插入数据:
insert into 表名(列名1,列名2,列名3) values(值1,值2,值3) ;
例:insert into student(sid,name,sex,age) values(1,’james’, 1,23) ;
简单写法:
insert into student values(2,’lucy’,2 ,25)
选择插入:
insert into student(sid,sname) values(3,’Lebron’,1,25) ;
批量插入:
insert into student values(4,’zhangsan’,1,25),(5,’lisi’,2,24),(6,’wangwu’,2,30) ;

删除数据:
delete from 表名 [where 条件]
例:delete from student where sid=1 ;
delete from student ; //全删

修改(更新 )表数据:
update 表名 set 列名=列的值,列名2=列的值2 [where 条件]
例:update student set sname=’张三’ where sid=2 ;
update student set sname=’李四’,sex=3; //全改


查询表中数据:
select [distinct] [*] [列名1,列名2] from 表名 [where 条件] //distinct:去除重复的数据
select * from 表名 ;
例:select * from student ; //查看表中所有内容
select sname,sex from student ; //选择查看

别名查询,as关键字,as关键字可以省略
表别名:select s.sname, s.sex from student as s; //主要用在多表查询
列别名:select 原列名1 as 新别名,原列名2 as 新别名 from product ;
例:select sname as 姓名,sex as 性别 from student ; //就是用”姓名”、”性别”,在表中替换掉sname、sex 。(as可省略)

去掉重复的值查询:
select age from student; //查询学生的所有年龄
select distinct age from student; //去重

运算查询:
select , age+1 as 年龄加一 from student ; //在查询结果做±/运算,作为新的一列展示

select * from student where age>18 ; //查询大于18岁的人
select * from student where age <> 22 ; //查询不等于22的人(!=也可)
select * from student where age > 18 and age <25 ; //查询年龄介于18-25的人
select * from student where age between 18 and 25 ; //同上
select * from student where age <18 or age >25; //查询年龄小于18或者大于25的人

like:模糊查询
_:代表的是一个字符
%:代表的是多个字符
select * from student where sname like ‘%张%’ ; //查询所有名字带有”张”的人
select * from product where sname like ‘_兆%’; //查询所有名字中第二个字是”兆”的人

in: 在某个范围中查询
select * from student where class in (1,2,4); //查询在1,2,4班的学生

排序查询: (order by 关键字)
asc : ascend 升序(默认)
desc: descend 降序
例: select * from student order by sid desc ;

聚合函数:(where 条件后不能接聚合函数)
sum():求和
avg():求平均值
count():统计数量
max():最大值
min():最小值
select sum(score) from student; //获取学生成绩总和
select avg(score) from student; //获取学生成绩平均分
select count(*) from student; //获取所有学生人数

select * from student where score > (select avg(score) from student); //查询成绩大于平均分的人

分组:group by
having关键字:可以接聚合函数,出现在分组之后
where关键字:不可以接聚合函数,出现在分组之前
根据班级分组,分组后统计学生的个数
select class,count(*) from student group by class;

根据class分组,分别统计每组学生的平均分,并且平均分 >60
select class,avg(score) from student group by class having avg(score) >60 ;

编写顺序:
select…from…where…group by…having…order by

执行顺序:
from…where…group by…having…select…order by

猜你喜欢

转载自blog.csdn.net/weixin_44264223/article/details/109732449