MySQL的基本操作

1.数据库的基本操作
1. create database hello;   #创建数据库
2. show databases;  # 查看当前有哪些数据库
3. drop database hello;  # 删除hello数据库
4. use hello; # 选择数据库hello,必须要选择了数据库才可以对表单进行操作
5 show tables;  # 查看在哪个数据库下,并且查看有哪些表单
2.表单的基本操作
1.  create table test(id int(10),sex char(10));  # 其中创建表的时候最少包含一个属性
2.  desc test;   # 查看test表单有哪些字段以及向对应的属性
3.  drop table test;  # 删除test表单
4.  alter table test rename hello;  # 将test表单的名字改为hello
5.  alter table hello add name char(10); # 给hello增加一个name的字段
6.  alter table hello drop name;  # 给hello表单删除name的字段
7.  alter table hello change sex sex int not null primary key;  # 将字段是sex 的字段名字换成sex,并且将需要改变的属性填写上去,注意:改变字段属性的时候一定要写上两个字段名字,
    并且后面一定要写上type类型(不改变也要写回原属性),不然会报错
3.表单数据的操作
1.select * from hello;   # 查看当前表单的所有数据
2.insert into hello(id,sex)  values(01,"男");  # 插入数据,可以选择插入哪些对应字段的数据
3.update hello set sex="女" where id=01; # 修改数据where后面接上判断条件
4.delete from hello where id=02;  # 删除id是2的数据
4.复杂一些的筛选操作
1.  where : 判断条件
    如:select * from hello where id<10;
2.  having:也是判断条件,但是后面加上的是虚拟字段条件
    如:select * from hello having max(id)
3.  group by:分组,筛选按照什么分组
    如:select * from hello by name;  # 名字相同的只会显示一行
4.  like:模糊查询
    如:select * from hello where name like "%张%" ;   # 查询某个叫做某张某的
5.  avg():求平均
    如:select avg(id) from hello;
6.  max():最大值
7.  min():最小值
8.  distinct():去重
9.  order by:排序
    如:select * from hello order by id desc;# 按id降序筛选,升序只需要将desc去掉
10. limit:限制多少行输出
    如:select * from hello limit 3;  # 只显示前3行

        select  * from hello limit 3,3;  # 显示第4到6行

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。