mysql之初级学习(一)

mysql初级学习(一)

1.mysql数据类型

mysql数据库是一种强类型的语言,

  1. 数字 :nt(整数 最大到10位) float(小数) number(数字)decimal(数字);
  2. 文本: char(字符串<=2000)-定长 varchar(字符串<=2000)-变长 text(大文本类型:几万个字是没有问题的,野史【官网没有详细说明】两亿个字);
  3. 大对象:二进制数据 blob: 通过高级语言进行控制转成二进制码,进行控制(有的时候:微信的照片,公安系统的身份证号) 视频音乐等信息数据库存储路径,视频存储硬盘
  4. 日期: datetime;

2.表 内容讲解

CREATE TABLE stuInfo(
sno int primary key auto_increment,
sname varchar(20) unique not null,
age int not null check(age>0 and age<100),
city varchar(30) default ‘地址不详’
)charset=utf8;

—约束:
—primary key :主键–不重复–能够唯一的表示一行数组
— auto_increment:自动递增 自动增长如果删除一个,索引值自动往下,并不会重复这个索引
—unique 唯一 考虑当前列取值的唯一
—not null:必填
—mysqlcheck发生冲突约束失效 其他数据的有效 check(age>0 and age<100)
—default ‘地址不详’

删除表:
drop table stuInfo
添加列,删除列
alter table 表名 add(增加)/drop(删除) 列 【类型】
alter table stuinfo add column tel char(11)
alter table stuinfo drop column tel

基础查询
—查询所有行所有列
select * from stuinfo

—查询个别列
—查询学生的姓名和城市
select sname,city from stuinfo

—查询个别列时,可以起别名
select sname as 姓名 ,city as 城市 from stuinfo
as关键字可以省略
select sname 姓名,city 城市 from stuinfo
多条件查询where
—查询部分行,where条件
—查询家庭住址在天津的同学信息
select * from stuinfo where city=‘天津’

—多条件查询:and or
—家庭在天津并且年龄小于20
SELECT * from stuinfo where city=‘天津’ and age<20

–查询家庭住址在邯郸或者年龄大于18的
select * from stuinfo where city=‘邯郸’ or age>19

—算术运算符或者数学运算符
—查询计算 算术,关系,> < >= <= != = 没有加加减减其他的和其他语言的算术运算符几乎一样

—求余数
select 10%3
select MOD(10,3)
—除法 前面3.333 后面3
select 10/3
SELECT 10 div 3

—补充:
select * from stuinfo
—注意:和null比较时,=与!=都失效
— city=’’ 被删除的数据查询 数据约束 is null:为空数据查询
—is not null相反
select * from stuinfo where city=’’
select * from stuinfo where city is null
select * from stuinfo where city is not null

**—模糊查询 like **
—通配符 :下划线表示一个未知字符 %:百分号多个未知字符==
—查询学生表中姓王的同学信息
select * from stuinfo where sname like ‘王%’
—查询学生姓名姓韩 两个字名字的学生信息
select * from stuinfo where sname like '韩

**—排序查询 **
order by (排序字段名称) asc(从小到大) desc(从大到小)
—根据学生年龄进行排序显示
select * from stuinfo order by age desc
—插入行
alter table stuinfo add column scope int
–多值排序 asc或者desc必须在每一个排序后面声明一次(先按照前一个排序,前面相同按照后一个排序)
select * from stuinfo order by scope desc,age desc

—消除重复记录 distinct
select distinct age from stuinfo

**—限制查询(分页查询) limit 只在mysql好用 **
—数据分页:每一页只显示3行 其他的换到下一行
—行索引从0开始 起始索引,截取长度 start=(page-1)*showNum
select * from stuinfo limit 0,3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值