mysql创建数据库,表,修改字段,插入数据,排序,模糊查询,聚合函数,分页查询

本文详细介绍了MySQL数据库的基本操作,包括创建、展示、删除数据库,以及创建、修改、删除表和字段。还涉及数据的插入、查询、更新和删除,批处理,以及各种查询条件如比较运算符、模糊查询和排序。此外,还提到了聚合函数、分页查询和值运算。
摘要由CSDN通过智能技术生成

1 关于数据库

create database db charset=utf8 //创建数据库。charset=utf-8,默认为安装mysql时选的字符集
show create database db         //展示数据库相关信息
drop database db                //删除数据库
show databases                  //展示所有数据库
use db                          //使用数据库

2 关于表

create table t1(name varchar(50),age int)charset=utf8  //创建表
create table t2(id int primary key auto_increment)     //id为主键约束,并且自动增加
show create table t1                                   //查询表信息
desc t1                                                //查询表字段 
rename table t1 to t2                                  //修改表名。t1=原表名  t2=新名
drop table                                             //删除表

3 关于表字段

alter table t1 add sex varchar(2)             //表最后面sex字段
alter table t1 add sex varchar(2) first       //表最前面添加sex字段
alter table t1 add sex varchar(2) after name  //在字段name后添加字段
alter table t1 drop sex                       //删除字段sex
alter table t1 change sex xingbie varchar(5)  //修改字段名 sex原名  xingbie新名

4 关于插入数据

insert into t1 values('李白',18)                    //插入一行全部数据
insert into t1(name) values('李白')                 //插入一行,部分数据
insert into t1 values('李白',18),('杜甫',20)        //插入多行,全部数据
insert into t1(name) values('李白','杜甫')          //插入多行,部分数据

5 关于数据

select name from t1                              //查看t1表中,name的值
update t1 set age=12 where name='李白'           //修改‘李白’的,年龄
delete from t1 where name='杜甫'                 //删除名字为‘杜甫’的一行数据

 6 批处理.sql文件

source d:/db.sql

7 查询相关distinct,is null ,比较运算符,between,in

select distinct name from t1                         //distinct,去除name中重复的列
select name from t1 where age is null                //年龄为null
select name from t1 where age is not null            //年龄不是null
select * from t1 where age>30 and age<50             //年龄在30-50,不包括30和50
select * from t1 where age>30 or age<50              //年龄小于30,或年龄大于50
select * from t1 where job!="李白"                   //名字不是李白
select * from t1 where job<>"李白"                   //名字不是李白
select * from t1 where age between 30 and 50         //年龄在30-50,包括30和50
select * from t1 where age not between 30 and 50     //年龄不在30-50
select * from t1 where age in(20,50)             //年龄是20或50

8 模糊查询like

select * from t1 where name like "李%"        //名字以'李'开头的 %:代表0或多个未知字符
select * from t1 where name like "_白%"       //第二个字是'白'的 _:代表1个未知字符 

9 排序order by

select * from t1 order by age                        //按年龄升序排列 (不写默认升序)
select * from t1 order by age asc                    //按年龄升序排列
select * from t1 order by age desc                   //按年龄降序排列

10 聚合函数

select avg(age) from t1                              //平均年龄
select max(age) from t1                              //最大年龄
select min(age) from t1                              //最小年龄
select sum(age) from t1                              //年龄总和
select count(age) from t1                            //统计,总共多少行数据
select name 名字 from ti                             //'name'起别名,为'名字'

11 分页查询 limit

//0 代表跳过条数       3 代表请求条数
select * from t1 order by age limit 0,3   //age升序排列,从0行开始,显示三行
select * from t1 order by age limit 3,3   //age升序排列,从第三行开始,显示前三行

12 值运算 + - * / %

select age,age*2  from t1      //年龄扩大2倍
select age,age+2  from t1      //年龄增加2
select age,age-2  from t1      //年龄减少2
select age,age/2  from t1      //年龄减少2倍
select age,age%2  from t1      //年龄对2求余

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值