设为首页 加入收藏

TOP

SQL增删改查(详细)
2015-11-21 01:35:42 来源: 作者: 【 】 浏览:0
Tags:SQL 删改 详细

一、增:有3种方法

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

   insert [into] <表名> [列名] values <列值>

  insert into Strdents (name,age) values ('atm',12)

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

 insert into <已有的新表> <列名> select <原表列名> from <原表名>   insert into newtable (name,class)select name,class from tableinfo

3.将数据插入原表中(生成测试数据用的较多)

 和第二种方法一样,只是复制到原表中 
 insert into tableinfo ('name','class')select name,class from tableinfo

二、删:有3中方法

1.delete删除

 delete from <表名> [where <删除条件>]   delete from tableinfo where name='atm'

2.truncate table 删除整个表的数据

 truncate table <表名>   truncate table tableinfo   删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用于有外建约束引用的表

3、drop删除

 drop table <表名> drop table tableinfo 删除表中所有行,表结构也删除了。

三、update更新修改

 update <表名> set <列名=更新值> [where <更新条件>] update tableinfo set age=12 where name='atm1'    set后面可以紧随多个数据列的更新值(非数字要引号);

四、查

1.普通查询

 select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]  1).查询所有数据     select * from tableinfo    2).查询部分行列--条件查询     select name,age from tableinfo where age=11;    3).在查询中使用AS更改列名     select name as 姓名 from a where age=11;    4).查询空行     select name from tableinf where class is null    5).查询返回限制行数(关键字:top )     select top 6 name from tableinfo     显示列name的前6行,oracle 中用rownum替代(select * from a where rownum<6 )         6).查询排序(关键字:order by , asc , desc)     例:select name from tableinfo where age>=11 order by desc(默认为ASC升序)

2.模糊查询

   1).使用like进行模糊查询
请看另一篇文章, SQL like四种用法
    
   2).使用between在某个范围内进行查询

select * from tableinfo where age between 11 and 22

  
   3).使用in在列举值内进行查询(in后是多个的数据)

select name from tableinfo where name in ('atm','atm1','atm2');
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇利用PL/SQLDevoloper备份与恢复Or.. 下一篇辛星浅析postgresql中的全文索引(..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: