一、对表字段进行操作
1.新建表
create table d3( id int(10) PRIMARY key , name varchar(20) UNIQUE,
age int(10) DEFAULT 18, fs int(20) not null
)DEFAULT charset=utf8;
2.add 添加字段
格式:ALTER table 表名 add 字段名 字符类型(字符长度);
案例:ALTER table d3 add sex int(10);
3.change 修改字段
格式:
ALTER table 表名 CHANGE 旧字段 新字段 字符类型(字符长度);
案例:ALTER table d3 CHANGE sex incoming int(10);
4.dorp 删除字段
格式:
ALTER table 表名 drop 字段名;
案例:
ALTER table d3 drop fs;
5.rename 修改表名
格式:ALTER table 源表名 rename 新表名
案例:ALTER table d3 rename dd
6、modify after 字段调用
格式:
alter table 表名 MODIFY 字段名 字符类型(字符长度) after 指定字段;
案例:
alter table dd MODIFY age int(10) after id;
查看字符长度和字符类型:desc查看
7.first 添加字段到第一位
格式:
alter table 表名 add 字段名 字符类型(字符长度) first ;
案例:
alter table dd add no int(10) first ;
====================
查:select
1、查询所有的内容
(1)查询所有的数 *
格式 :select * from 表名 ;
案例:select * from student2;
(2)查询部分数据结条件 where +比较运算符
格式:select * from 表名where 条件;
案例:
select * from student2 where id=2 ;
比较运算符号:>,<,=,>=,<= ,!=,<>
案例:
select * from student2 where id=2 ;
select * from student2 where id!=2 ;
select * from student2 where id<>2 ;
select * from student2 where id>2 ;
select * from student2 where id<2 ;
select * from student2 where id<=2 ;
select * from student2 where id>=2 ;
and 同时满足
案例:
select * from student2 where id>2 and math>90;
截图:
or 只要满足一个条件
案例:select * from student2 where id>5 or math>90;
between ... and 在什么之间
包含开始值和结束值
案例:
select * from student2 where id BETWEEN 1 and 3 ;
in 在一组数据中
案例:
select * from student2 where id in(1,3,9)
not in 不在一组数据中
案例:select * from student2 where id not in(1,3,9)
is null 为空
案例:
select * from student2 where class is null;
is not null 不为空
案例:select * from student2 where class is not null;
(3) 查询指定字段
格式:select 字段1,字段2 ,字段 3 from student2 ;
案例:select name ,math from student2 ;
(4)查询的字段可以取别名
as 取别名
案例:
select name as "姓名" ,math as "数学" from student2 ;
5、order by 排序
升序: order by asc 省略不写
降序:order by desc
案例1:升序
select * from student2 order by math asc ;
案例2:降序
select * from student2 order by id desc ;
案例3 :二次排序
案例:select * from student2 order by math desc,chinese desc ;
6.like 模糊查询
% :表示匹配一个字符或多个字符
_: 表示匹配一个字符
案例:
select * from student2 where math like "9%"
select * from student2 where math like "%3"
select * from student2 where math like "%9%"
select * from student2 where math like "9_"
7.limit (索引位,步长) 显示指定的行数
select * from student2 limit 0,2; # 索引0,第一行 2:取两行
select * from student2 limit 4,3; # 索引4,第5行,3是取3行
8、聚合函数
max 最大值
min 最小值
count 统计个数
sum 求和
avg 平均值
distinct 去重
案例:
9.group by 分组
案例:
select class ,sum(age) from student2 group by class
having 接条件
案例:
select class ,sum(age)as s from student2 group by class HAVING s>60;
改:
update .....set .....
格式:
案例:
UPDATE student2 set name="aa" where id=1
删除:
drop 删除表
案例:drop table student2;
delete from ...删除表数据
个数:DELETE from 表名 where 条件;
案例:DELETE from student2 where id=1 ;
truncate 表名 快速删除
案例:TRUNCATE student2; 删除所有的数据
drop >truncate>delete
=========================
单行注释:ctrl +/
取消注释:shift+ctrl+/
多行注释:选中多行 ,ctrl +/
取消注释:选中多行 shift+ctrl+/
=========================
备份:
1、备份表结构
案例:
create table aa like student2 ;
2.备份表数据
先有一个表结构:
案例:
INSERT into aa select * from student2 ;
(3)备份部分数据
案例:INSERT into bb(id,name) select id,name from student2 ;
(4)备份表结构和数据
案例:
create table cc as (select * from student2 )
============================
在linux 中备份和还原
linux备份:
格式:mysqldump -u root -p 已经存在的库>/路径/新建的sql文件
案例:mysqldump -u root -p sjk>/home/dcs59.sql
linux还原
1.新建库
2.还原数据
案例:mysql -u root -p kk</home/dcs59.sql