找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
[size=19.5181pt]mysql 常用命令
[size=16.5153pt]1、安装及启动
[size=13.5125pt]rpm -aq|grep mysql 查看当前服务器是否有安装数据库
[size=13.5125pt]rpm -ev +文件名(上一步查出来的复制) --nodeps 移除安装
[size=13.5125pt]yum install -y mysql 安装数据库客户端
[size=13.5125pt]yum install -y mysql-server 安装数据库服务端 无空格
[size=13.5125pt]service mysqld[size=13.5125pt] start 启动Mysqld 数据库
service mysqld restart 重启数据库
service mysqld stop 停止数据库
mysqld mysql 后面这个d代表一个守护进程
第一次登录 mysql数据库不需要密码
mysql -uroot -p 进入mysql服务器
-u 表示用户user root 用户
-p 表示密码password
按住crtl+Z 停止 或exit退出数据库
修改用户密码:在linux界面
mysqladmin -uroot passwod "123456" 修改mysql 数据库
mysql -uroot -p123456 修改密码后登录mysql 数据库
show databases ;查看所有的库 分号表示这句话结束了
create database yalin ; 创建一个叫yalin的数据库
use yalin; 进入yalin;这个数据库
show tables; 查看当前所在库下的所有表
select database(); 查看当前已进入到哪个数据库中
drop database yalin; 删除yalin这个库
表结构
desc +表名;查看表结构
新建 user表
create table user(id int(5)primary key auto_increment,name varchar(10)not null,age
int(5),class int(5),phone bigint(20)default'13511111111',time date); 注意空格
数据类型
2.创建一个表
use +库名 先进指定库中 再create table +表名(
字段1名称,数据类型, 约束 ,
字段2名称,数据类型,约束,
字段3名称,数据类型,约束,

例如:创建一张学生表student,学号id, 姓名name.
create table student( id int(20) primary key,name varchar(30));
int 代表整型(整数) ,最大存储为2147483647 超过2147483647(十位) 用bigint
bigint ——长整型 存储手机号
varchar(10)存储字符串 最大存储10个字符 存入数据时需要加单引号或双引号
date——建表的时候在日期的后面接 date,并且加引号 '2022-05-31'
float ——小数默认保存6位精度(包括小数位和整数位)Float(20,2)指小数点后面2位
char 存储字符 char(10)占用了10个字符
注意点
int(3) int(M) M指最大显示宽度
char(20) char(M) M指最大能存储20个字符
3.约束约束用于对表中字段进行限制,保证表中数据的正确性和唯一性
约束类型
primary key 主键约束 说明:非空,唯一,用于唯一标识对应的记录。类似身份证。
foreign key 外键约束 说明:用于表与表建立关系模型,使表与表紧密的结合起来。
not null 非空约束 说明:字段值不能为Null
default 默认值约束 说明:默认给字段指定默认值
auto_increment 自增约束 说明:作用在整数类型,字段默认从1开始自增
修改添加表字段 标题列
1.查看表结构desc user;
2.修改表名 alter table +原表的名字 +rename+新表的名字;
如alter table user rename xinxibiao;修改 user这个用户表为xinxibiao
3.修改表字段
alter table +表名 change +原字段名 +新字段名 数据类型,约束
如 alter table xinxibiao change id sid int(10);将id 字段修改为sid 并修改显示宽度(int)
alter table xinxibiao change sid id int(5)auto_increment;将id 字段修改为sid 并增加自增长约束
添加 id和ID 是一样的,会显示重名
4.添加表字段,并放到第一个字段前
alter table +表名 add +字段名 数据类型 约束 first
alter table xinxibiao add id2 int(8); 增加id2这个字段
alter table xinxibiao add li int(10) first; 增加ili这个字段并且放在第一个字段前面
5.添加表字段,并放到某个字段后
alter table +表名 add +字段名 数据类型 约束 after +字段名
alter table xinxibiao add id7 int(10) after class; 添加 id7这个字段并且放在class后面
6.同时添加两个字段,默认添加到字段最后
alter table +表名 add(字段1 数据类型,字段2 数据类型)
alter table xinxibiao add(id3 int(10),id4(10)); 同时添加 id3和id4 两个字段
7. 删除表字段
alter table +表名 drop + 字段8.删除表两个字段
alter table +表名 drop 字段1,drop 字段2
alter table xinxibiao drop id2,drop id6,drop id7; 同时删除多个字段
9.修改主键id为自增长
alter table +表名 change 字段名 字段名 数据类型 auto_increment
10.删除表数据和表结构,全没有
drop table +表名
Mysql增删改语句
select * from xinxibiao; 查看当前表所有数据
insert into 插入数据
1.表中插入数据
insert into +表名 values(字段1value,字段2value,字段3value...)
2.一次性表中插入多条数据
insert into +表名 values(字段1value,字段2value,字段
3value...),(字段1value,字段2value,字段3value...)
写法一、insert into
xinxibiao(id,name,age,class,phone,time)values(6,"xiaohong",20,1401,13451561234,"2022.06.0
1");
写法二、insert into xinxibiao values(2,"xiaohong",20,1401,13451561234,"2022.06.01");
插入所有信息时,可以不写标题列
编辑后的信息
3.对表中指定字段插入数据
insert into +表名(字段1,字段2) values(字段1值,字段2值)
例如insert into xinxibiao(name)values("xiaoyang");
4. 删除表中指定数据 delete from +表名 where 条件;
delete from xinxibiao where id=6; 删除id=6的数据
删除全表数据 delete from +表名;
删除表数据和表结构,全删没 drop table +表名
删除表数据 truncate +表名 ;
5.更新表中指定字段数据update +表名 set 字段名=值 where 条件update xinxibiao set class=1400 where name="xiaoyang"; 修改xiaoyang这条数据的班级为1400
update xinxibiao set phone=12345678933 where id<7; 修改id<7的数据 的电话号码
查看单表查询
1、查看当前表所有数据 select * from +表名 ”*“ 代表所有
2、查询单个字段的数据 select * from +表名 where +具体条件
select *from xinxibiao where name="xiaohong"; 查询name为xiaohong的所有数据
查询一个字段的信息 select id,from xinxibiao where name="xiaohong"; 查询name为
xiaohong的id数据
3. 查询多个字段的数据
select 字段1,字段2 from +表名select id,name,class from xinxibiao where
name="xiaowu"; 查询name 为xiaowu的id,name,class信息
4.查询满足某个条件的所有数据
select * from +表名 where 字段=值
5、查询不满足某个条件的所有数据
select * from +表名 where 字段!=value
“!=” 代表不等于 ,也可以用符号 ”<>“ 代表不等于
select *from xinxibiao where name !="xiaohong"; 查询name字段不是xiaohong的数

或者写成select *from xinxibiao where name <>"xiaohong";
6、查询同时满足多个条件数据
select * from +表名 where 条件1 and 条件2
select *from xinxibiao where class=1402 and age=27; 查询班级为1402,年龄为27
的数据
7、查询满足至少1个条件的数据
select * from +表名 where 条件1 or 条件2 select *from xinxibiao where class=1402
or phone=13451561234;
8.查询一个条件范围内的数据
select * from +表名 where 字段 between m and n
between...and ... 指定一个范围
select *from xinxibiao where id>2 and id<8; 查询 id>2 and id<8 范围内的数据 不包
含2和8select *from xinxibiao where id>=2 and id<=8; 包含2和8
select *from xinxibiao where id between 2 and 8; 包含2和8
9查询字段满足在指定的集合中的数据
select * from +表名 where 字段 in(值1,值2,值3)
select *from xinxibiao where class in(1401,1402);
10查询字段不满足在指定集合中的数据
select * from +表名 where 字段 not in (值1,值2,值3)
select *from xinxibiao where class not in(1401,1402);不在1401和1402的数据
11.查询字段值为空的数据
select * from +表名 where 字段 is null
注意:字段是空不能写成 字段=null
select *from xinxibiao where class is null; 查询查询class字段为空的数据
12.查询字段不为空的数据
select * from +表名 where 字段 is not null
select *from xinxibiao where class is not null; 查询class字段不为空的数据
注:0不等于nul ,null指的是空的属性,0代表一个值
13.查询某个字段模糊匹配成功的数据
select * from +表名 where 字段 like “%值%”
%用于匹配字段开头和结尾select * from xinxibiao where name like "%xiao%"; 查询xinxibiao中名字以xiao开头的数据
14.查询限定的数量的数据
select * from +表名 where 字段 limit m,n
m 指下标,n指限定的数量,下标为m的开始的n条数据
m(下标值)=查询的开始行-1 m从0开始算
n(指定数量)=查询的截止行-m
查询第二行到第四行的数据select *from xinxibiao name limit 1,3;
查询四行的数据 select *from xinxibiao name limit 0,4;
15.查询的数据根据某个字段从小到大排序 升序
select * from +表名 order by 字段 asc
order by ...asc 从小到大排序
select *from xinxibiao order by id asc;
16.查询的数据根据某个字段从大到小排序 降序select * from +表名 order by 字段 desc
order by ... desc 从大到小排序
s elect *from xinxibiao order by age desc;
-- 查询emp表中所有的数据,然后根据部门的编号进行升序排列,如果部门编号一致,根据员工的编号进行降序排列
select * from emp order by deptno asc ,empno desc; 升序可以不写
17.查询的数据根据某个字段进行分组
select * from +表名 group by 字段
group by ... 根据字段分组
select *from xinxibiao group by class; 查询分班信息(通过class分组)
18.查询的数据根据某个字段进行分组再条件过滤
select * from +表名 group by 字段 having 条件
having跟在group by 后面,作用相当于where
select class,count(*) from xinxibiao group by class having class is not null;
通过class字段进行分组再过滤班级不为空的数据
group by分组后只能用having进行条件筛选
使用group分组后只有分组字段和函数可以放在from前面
select class,count(*)from grade where math>80 group by class; 求每个班数学成绩大于80的人数
mysql聚合函数 常用
19. 统计查询数据的数量
select count(*) from +表名
select class,count(*) from xinxibiao group by class; 查询每个班级多少人select count(*)from xinxibiao; 查询xinxibiao里面有多少数据;
20. 查询某个字段求和
select sum(字段) from +表名
select sum(age) from xinxibiao where class=1402; 年龄之和
21. 查询某个字段进行平均值
select avg(字段) from +表名
select avg(age) from xinxibiao where class=1402; 年龄的平均值
22. 查询某个字段最大值
select max(字段) from +表名
select max(id) from xinxibiao;
23. 查询某个字段最小值
select min(字段) from +表名
select min(id) from xinxibiao;
24 对某个字段进行去重 去除重复的
select distinct(字段) from +表名
select distinct(age) from xinxibiao;
单表查询总结
1.where 不能放在GROUP BY 后面
where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,
即在分组之前过滤数据,条件中不能包含聚组函数比如SUM(),AVG()等,使用
where条件显示特定的行。
2.having 是跟GROUP BY 连在一起用的,放在GROUP BY 后面,此时的作用相当
于WHERE。
having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含
聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册