一、alter table + 表名 --------对表内容修改
1、添加表字段: add
alter table +表名 add 字段名 字符范例(字符长度);
例如:alter table student add dcs int(10);
2、删除表字段:drop
语法格式:alter table + 表名 drop + 字段名
alter table student drop hzdcs;
3、修改表字段:change
语法格式:alter table + 表名 change 原字段名 新字段名 + 字符范例(字符范例长度)
alter table student change dcs hzdcs int(10);
4、修改表名字:rename
语法格式:alter table +表名 rename 新表名
alter table student rename student_2;
5、调解表字段顺序:modify
语法:alter table +表名 modify +原字段名 + 字符范例(字符范例长度) after 表中字段名
alter table student modify math int(10) after name -----------把student表中的math字段放在name字段后面
6、 添加新字段到表的第一位:first
alter table +表名 add 新字段名 字符范例(字符范例长度) first;
alter table student add no int(10) first;
二、工作中可能会用到
备份表,备份数据,备份数据库,还原数据库
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 ;
(2)备份表数据
a、备份部分数据
INSERT into mm(id,age) select id,age from student
b、备份全部数据
INSERT into mm select * from student
(3)备份报表结构和数据
create table nn as (select * from student)
在linux中备份:
(1)备份数据
语句:
mysqldump -u root -p hh>/home/hz56.sql
(2)还原数据
a、新建一个数据库
b、还原数据
mysql -u root -p kk</home/hz56.sql
三、修改表数据 update set
语法:update +表名 set 修改后的内容 where + 条件 案例:把student表中英语分数小于60的同砚分数改为60分 update student set english=60 where english<60;
四、单表查询
1、查询表的全部内容
select * from +表名
2、查询部分字段信息数据
格式:select 字段1,字段2 from 表名;
3、查询字段可以用as 取别名
格式:
select 字段1 as "别名名称",字段2 " 别名名称2" from 表名 ;
备注:as也可以省略不写
案例:
select name as "姓名",math " 数学" from student2 ;
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;
(8)and 同时满意
select * from student2 where id>3 and math>90;
(9)or 满意此中一个条件就能查询出数据
select * from student2 where id>6 or math>97;
(10)beteen ... and ... 在什么范围之间
select * from student2 where id BETWEEN 2 and 5;
(11)in 在一组数据中匹配
select * from student2 where id in (2,8,6)
(12)not in 不在一组数据中匹配
select * from student2 where id not in (2,8,6)
(13)is null
select * from student2 where class is null
(14)is not null
案例:select * from student2 where class is not null
5、排序:order by
(1)升序:asc 可以省略不写
select * from student2 order by math asc ;
(2) 降序:desc 降序
select * from student2 order by math desc ;
(3)二次排序
select * from student2 order by math desc ,age desc;
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___"
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |