mysql语句二(alter table 表内容修改、备份数据库、修改表数据updata set、单表查询select)
一、alter table + 表名 --------对表内容修改
1、添加表字段: add
alter table +表名 add 字段名 字符范例(字符长度);
例如:alter table student add dcs int(10);
https://i-blog.csdnimg.cn/direct/2240a8369d8a4a92a0652ea9f8e678c3.png
2、删除表字段:drop
语法格式:alter table + 表名 drop + 字段名
alter table student drop hzdcs;
https://i-blog.csdnimg.cn/direct/b0a8c603fc3344f2937d129961bd08a7.png
3、修改表字段:change
语法格式:alter table + 表名 change 原字段名 新字段名 + 字符范例(字符范例长度)
alter table student change dcs hzdcs int(10);
https://i-blog.csdnimg.cn/direct/b61351a1f1354ffd86622a94413f554f.png
4、修改表名字:rename
语法格式:alter table +表名 rename 新表名
alter table student rename student_2;
https://i-blog.csdnimg.cn/direct/bfe0fb7965994ed79c3192213a72ea4e.png
5、调解表字段顺序:modify
语法:alter table +表名 modify +原字段名 + 字符范例(字符范例长度) after 表中字段名
alter table student modify math int(10) after name -----------把student表中的math字段放在name字段后面
https://i-blog.csdnimg.cn/direct/0e9dca7869e841c88cf1e351cab17e7d.png
6、 添加新字段到表的第一位:first
alter table +表名 add 新字段名 字符范例(字符范例长度) first;
alter table student add no int(10) first;
https://i-blog.csdnimg.cn/direct/e258adb7dc8d48bbb051b0a86aa8b5e2.png
二、工作中可能会用到
备份表,备份数据,备份数据库,还原数据库
1.备份表,创建一个表与某个表相同 create table +表1 like +表2 2.备份数据,把一个表的数据插入到另一个表 insert into +表名 select * from +表名 留意点:插入的表必须要存在 3.把一个表的某些字段插入到一个新表中 insert into +表1(字段1,字段2) select 字段1,字段2 from 表2 留意点 1. 插入的表必须存在 2. 插入的表是新表,没有数据。 4.备份数据库 mysqldump -uroot -p 数据库名 >脚本名 5.还原数据库 mysql -uroot -p +数据库 <脚本名 1)备份表结构
案例:
create table hh like student ;
https://i-blog.csdnimg.cn/img_convert/e35120d8e1069eafb3de972b5a936970.png
(2)备份表数据
a、备份部分数据
INSERT into mm(id,age) select id,age from student
https://i-blog.csdnimg.cn/img_convert/47ddd8ca59b474b43505555eb64c5447.png
b、备份全部数据
INSERT into mm select * from student
https://i-blog.csdnimg.cn/img_convert/e8b59da46a57f09b788ec292d555afeb.png
(3)备份报表结构和数据
create table nn as (select * from student)
https://i-blog.csdnimg.cn/img_convert/104759a5736a8d99469ef1ebb5022542.png
在linux中备份:
(1)备份数据
语句:
mysqldump -u root -p hh>/home/hz56.sql
https://i-blog.csdnimg.cn/img_convert/162297ff2f24ba017aa8e4d558936e8e.png
https://i-blog.csdnimg.cn/img_convert/cec6e08286ff965d59b3460a88c0e537.png
(2)还原数据
a、新建一个数据库
https://i-blog.csdnimg.cn/img_convert/bf0ab3c2355d823adfa675ea7a05cbe1.png
b、还原数据
mysql -u root -p kk</home/hz56.sql
https://i-blog.csdnimg.cn/img_convert/fecbbc2b35ce8e75a716119babb5259b.png
三、修改表数据 update set
语法:update +表名 set 修改后的内容 where + 条件 案例:把student表中英语分数小于60的同砚分数改为60分 update student set english=60 where english<60;
四、单表查询
1、查询表的全部内容
select * from +表名
https://i-blog.csdnimg.cn/direct/a5c46b3d46804a0ebe67370d8b2c2ab1.png
2、查询部分字段信息数据
格式:select 字段1,字段2 from 表名;
https://i-blog.csdnimg.cn/direct/e5f28e106d7e4b55bd9f7f7fbdadbf04.png
3、查询字段可以用as 取别名
格式:
select 字段1 as "别名名称",字段2 " 别名名称2" from 表名 ;
备注:as也可以省略不写
案例:
select name as "姓名",math " 数学" from student2 ;
https://i-blog.csdnimg.cn/img_convert/a0558ea41747a480d9edc5651f8ff2c9.png
4、where接条件指定查询内容
where +条件
条件:比较运算符
>,<,=,!=,<>,>=,<=
案例:
(1)=即是
select * from student2 where id=2;
(2)!=不即是
select * from student2 where id!=2;
(3)<>不即是
select * from student2 where id<>2;
(4)大于
select * from student2 where id>2;
(5)小于
select * from student2 where id<2;
(6)大于即是
select * from student2 where id>=2;
(7)小于即是
select * from student2 where id<=2;
https://i-blog.csdnimg.cn/img_convert/e514ac6e403610853b99978bd557e6e3.png
(8)and 同时满意
select * from student2 where id>3 and math>90;
https://i-blog.csdnimg.cn/img_convert/e206e21e5cb00c3c85eb8e7e5c9cc801.png
(9)or 满意此中一个条件就能查询出数据
select * from student2 where id>6 or math>97;
(10)beteen ... and ... 在什么范围之间
select * from student2 where id BETWEEN 2 and 5;
https://i-blog.csdnimg.cn/img_convert/ef19dd7a1d03a0172737423e4a3f9483.png
(11)in 在一组数据中匹配
select * from student2 where id in (2,8,6)
https://i-blog.csdnimg.cn/img_convert/0affa7ddb72f969c85fc5d79f1414c6f.png
(12)not in 不在一组数据中匹配
select * from student2 where id not in (2,8,6)
https://i-blog.csdnimg.cn/img_convert/b4fc4b6e27710ba786a711181d452f41.png
(13)is null
select * from student2 where class is null
https://i-blog.csdnimg.cn/img_convert/cfd30e58adf5bcf38f05a417cb3adbc5.png
(14)is not null
案例:select * from student2 where class is not null
https://i-blog.csdnimg.cn/img_convert/18f0efc918f6ba5b5a04925ab9ee8c08.png
5、排序:order by
(1)升序:asc 可以省略不写
select * from student2 order by math asc ;
(2) 降序:desc 降序
select * from student2 order by math desc ;
https://i-blog.csdnimg.cn/img_convert/1178b51ad3077a372fd5605085ab06b5.png
(3)二次排序
select * from student2 order by math desc ,age desc;
https://i-blog.csdnimg.cn/img_convert/93b4ae1b7f6a3cebb14d1b62b79ba269.png
6、group by 分组
select class,name from student group by class-----------查询每个班级的门生姓名和班级
留意:
group by 后面接的分组条件应该与查询内容有相对于的
grout by 后面接条件可以用having,不能用where
where后面可以接group by
例如:查询每个班级的年龄小于17岁的门生姓名和班级
select class,name from student group by class having age<17;
7、like 模糊查询
%:%是通配符
_:代表一个字符
(1)查找8开头的数据
select * from student where english like "8%"
(2)查找包罗8的数据
select * from student2 where math like "%8%"
(3)查找8结尾的数据
select * from student2 where math like "%8"
(4)查看指定字符的数值
select * from student where english like "8___"
https://i-blog.csdnimg.cn/img_convert/fb379dd435c92925126a58e79f7c298b.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]