找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

MySQL数据库基础指令2(C++规则)

[复制链接]
增删改查
删除主键是必须先删除自身带有的其余约束条件
1.     删除自增长约束:
Alter table test modify id int( 10);
2.     删除主键:
Alter table test drop primary key;
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg
3.     增加主键:通过修改字段属性的方法增加
Alter table test modify id int(10) primary key;
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg
Drop table 表名:删除表
Drop table 表路径(库.表名):通过绝对路径的方式删除表
Alter table 表路径(库.表名):通过绝对路径的方式对其他库的表进行操作
Alter table:主要用于改变表结构
Insert into:MySQL中进行数据插入
Insert into 表名(表内字段名若干,表名用逗号分隔) values(数据,对应表内字段的存储数据类型及顺序)(可进行数据的单独插入)
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image006.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image008.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image010.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image012.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image014.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image016.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image018.jpg
Select *from 表名:MySQL表中进行数据查询
插入数据的三种方式:
1.     插入数据时,完整的说明表中字段,且值字段须于表字段顺序一一对应
2.     插入数据时,单独对某一个字段单独插入时,值的位置和说明字段位置一一对应
3.     插入数据时,不说明表中字段情况,默认插入表中所有字段的数据,要求对表内容
非常熟悉
插入数据时可以插入多行值
注:若数据类型为字符串类型,数据需要使用单/双引号(char、varchar)
    Date日期写入也需要使用单/双引号
非空约束中,类型字符串的字段默认值为‘ ’,类型为数字的字段默认值为0
与其他没有非空约束的字段对比,其他字段的null是不占空间的,但非空约束
字段的值,默认是占一个字节0的控件
删除表数据:delete from 表名 where 条件
Delete from 表名where 对应字段的详细数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image020.jpg
多条件删除:
Delete from 表:表内数据完全清除
Delete from 表名where 对应字段 = 详细数据 and 另一字段= 详细数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image022.jpg
更新(修改)表数据:
Update 表 set 字段= 新数据:所有字段内容都被修改更新
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image024.jpg
Update 表 set 字段= 新数据 + where 条件:具体数据内容的修改
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image026.jpg
删除表数据的三种方式:
1.     delete from 表:删除表中所有数据,但不释放空间,自增长在原来最大值基础上+1
2.     truncate 表名:删除表中所有数据,且释放空间,自增长从头开始
3.     trop table 表:删除表
查询数据:
Select *from 表名:查询表内所有数据
Select *from 表名where 条件:查询表内满足条件的数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image028.jpg
Select 字段名 from 表名:查询表内关于字段的所有数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image030.jpg
Select 字段名 from 表名 where 条件:查询表内满足条件时字段内的数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image032.jpg
Select 字段名 from 表名 where 条件 and 条件:查询表内满足条件时字段内的数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image034.jpg
Select 字段名 from 表名 where 条件 or 条件:查询表内满足条件时字段内的数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image036.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image038.jpg
注:不能对同一条件进行and条件并列查询
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image040.jpg
Select 字段名 from 表名 where 除此条件:排除条件的字段数据查询
<>、!=:不等于
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image042.jpg
Select *from 表名where 条件:查询表内满足条件的数据
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image044.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image046.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image048.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image050.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image052.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image054.jpg
区间表示:>=78.65 and <= 90
          Between 78.65 and 90(闭区间,即【78.65,90】)
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image056.jpg
select *from test1 where id in (1,6,10);即:id满足于1,6,10时
等同于
Select *from test 1 where id = 1 or id = 6or id = 10;
select *from test1 where id not in(1,6,10);即:id满足于不是1,6,10时
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image058.jpg
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image060.jpg
Like:模糊匹配
Select *from 表名 wherelike “%搜索字符%”;(%:匹配0个或多个字符,与Linux中的*相像
搜寻字符中带有特定字符的数据段
Select *from 表名 wherelike “搜索字符%”;
搜寻以特定字符开头的数据段
Select *from 表名 wherelike “%搜索字符”;
搜寻末尾处以特定字符收尾的数据段
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image062.jpg
Limit m,n:查询限定的数量的数据,由m+1开始后n行
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image064.jpg
数据的排序:
Order by:默认为升序排序,即从小到大
Select *from 表名 orderby score;
同等于:
Select *from 表名 orderby 字段名 asc;
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image066.jpg
Desc:降序,即从大到小
Select *from 表名order by 字段名 desc;
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image068.jpg
Select *from test1 where score is not nullorder by score desc limit 0,2;
先进行筛选,显示score不为null的数据,进行降序排列,并且只显示前两行
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image070.jpg
分组显示:
Group by:查询的数据根据某个字段进行分组,一般显示该字段的第一个数据(实际
          为随机匹配)
MySQL中允许where条件过滤之后,在进行分组,反之则不可
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image072.jpg
若想在分组之后进行条件过滤,则需要使用having
select * from +表名group by 字段 having 条件
having跟在group by后面,作用相当于where
注:having后可接聚合函数,而where后不可
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image074.jpg
面试题:统计每个班级成绩合格的人数
1. 先过滤出成绩合格的数据
2. 对结果进行分组
3. 统计分组后的人数情况
注:只能先过滤,再分组(过滤条件在前,分组在后)
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image076.jpg
聚合函数:
Count():统计函数
Select count(*) from 表名:统计表中数据有多少行
等同于:
Select count(1) from 表名
Select count(字段名)from 表名:统计表中字段内的有效数据数量
select count(字段名)from 表名 where 条件:先进行条件筛选,再进行字段统计
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image078.jpg
Sum():求和函数
Select sum(字段)from 表:表内字段求和
Select 字段1,sum(字段)from 表 group by字段1:以字段1进行分组并求和
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image080.jpg
Avg():求平均数
Select avg(字段)from 表:求表内字段的平均值
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image082.jpg
Max():求最大值
Select max(字段)from 表:求表内字段的最大值
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image084.jpg
Min():求最小值
Select min(字段)from 表:求表内字段的最小值
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image086.jpg
Distinct():去重函数
Select distinct(字段)from表:表内字段中进行去重操作
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image088.jpg
MySQL中可进行四则运算:
file:///C:/Users/1/AppData/Local/Temp/msohtmlclip1/01/clip_image090.jpg
select max(score),min(score),max(score)+min(score)as value from test1;
select max(score),min(score),max(score)-min(score)as value from test1;
select max(score),min(score),(max(score)-min(score))*2as value from test1;
select max(score),min(score),(max(score)-min(score))/2as value from test1;
数据库备份的两种方式:
1.     在数据库中进行备份
1.     创建一个备份表,表结构与需要备份的表一致
create table +新表 like +旧表
2.     往新表插入旧表的所有数据
insert into +新表 select * from +旧表
3.     把旧表的某些字段插入进新表中
insert into +新表(字段1,字段2) select 字段1,字段2 from 旧表
2.     在数据库以外进行备份(Linux中进行备份)
1.     通过MySQL数据库的备份指令进行数据库的备份
Mysqldump -uroot -p 要备份的数据库 > 备份的路径并命名(.sql)
2.     回复还原备份
首先于数据库新建一个库,用于存放还原的数据
Create database 新库名
Mysql -uroot -p 新库名 < 对应路径的库备份文件

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册