MySQL小结

一、基本命令

1.启动服务
以管理员身份运行cmd net start mysql57

2.停止服务 net stop mysql57

3.连接数据库 mysql -u root -p

4.退出登录(断开连接)quit或者 exit

5.查看版本 select version();(注意分号)

6.显示当前时间 select now();

7.远程连接 mysql -h ip地址 -u 用户名 -p

二、数据库操作

1.创建数据库 create database 库名 charset=utf-8;

2.删除数据库 drop database 库名;

3.切换数据库 use 库名;

4.查看当前使用的数据库 select database();

三、表操作

1.查看当前数据库中所有表 show tables;

2.创建表 create table 表名(列及类型);

create table student(
                    id int auto_increment primary key, 
                    name varchar(20) not null,
                    age int not null,
                    gender bit default 1,
                    address varchar(20),
                    isDelete bit default 0
                    );

3.删除表 drop table 表名;

4.查看表结构 desc 表名;

5.查看建表语句 show create table 表名

6.重命名表名 rename table car to newCar;

7.修改表结构 alert table 表名 add|change|drop 列名 类型;

alert table newcar add isDelete bit default 0;

四、数据操作

1.增
a.全列插入:insert into 表名 values(…)

insert into student values(0,'tom',19,1,'北京'0)

注意:第一个0是占位

​ b.缺省插入:insert into 表名(列1,列2,…)values(值1,值2,…);

insert into student(name,age,address) values('lilei',19,'上海');

​ c.同时插入多条数据 insert into 表名 values(…),(…),…

insert into student values(0,'hanmeimei',19,0,'北京',0),
                        (0,'popo',22,1,'海南'0);

2.删
delete from 表名 where 条件;

delete from student where id=4;

注意,没有条件是全部删除,慎用

3.改
update 表名 set 列1=值1,列2=值2,…where 条件

update student set age=16 where id=7;

注意:如果没有条件,全部列都会修改,慎用

4.查
查询表中全部数据 select * from 表名;

五、查

1.基本语法
select * from 表名;
select后面写表中的列名,如果是*,表示结果集中显示表中的所有列
如果要查询多个列,之间使用逗号分隔

select name,age from student;

select name as a ,age as b from student;

2.消除重复行
在select后面,列的前面使用distinct可以消除重复的行

select gender from student; 

select distinct gender from student;

3.条件查询

a.语法
select * from 表名 where 条件;

b.比较运算符
等于  =
大于  select * from student where id > 8;
大于等于 select * from student where id >= 8;
小于等于 <=
不等于 != or <>

c.逻辑运算符
and select * from student where id>7 and gender=0; 
or
not 

d.模糊查询
like    
%表示任意多个任意字符
_表示一个任意字符
select * from student where name like '习%';
select * from student where name like '习_';

e.范围查询      
in 表示在一个非连续的范围内
between...and... 表示在一个连续的范围内

select * from student where id in (8,10,12);
select * from student where id between 6 and 8;

f:空判断
注意 null与‘’不同
判断空 is null
判断非空 is not null

select * from student where address is null;

g.优先级
小括号,比较运算符,逻辑运算符
and比or优先级高,如果同时出现并且想用or,就用小括号

4.聚合
为了快速得到统计的数据,提供了5个聚合函数
a. count() 表示统计总行数,括号中可以写和列名
b. max(列) 表示求此列的最大值
c. min(列) 表示求此列的最小值
d. sum(列) 表示求此列的和
e. avg(列) 表示求此列的平均值

select count(*) from student; 
select max(id) from student where gender=0;
select sum(age) from studnet where gender=0;

5.分组
按照字段分组,表示此字段相同的数据会被放到一个集合中。
分组后,只能查询出相同的数据列,对于有差异的数据列,无法显示在结果集中。可以对分组后的数据进行统计,做聚合运算。

select gender,count(*) from student group by gender;    计算男女各有多少个

select gender,count(*) from student group by gender having age>20

where是对from后面表中的进行筛选
having是对group by的结果进行筛选

6.排序
select * from 表名 order by 列1 asc|desc,列2 asc|desc,…
将数据按照列1进行排序,如果某些列1的值相同,则按照列2的进行排序
默认从小到大排序,asc升序,desc降序

select * from student order by age;

select * from student where isDelete=0 order by age desc,id desc;

7.分页
select * from 表名 limit start,count;

select * from student limit 0,3;

select * from student limit 3,3;

select * from student where gender=1 limit 0,3;

注意:索引从0开始

六、关联

1.建表语句:

1.create table class(
                    id int auto_increment primary key,
                    name varchar(20) not null,
                    stuNum int not null
                    );

2.create table student(
                    id int auto_increment premary key,
                    name varchar(20) not null,
                    gender bit default 1,
                    classid int not null,
                    foreign key(classid) references class(id)
                    );

2.插入一些数据:

insert into class value(0,'python01',55),
                    (0,'pyhton02',50);

insert into students value(0,'tom',1,1);

猜你喜欢

转载自blog.csdn.net/HS_blog/article/details/81283158