第二天MySql(2)

MYSQL第二天

alter

修改的作用 修改库 修改表 修改字段

修改库的编码集

alter database shujia character set utf8

修改表的编码集

alter table students character set utf8

修改表名

alter table student rename to students

修改字段:change(字段一旦包含数据:类型的变化)

能修改字段的所有内容:可以修改成新的 也可以在原字段上修改

# 修改成一个新的字段 alter table students change age agenew int 属性; # 修改原字段的属性 alter table students change age age int 属性;

修改字段:modify(字段一旦包含数据:类型的变化)

只能在原字段上进行修改,不能修改名称

# 修改age的类型 从int>varchar alter table students modify age varchar(255) 属性;

添加字段

alter table studnets add 字段名 字段类型 字段属性; alter table studnets add clazz varchar(255) 属性;

删除字段

alter table studnets drop clazz;

insert

添加数据格式:insert into 表明(字段名1,字段名2...字段名n) values('值1','值2',....'值n'),(),.....();注意事项: 1.值一定要加'' 方便与其他的语言的使用 2.字段和值之间的数量要一一对应 3.字段和值之间的位置一一对应 4.字段可以不按照顺序给

insert into student(id,name,age,sex)values('1001','test','18','0'); insert into student(age,name,id,sex)values('1001','test','18','0');# 1001添加到了age insert into student(id,name,age,sex)values('1001','test','18','0'),('1002','test','18','0');

update

格式:update 表名 set 字段1='值1',字段2='值2'...字段n='值n'默认修改所有数据

update students set name='show'; update students set age='22'; update students set name='show',age='22';

Select

查询格式:select 字段1,字段2...字段n from 表名如果显示的时候 显示另外的格式选择as

select * from students;# *表示所有字段 select id,name,age,sex from students; select id,age,sex,name from students; select id as '学号',name as '姓名', age '年龄' , sex '性别' from students;

where子句

用于做操作的筛选格式:操作 where 判断条件> < = = <>大于、小于、大于(小于)等于、不等于between ...and...显示在某一区间的值in(set)显示在in列表中的值,例:in(100,200)like '张_'模糊查询 使用% (匹配所有)和 _(匹配一个所有)Is null判断是否为空

select * from student where age>=18; select * from student where age between 18 and 22;# 查询age在18-22的 select * from student where age in(18,22); # 查询age等于18和22 select * from student where name like 'z%'; # 查询z开头的 select * from student where name like 'z_'; # 查询z开头两个字的 select * from student where name is null;

and多个条件同时成立or多个条件任一成立not不成立,例:where not(expection>10000);如果not和is一起使用 写法为:is not null

select * from students where age>=18 and age<=22; select * from students where name='zs' or name='ls';

delete

删除格式:delete from 表 [where 子句]表示清空所有数据

delete from stduents;# 默认清空所有数 delete from students where sex='0';# 删除sex=0的数据 delete from students where name like 'z%';#删除name等于z开头的

常用函数和操作

DISTINCT (去重)

格式:DISTINCT 字段1,字段2...字段n注意:1.有DISTINCT的SQL语句只能做去重使用2.DISTINCT会根据之后的字段一起做去重

select DISTINCT name from students; # 对name去重 select DISTINCT name,age from students; # 对name和age一起去重

字段进行算数运算

字段运算符字段注意:varchar类型的问题 数值类型的varchar可以进行算数运算

select price+sum from goods;

字段的拼接

concat

字段和字段之间如果有拼接符 需要手动加上格式: concat(值1,值2...值n)

select concat(name,age) from students; select concat(name,'-',age,'-',sex) from students;

concat_WS

字段和字段之间如果有拼接符 需要只当拼接符 自动去加格式:concat_WS(拼接符,值1,值2...值n)

select concar_WS('-',name,age) from students # 结果会在name和age之间自动加上-

聚合函数

count() 格式:count(列) 注意:求一个列 不会计算nullavg() 格式:avg(列) sum() 格式:sum(列) min() 格式:min(列) max() 格式:max(列)

select count(age) from students; # 求age这一列有多少条记录 select count(*) from students; # 求总共有多少条记录 select avg(age) from students; select sum(age) from students; select min(age) from students; select max(age) from students; # 可以获取varchar的最大值 和最小值 select min(name) from students; select max(name) from students;

浮点数的计算

floor():向下取整ceil() :向上取整round(X,D): D表示保留几位小数 四舍五入 针对于D+1位round(X):四舍五入 不保留小数 只针对对于第一位小数rand();随机产生0-1之间的小数

select floor(10.5);# 10 select ceil(10.2); # 11 select round(10.2);#10 select round(10.4555,2);#10.46

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值