mysql语句二(alter table 表内容修改、备份数据库、修改表数据updata set、单表查询select)

一、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___"

图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值