首页 > 代码库 > 【6.1】SQL基础----2 DML语句【数据操纵语句】

【6.1】SQL基础----2 DML语句【数据操纵语句】

2 DML语句【数据操纵语句】

2.1 插入数据

1.为表的所有字段插入数据:insert into 表名 values("值1","值2","值3","值4");  或者  insert into 表名(字段名1,字段名2,字段名3,字段名4) values("值1","值2","值3","值4");

备注:其中values()括号里的值用英文的单引号‘ ‘ 或者 英文的双引号" "都是一样的。

2.为指定字段插入数据:insert into 表名(字段1,字段3) values("值1","值3");

3.同时插入多条数据:insert into 表名(字段名1,字段名2,字段名3,字段名4) values("值1","值2","值3","值4"),("值1","值2","值3","值4");

          或者  (省略字段名)

          insert into 表名 values("值1","值2","值3","值4"),("值1","值2","值3","值4");

例子:INSERT INTO student VALUES("4","女","4444"),("3","女","3333");

技术分享

 

 

 

2.2 修改数据

update 表名 set 字段名1 = 取值1,字段名2 = 取值2,... ,字段名n = 取值n  where 条件表达式;

 

2.3 删除数据

1.使用delete删除数据: delete from 表名 [where 条件表达式];  (说明:如果没有where 条件表达式,将删除表中的所有数据)

2.使用truncate清空表数据:truncate table 表名;

3.truncate,delete,drop的区别:truncate速度比delete快并且会释放空间但两者都不删除表定义,drop删除表定义+释放空间【参考博文,感谢博主】:http://blog.csdn.net/ws0513/article/details/49980547

 

2.4 表单查询

2.4.1 简单查询

  1. 查询所有字段:select * from 表名;
  2. 查询指定字段:select 字段1,字段3 from 表名;
  3. 避免重复查询:select distinct 字段名... from 表名;
  4. 为表和字段取别名:select 字段名 as 别名 from 表名 as 别名;

 

2.4.2 条件查询

  • 技术分享
  1. 带关系运算符和逻辑运算符的查询:  例子:select * from student where  sno>3; 
  2. 带in关键字的查询:  例子:select * from student where sno in(1,3,6);
  3. 带between and 关键字的查询:  例子1:select * from student where sno between 3 and 5;   <<>>  例子2:select * from student where sno not between 5 and 6;
  4. 带is null 关键字的空值查询:  例子:select * from student where zno is null;
  5. 带like [not like] 关键字的查询:  例子1:select * from student where zno like ‘6%‘;     <<>>  例子2:select * from student where zno like ‘666_‘; [其中‘%‘代表任意长度的字符,‘_‘代表单一字符]   

 


 

 

2.4.3 高级查询

  1. 分组查询【group by关键字 和 having关键字】:having和where都是用来限制显示的,但having只用在分组group by关键字之后,用于选择满足having条件的组。
  • 例子:select ssex,count(ssex) from student group by ssex; 结果如下:
  • 技术分享
  • 例子:select ssex,count(ssex) from student group by ssex having count(ssex) > 3; 结果如下:
  •  技术分享

     

  1. null;
  2. 对查询结构排序:  例子:select * from student order by sno;
  3. 限制查询数量:  例子:select * from student order by sno limit 2,3;[查找student表中从第3名同学开始的3位同学的信息,select查询返回的结果集的下标是从0开始的]
  4. 集合函数:  例子: select count(sno) from student;

 

 

【6.1】SQL基础----2 DML语句【数据操纵语句】