MySQL最基础命令:数据库、表、表数据增删查改

启动MySQL:net start mysql;
关闭MySQL:net stop mysql;或者是exit;
连接MySQL:mysql -uroot -p;
数据库注释方式:#   或者 --

对数据库操作

数据库操作之:增
创建数据库:create database 数据库名;
创建原先没有的数据库:create database if not exists 数据库名;
创建自定义字符集数据库:create database 数据库名 character set 字符集;
配合上述方法使用:create database if not exists student character set gbk;
数据库操作之:删
删除指定数据库:drop database 数据库名称;
先判断再删除:drop database if exists study;
数据库操作之:查
查询当前使用的数据库名称:select database();
查看本机所有数据库:show databases;
查看某个数据库:show create database 数据库名字;
数据库操作之:改
修改数据库字符集:alter database 数据库名称 character set 字符集名称;

对表操作

数据库操作之对表操作
操作一个数据库的表,需要先进入这个数据库:use 数据库名称;
对表操作之:增
create table 表名(
列名1 数据类型1,
列名2 数据类型2,
......最后一个不需要逗号
);
复制表:create table 新表名 like 要复制的表名;
对表操作之:删
表的删除:drop table 表名;
删除某一个字段:alter table 表名 drop 列名;
判断是否存在再删除:drop table if exists表名;
对表操作之:查
查询某个数据库里面的所有表:show tables;
查询表结构:desc 表名;
对表操作之:改
修改表名:alter table 表名 rename to 新的表名;
修改表的字符集: alter table 表名 character set 新的字符集;
添加一列: alter table 表名 add 新的一列名字 数据类型;
修改某个字段数据类型: alter table 表名 change 原先字段名  新字段名 新数据类型;
只改变数据类型: alter table 表名 modify 字段名 新的数据类型;

对表数据操作

对表数据操作之:增(这里的列指的是字段)
添加数据:insert into 表名 (列1,列2,……,列n)values (值1,值2,……);值未知填null
可以省略字段:insert into 表名 values(值1,值2,……);值必须全写
添加多条(行)数据也可省略:insert into 表名 values(值1,值2,……),(值1,值2,……),(值1,值2,……);
对表数据操作之:删
删除数据:delete from 表名 where 条件;如果不加 where 条件,则删除整个表会被删除不推荐使用效率低,会执行很多次
推荐使用: truncate table 表名;先删除后创建一样的表;
对表数据操作之:查
select 字段列表 from 表名 where 条件列表 group by 分组字段 having 分组后的条件 order by 排序 limit 分页限定;
查询表的记录的数据: select 列名1,列名2……  from 表名; select * from 表名; *表示全部字段
数据相同只显示一次:select 列名1,列名2…… address from 表名;
数据求和输出举例:select name,math,english,math+english from student;如果参与计算有null那么结果就是null
                select name,math,english,math+ifnull(english,0) from student;ifnull(为null列名,参与运算的值)
起别名:select name,math,english,math+ifnull(english,0) as 总分 from student;
        select name as 姓名,math as 数学,english as 英语,math+ifnull(english,0) as 总分 from student;
也可以不写as直接空格就行:select name 姓名,math 数学,english 英语,math+ifnull(english,0) 总分 from student;
条件查询:
select *from student where age>20;
范围查询:
and或者&&   select *from student where age>20 and age<30;
                between……and……:select *from student where age between 20 and 30; 包含了20  30
多条件查询 or 或者||:
        select *from student where age=20 or age=22 or age=44;
        多条件查询简化写法:select *from student where age in(20,22,44);
        null查询方法:select *from student where english is null; select *from student where english is not null;
模糊查询:
    使用like语句
    占位符:_单个任意字符
    :%任意多个字符
    select *from student where name like '马%';  任意长度
    select *from student where name like '马_'; 两个字  三个字'马__'
    也可以查询特殊位置 比如第二个字  "_化%"
    姓名是三个字的人  "---"
    姓名中包含德的人  "%德%"
排序查询:
    语法:order by 排序字段 排序方式  默认是从小到大可不写方式
        select *from student order by math;
    升序 ASC(默认方式)   DESC(降序排列)
        select *from student order by math desc;
    多字段排序  比如数学成绩一样时候,我们可以在对其按英语成绩比较
        select *from student order by math ASC,english ASC;
聚合函数:
    select 函数名(列名)from 表名;
    count():计算个数 select count(name) from student; 他会排除该列有null的数据
    比如有个人英语为null那么count(english)人数就会少一个  解决办法是 设置为0
    或者 count(ifnull(english,0))就可以了
    或者是 count(*)
    max():计算最大值 select max(math) from student;
    min():计算最小值 select min(math) from student;
    sum():求和    select sum(english) from student;
    avg():求平均    select avg(english) from student;
分组查询:
    比如男女一组,分别查询平均分等
    语法:group by分组字段(必须要有意义,如下面要是前面加上name则输出结果没有意义)
    男女数学平均分
    select sex,avg(math) from student group by sex;  没有*号
    男女学生人数
    select sex,count(name) from student group by sex;
    限定分组条件 按男女分但是分数不得低于70
    select sex,avg(math) from student where math>70 group by sex;
    再进行限定,人数小于4的不参与分组 having
    select sex,avg(math) from student where math>70 group by sex having count(id)>2;
    男女分组 数学大于70记录个数 此时只有女生满足
    where having区别
        where 在分组之前限定  不满足条件不分组  having在分组之后限定 不满足结果,不被查询出来
    where不可以跟聚合函数 having可以跟聚合函数
对表数据操作之:改
修改数据:update 表名 set 列名1=值1,列名2=值2…… where 条件;如果不加条件,会把该列所有数据全部修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值