Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38510
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 282
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-27 16:04
文章分类
文章存档

2014年(21)

我的朋友

分类: SQLServer

2014-01-27 16:11:18

写在前面,本文学习的语句都只是针对数据的增删改查,不涉及对数据库及表本身结构等参数的操作哦;

 

一、增

1.1 insert into
    语法:insert into 库名.表名(列名) values("列值","列值","……");
    insert into mydatabase.mytable (name,phone) values ("张三","18659212120");

 

1.2 insert into + select
    语法:insert into 已有的新表名(列名) select 列名 from 原表名;
    insert into mynewtable (name) select name from mytable;

 

1.3 select + into
    语法:select 新列名 into 新表名 from 原表名;
    select phone into myintotable from mytable;

注:MYSQL 不支持该语句;可用以下语句代替:

    create table myintotable(select phone from mytable);

 

1.4 insert into + select + union + select
    语法:insert into 表名(列名) select "列值" union select "列值";
    insert into myintotable (phone) select "12345678900" union select "1345678901";
    注:union 表示下一行;

 

二、删

2.1 delete

    语法:delete from 表名 where 列名="列值";

    delete from myintotable where phone="1345678901";

 

2.2 truncate

    语法:truncate table 表名;

    truncate table myintotable;

注:清除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用于有外键约束引用的表;

 

三、改

3.1 update

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

    update mytable set phone="18759212120" where name="张三";

 

四、查

4.1 select + from

    语法:select 列名 from 表名;

    select * from mytable;

    select name from mytable;

 

4.2 select + from + where

    语法:select 列名 from 表名 where 查询条件;

    select name from mytable where phone="18659212120";

 

    select * from mydatabase.mytable where phone is NULL;

    注:把电话号码这列为空的全部查出来;

 

4.3  select + from + limit

    语法:select 列名 from 表名 limit 行数;

    select * from mytable limit 2;

    注:只查示前两行数据;

 

4.4 select + from + order by

    语法:select 列名 from 表名 order by 列名

    select * from scoretable order by score;

    注:针对分数列进行排序,默认为asc升序,末尾加 "desc" 参数更改为降序;

 

4.5 select + from + where + like 

    语法:select 列名 from 表名 where 列名 like 列值

    select * from mytable where name like "李%";

    注:把name列 "李" 字打头的全部查出来;

 

4.6 select + from + where + between

    语法:select 列名 from 表名 where 列名 between 列值

    select * from scoretable where score between 75 and 89;

    注:把分数在75至89之间的数据全部查出来;

阅读(227) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~