详解SQL语句实现增、删、改、查

一、增(两种方法)

1、使用 insert 插入单行数据:

语法:insert  into  表名  (列名)  values  (列值)

例:insert  into Students  (姓名,性别,学号)  value ('Bruce','男','14')

(将Bruce,男,14插入到表Students中)

2、使用 insert,select 语句将 原有表中的数据 添加到 已有新表 

语法:insert  into  已有新表名  列名  select  原表列名  from  原表名

例:insert  into  AddressList  ('姓名','地址','电话')  select  name, address,  phoneNum  from Students

(将Students表中的name,address,phoneNum插入到AddressList表中的姓名,地址,电话中)

二、删(两种方法)

1、使用 delete 删除数据

语法:delete  from  表名  where  删除条件

例:delete  from  Students  where  number='14'

(删除Students表中学号为14的列)

2、使用 truncate table 删除整张表的数据

语法:truncate  table 表名

例:truncate  table Students

(删除Students表)

三、改

语法:update  表名  set  列名=更新值  where  更新条件

例:update  Students  set  age  =  25  where  name  =  'Bruce'

(将Studets表中姓名为Bruce的学生年龄改为25)

四、查(* 表示查询所有字段)

1、常规查询

语法:select  列名  from  表名  where  查询条件  order  by  排序列名  asc或desc

(1) 根据表的 行/列 查询

语法:select  *  from  表名  where  列/行值

例:select  *  from  Students  where  name  =  'Bruce'

(查询Students表中姓名为Bruce的行/列)

(2) 查询部分行/列

语法:select  所需要的列  from  表名  where  查询条件

例:select  i,j,k  from  Students  where  f=5

(查询Students表中所有行,并显示i,j,k 这三列)

(3)再查询中使用as更改列名

语法:select  原表中列名  as  改后的列名  from  表名  where  查询条件

例:select  name  as  姓名  from  Students  gender = '男'

(查询Students表中性别为男的所有行,显示Name列,并将name改为“姓名”显示)

(4)查询空行(SQL语句中is null 或is not null来判断是否为空)

语法:select  要显示的列  from  表名  where  为空的字段  is  null

例:select  name  from  Students  where  number  is  null

(查询Students表中number为空的所有行,并显示name列)

(5)在查询中添加常量

语法: select  name  '北京'  as  地址  from  Students

(查询Students表中,显示name列,并添加列值均为“北京”的地址列)

(6)查询返回限制行数(oracle中没有top关键字用rownum代替)

语法:select  top  限制的行数  显示的列名  from  表名

例:select  top  5  name  from  Students 

(查询Students中name的前5列)

(7)排序查询(desc是降序   ascs是升序)

语法:select  显示的列名  from  表名  where  查询条件  order  by  desc

例:select  name  from  Students  where  grade  >= 60  order  by  desc

(查询表中成绩大于60的所有行,并按降序显示name列)

2、模糊查询

(1)使用like进行模糊查询(like只用于字符串)

语法:select  *  from  表名  where  查询的列名  like  模糊的字符串

例:select  *  from  Students  where  name  like '张%@'

(查询Students表中name列中姓张的记录)

(2)使用between在某个范围内查找

语法:select  *  from  表名  where  查询的列名  between  数字  and  数字

例:select  *  from  Students  where  age  between  18  and  20

(查询Students表中年龄在18~20 之间的记录)

(3)使用in在列举值内进行查询

语法:select  要显示的字段名  from  表名  where  列名  in  查询的值

例:select  name  frome  Students  where  address  in  '北京'

(查询Students表中地址为北京的记录,并显示姓名)

3、多表连接查询

语法:select  表名1.要显示的表1关联的字段,表名2.要显示的表2中的字段  from  表名1,表名2  where  表名1.与表名2相等的字段  =  表名2.与表名1相等的字段

例:select  a.name  , b.age  from  a, b  where  a.name  =  b.name 

(查询表a和表b中name相等的记录,并显示a表中name字段和b表中age字段)

4、分组查询

(1)使用  group by  进行分组查询

例:select studentID as 学员编号, AVG(score) as 平均成绩  (注释:这里的score是列名)from score (注释:这里的score是表名) group by studentID

(2)使用 having 子句进行分组筛选

例:select  studentID as 学员编号, AVG from score  group  by  studentID having  count (score)>1

说明:对于“分组查询”小编还不是特别理解,先暂时放在这里,等以后用得到的时候在整理!

再此感谢技术支持:在此感谢技术支持

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cna...
    最美的太阳WW阅读 3,240评论 0 23
  • 一、基础 1、创建数据库(create) CREATE DATABASE database-namecreate ...
    YBshone阅读 654评论 0 1
  • 什么是SQL数据库: SQL是Structured Query Language(结构化查询语言)的缩写。SQL是...
    西贝巴巴阅读 1,862评论 0 10
  • day5,我家有两米,所以,幸福的时刻是每天抽时间给她们读不同的书。小米今天听的是《猴子捞月亮》和《三只小猪》,哈...
    悦米时光阅读 204评论 0 0
  • 小时候,总是把喜欢吃的东西留在最后,现在总是把喜欢的第一口就要尝到。大概是害怕拥有的会失去,失去的不会再来。 总以...
    ending鱼子酱阅读 135评论 0 0