打开MySQL:
windows中启动服务
net start mysql
关闭: net stop mysql
.linux启动服务, 默认开启的
service mysql start
关闭: service mysql stop
查看数据库版本:select version();
查看当前时间: select now();
退出exit 或者 quit
库的操作:
1.展示所有的数据库
show databases;
展示所有的已经存在的数据库
mysql默认会有管理自己的库,表,用户,配置的数据库
2.创建一个库:
语法:create database 数据库名 charset="utf8";
例子:create database class charset=”utf8”;
3.删除一个数据库
格式: drop database 数据库名;
例: drop database class
;
4.改数据库(不建议修改数据库名字)
5.使用某个库:use 数据库名
6.查看当前使用的数据库; select database();
表的操作:
1.查询当前库中的所有的表:show tables;
2.创建一个表:
格式: create table 表名(字段名 字段描述,字段名n 字段描述,.....);
例子: create table student(id int primary key auto_increment,name varchar(20) not null,age int default 17,address varchar(20),sex bit default 1);
3.查看表的结构:
1.格式: desc 表名;
2.格式2: show create table 表名; 查看创建表的sql语句
4. 删除一个表
格式: drop table 表名;
例: drop table student;
5.修改表:
1.修改表名
格式1: rename table 旧表名 to 新表名;
2.修改表结构:
格式: alter table 表名 add | drop | change
1.添加一个新的字段
格式: alter table 表名 add 字段名 字段描述;
例: alter table people add phonenumber varchar(20);
2.修改一个字段
格式: alter table 表名 change 要修改的字段名 新的字段名 新的字段类型描述;
例: alter table student change phonenumber phone varchar(20);
**3.开发过程中尽量的不要修改已经有数据的字段 ,
设计表的时候尽量设计一些预留字段
3.删除一个字段
格式: alter table 表名 drop 字段名;
例: alter table people drop phone;
数据的操作:
1.增加数据:
1.插入一条数据:
格式: insert into 表名 values(对应的值1,对应的值2,对应的值n)
例: insert into people values(0,"零零一",38,"平壤",1);
注意:插入的值要与表对应字段一一对应
如果是自动增长的数据类型,将该值设置为 0即会自动的增长
2. 缺省值插入
格式: insert into 表名(字段1,字段2,字段n,...) values(字段1对应的值,字段2对应的值,字段n对应的值,....);
例: insert into people(name,age,sex) values("三棒子",74,0);
3. 插入多条数据
格式: insert into 表名 values(对应的值1,对应的值2,对应的值n),(对应的值1,对应的值2,对应的值n) ,(对应的值1,对应的值2,对应的值n) ....;
例: insert into people values(0,"零零三",66,"北京",1),(0,"零零四",55,"北京",1),(0,"零零女",67,"中南海",0),(0,"零零六",78,"北京",1),(0,"零零七",15,"东京",1);
2.删除数据
格式: delete from 表名 where 条件
例1: delete from people where id =2;
delete from people where name ='三棒子';
注意:删除一条数据后,该数据后的数据对应的id不会变
3.修改数据
格式: update 表名 set 字段名 = 值1,字段名2 = 值2 where 条件
例2: update student set name = '金三胖胖', address = '中国' where id =1;
4.查询数据库表中的所有数据:select * from 表名;
5.查询语句:
1.格式1: select 字段名1,字段名n,.... from 表名 where 查询条件
格式2: select 字段名1 as 别名,字段名n,.... from 表名 where 查询条件
* 表示显示所有字段的数据
from 后面是表名,表示从那个表中查询
where 条件 表示以某个条件进行筛选
如果没有where条件,表示查询所有
字段名1 as 别名 可以给显示的字段名取个别名,方便查看
查询条件:
- 比较运算符
> 大于
< 小于
>= 大于等于
<= 小于等于
= 等于
!= 不等于
例:查询大于60岁的人
select * from people where age > 60;
例:查询不等于55岁的人
select * from student where age != 55;
2.逻辑运算符
且 and
或 or
非 not
例:查询大于55岁且小于70岁的人
select * from people where age>55 and age < 70;
例:查询除了北京的人
select * from people where not address = '北京';
3.模糊运算符
格式: ..... where 字段名 like '字符串'
任意字符: % 任意多个任意字符
_ 一个任意字符
例:查询姓大的人
select * from people where name like '大%';
4.范围运算符
成员运算符
格式1: .... where 字段名 in (值1,值2,值3)
是否等于 () 中的某一个值
格式2: ... where 字段名 between 值1 and 值2
例:查询住在北京或者中南海的人
select * from people where address in ('北京','中南海');
5.空值判断
格式1: select * from people where 字段名 is null;
例:将address为空的所有数据筛选出来?
select * from people where address is null;
格式2: select * from people where 字段名 is not null;
例:将address不为空的所有数据筛选出来?
select * from people where address is not null;
6.运算符的优先级
1.以上运算符可以综合使用
2. 加上()
3.聚合函数
count(*) 统计查询结果的数量
max(字段名) 统计某个字段的最大值
min(字段名) 统计某个字段的最小值
avg(字段名) 统计某个字段的平均值
sum(字段名) 统计某个字段的总和
格式: select 聚合函数 from 表名 where 条件
例:查询该表总共有多少人
select count(*) from people;
例:获取人的最大年龄?
select max(age) from people;
例:统计年龄的平均值 ?
select avg(age) from people;
例:统计年龄的和
select sum(age) from people;
4.分组 group by
格式: select ..... from 表名 where 条件 group by 字段名
**统计某个字段有多少种值
例:查看有多少种地址 ?
select address from people group by address;
例:查询每个地址有多少人?
select address,count(*) from people group by address;
例:再查询中北京有多少人 ?
select address,count(*) from people group by address having address = '北京';
having 条件 表示在某个结果集上继续筛选
注意: where 与having 后面都是跟一个条件表示查询,
但是where是先筛选, having是在where的结果后再筛选
5.排序 order by
格式: select ..... from 表名 where 条件 order by 字段名 排序规则;
例:查询所有人,并按年龄排序(升序,默认的) ?
select * from people order by age desc;
降序: desc 升序asc 默认的
例:多个字段排序
格式: select ..... from 表名 where 条件 order by 字段名 排序规则,字段名2 排序规则;
6.分页:
格式: select ... from 表名 where 条件 limit 值1,值2;
例: select * from people limit 1,4;
值1 表示的是分页的起始位置, 注意: 从 0 开始
值2 表示的是每一页的结果数量
获取第n页数据, 每一页4条数据
select * from people limit 4(n-1),4;