MySql基础学习笔记

从零开始:

数据类型:

整型:int

浮点型:double 例如 double(10,2)表示最多10位 其中必须有两位小数 即最大值:99 99 99 99.99

浮点型:decimal  可以用于钱方面的类型 不会出现精度丢失问题

字符串类型:char 固定长度 最大255 比如身份证号或者学号可用

字符串类型 varchar 可变长度  最大65535

日期类型: date 格式为 yyyy-MM-dd

时间类型:time 时间类型,格式为hh:mm:ss

时间戳类型:timestamp

字符串类型: text :tinytext 2^8-1B text 2^16-1B mediumtext 2^24-1B longtext 2^32-1B binary 255B(固定长度二进制类型)

字节类型:blob 二进制

 

查看数据库列表:show databases

创建数据: create 例如:创建一个名为 student_tb的数据库 create database student_db;

删除数据库 :drop 例:删除名为student_tb的数据库 drop databse student_tb;

使用某个数据库:use 例如:打开一个叫student_db的数据库 :use student_db;

查看当前数据库下所有表 show tables;

创建一张名为 tb_stu的数据表,属性有姓名,年龄,学号,性别: create table tb_stu( number char(10),name varchar(20),age int,gender varchar(10) );

修改数据表关键字 alter: 

为tb_stu新增一行属性 education :alter table tb_stu add ( education varchar(50) );

为tb_stu中education的属性取值范围进行修改: alter table tb_stu modify education varchar(100);

删除tb_stu中的education的属性: alter table tb_stu drop education;

把名为 tb_stu的数据表更名成tb_student: alter table tb_stu rename to tb_student;

数据表中添加数据 insert into, insert into  tablename (列名,列名)values(值,值)  

列名如果不填 则表示默认插入所有列,值的顺序与表的列名顺序相同

更新数据库:update 表名 set 列名=值,列名=值。

例如:修改tb_stu 中的age 属性为18 : update tb_student set age=18;这么改会更改全部数据

如果要设置条件的话应该这么写 update 表名 set 列名=值,列名=值 where 列名=值 

例如:把tb_student 表中名字是王五的 年龄改成32:

update tb_student set age=32 where name='王五';

对于数值类型区间操作:

比如 把表中age在18到25之间的数据 age全部加1

update tb_student set age=age+1 where age=>18 and age<=25;

或者 
update tb_student set age=age+1 where age between 18 and 25;

对于值区间操作:

比如 把表中姓名是张三或者李四的 age全部加1

update tb_student set age=age+1 where name='张三' or name='李四';

或者 

update tb_student set age=age+1 where name in( '张三','李四');

对于表中 某一行数据某一列数据为null 对此修改的话不能用= 用is

比如表中有一条数据 01234567891 zhangsan null nan,

要修改该条数据 

update tb_student set age=8 where age is null;

删除数据库  delete from 表名 where 条件 全部删除的话 不加条件

指定列查询 :

假如只想查询姓名 和年龄

seletc name,age from tu_student;

关于重复数据的数值查询 :

首先假如我的数据是这样的

现在我要查询 年龄有多少 应该是18 26,用到关键字distinct

select age distinct age from tb_student;

关于包含null类型的数据查询 可用 ifnull(列名,替代值)指定其替代值

比如我的表是这样的:

可用看到第一条数据的grender 为null

select ifnull (grender,'nan') from tb_student;

连接字符串在mysql中 用关键字 CONCAT(列名,列名)

比如还是上图的数据库数据,我要连接name和grender的值

select CONCAT(name,grender) from tb_student;

因为第一行数据是null,可用和ifnull关键字合用,替代null的值为nan

select CONCAT(name,ifnull(grender,'nan')) from tb_student;

再有这样:

给查询的数据起别名话用到as

模糊查询 用到关键字 like  如:查询表中name z开头的数据

select * from tb_name where name like'z%'; %代表0到N个字符 _代表1个字符

排序查询关键字 order by   desc(降序) asc(升序)

select * from tb_student order by age desc;

根据多个字段排序的话 直接在后边追加 都好分开

select  * from tb_student ORDER BY age asc,name desc;

这句则表示查询表中按照年龄升序排序 如果年龄相同 则按照姓名降序排序

然后是关键字 count  表示查询数量 

例如 select count(*) from tb_student;

可见表中数据是3条,然而如果把* 换成grender则显示2条 

因此 count适用于非Null的数据计数 即有效行数 如果表中有一行数据全是null,则count也不会将该条数据计入总数

其余  还有sum,max,min ,avg(平均)的关键字  用法类似 

select max(age) from tb_student;

分组查询关键字 group by;

select age,count(*) from tb_student group by age;

分组后条件语句查询用到having 

比如 分组查询年龄并且人数大于1的:

select age,count(*) from tb_student group by age having count(*)>1;

还有行数限制语句limit:

limit 限定查询结果的起始行,以及总行数

例如 查询其实行 第五行,查询一共3条数据

select * from tb_student LIMIT 4,3;

即表示查询的是5 6 7行数据

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值