MySQL:一些常用的语句(持续更新)

一、创建example表
create table example(id int primary key auto_increment, --创建整数型自增主键
name varchar(4) not null, --创建非空字符串字段
math int defaut 0, --创建默认值为0的整数型字段
minmax float unique --创建唯一约束小数型字段
);

二、修改表名:
alter table 旧表名 rename 新表名;
例:alter table example rename student; --将example表的表名改为student

三、修改字段的数据类型:
alter table 表名 modify 字段名 数据类型;
例:alter table student modify math float; --将字段math改为小数型

四、修改字段名:
alter table 表名 change 旧字段名 新字段名 数据类型;
例:alter table student change name stuname varchar(8); --将student表中的name字段改名为stuname,并且设置该字段的数据类型为varchar(8)

五、增加字段:
alter table 表名 add 字段名 数据类型[完整性约束条件];
例:alter table student add english float default 0; --为student表新增一个小数型、默认值为0、字段名为english的字段

六、删除字段:
alter table 表名 drop 字段名;
例:alter table student drop math; --从student表中删除math字段

七、修改字段顺序:
alter table 表名 modify 字段名 1 数据类型 first/after 字段名2;
例1:alter table student modify stuname varchar(8) first; --将stuname字段顺序修改到第一个位置
例2:alter table student modify stuname varchar(8) after id; --将stuname字段位置修改到id字段之后

八、删除表:
drop table 表名;
例:drop table student; --删除student表

九、为表添加数据:
在mysql中有两大类为表添加数据的方法,一类是使用insert语句为表添加键入的数据,另一类是为表导入外部数据源中的数据。
键入数据:
insert into 表名(字段1,字段2, …,字段n) values(值1,值2,…,值n)
例:insert into student(id, stuname, math, minmax, english) values(a01,李雷,85.2,0.3,92.5);

导入外部文本文件:
load data[local] infile ‘文本文件所在完整路径’
into table 表名
fields terminated by ‘文本文件的分隔符符号’
ignore 省略的行数 lines;
例:为student表导入本地E盘下student.csv文本文件中从第二行开始的所有数据
load data local infile ‘E:/student.csv’ --此处E:后边的分隔符需要使用/,而不能使用
into table student
fields terminated by ‘,’ --csv文件的分隔符为都好,所以此处指定’,’
ignore 1 lines; --忽略第一行数据,从csv文件的第二行开始导入数据

十、查看表结构:desc 表名
例:desc student; --查看student表的表结构信息

十一、select语句语法:
select 字段列表 from 表名
[where 条件表达式1] --用where语句指定查询条件
[group by 字段名1 [having 条件表达式2]] --用group by语句进行分组,用having语句指定分组条件
[order by 字段名2 [asc/desc]] --用order by语句进行排序,asc为升序排序,desc为降序排序
例1:从student表中选取math大于0的所有数据并按照math字段的降序进行排序
select * from student where math>0 order by math desc;
例2:在student表中对id字段进行分组,并求出每组id中数学成绩大于0的所有学员的平均数学成绩
select id, AVG(math) from student group by id having avg(math)>0;

十二、在SQL中通过UPDATE语句来更新表中已经存在的字段内容,通过DELETE语句删除表中不再使用的记录
UPDATE语句语法:
update 表名 set 字段名1 = 取值1,
字段名2 = 取值2,

字段名n = 取值n;
例:为student表增加一个mathandenglish字段,并用数学成绩与英语成绩的加总成绩为此字段赋值
alter table student ADD mathandenglish FLOAT DEFAULT 0;–增加字段
update student set mathandenglish = math+english; --用math与english的加总值为mathandenglish赋值

DELETE语句语法:
DELETE FROM 表名 [where 条件表达式];
例:删除student表中数学成绩小于40分的记录
DELETE FROM student WHERE math<40;

十三、当表的数据量太大时,使用视图是一种便捷访问数据的方法,使用create view语句可以创建视图;
create view 视图名 as select 字段 from 表名

十四、连接两张表:
select 表1.字段1,表1.字段2,表2.字段1,表2.字段2 from 表1 inner/left/right/outer join 表2 on 表1.字段1 = 表2.字段2;

十五、使用关键字:
AND, OR, IN, BETWEEN, LIKE, ISNULL, DISTINCT
例:select * from student where id in(‘a01’,‘a02’);
例:select * from student where math between 60 and 85;
通配符:%代表任意长度的字符串, _代表单个字符
例:select * from student where id not like ‘a*’;
not是可选参数,加上not表示字段不是空值时满足条件:
例:select * from student where id is not null;
DISTINCT关键字:用来消除重复记录
SELECT DISTINCT 字段名
例:select distinct id from student;

十六、将符合需求的元素展现在同一行上
例:select stuname, GROUP_CONCAT(id) from student group by stuname;

十七、快速修改数据类型
例:select CAST(12 AS int)语句可以将字符串型的12转换为整数型的12

十八:清除表中的空行;
例:delete from 甩单数据-训练数据 where 甩单流水号 is null;

十九:匹配
例:update 订单数据-训练数据 LEFT join 甩单数据-训练数据
on 订单数据-训练数据.甩单流水号 = 甩单数据-训练数据.甩单流水号
SET 订单数据-训练数据.甩单备注 = 甩单数据-训练数据.甩单备注;

二十、快速删除表中的所有数据,但保留表结构
TRUNCATE TABLE 表名;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值