zoukankan      html  css  js  c++  java
  • SQL查询学习

    create database seldata
    on
    (
    name=seldata,
    filename='d:dataseldata.mdf',
    size=100MB,
    maxsize=2000mb,
    filegrowth=5%
    )
    log on
    (
    name=seldata_log,
    filename='d:dataseldata.ldf',
    size=50MB,
    maxsize=1000MB,
    filegrowth=5%
    )
    --创建查询数据库
    use seldata
    create table teacher
    (
    tno int not null,
    tname char(60) not null,
    cno int not null,
    sal int ,
    dname char(100) not null,
    sex char(2) not null default '男',
    age int not null age
    )
    select * from teacher
    alter table teacher
    add constraint ck_age check(age between 1 and 200)
    --为age字段创建录入值约束
    alter table teacher
    drop constraint ck_age
    --删除约束
    insert into teacher values (2,'李彤',5,1200,'生物','女',54)
    insert into teacher values (3,'王水军',5,900,'计算机','男',40)
    insert into teacher values (4,'刘小静',2,1200,'计算机','女',46)
    insert into teacher values (5,'高伟',8,2100,'电子工程','男',39)
    insert into teacher values (6,'李伟',7,1200,'机械工程','男',29)
    insert into teacher values (7,'刘辉',3,900,'生物','女',46)
    insert into teacher (tno,tname,cno,dname,sex,age)
    values (8,'李伟',9,'计算机','女',43)
    insert into teacher values (9,'刘静',12,1300,'经济管理','女',28)
    insert into teacher (tno,tname,cno,dname,sex,age)
    values (10,'刘一凯',13,'计算机','女',33)
    select * from teacher
    --添加数据表
    select * from teacher where 1>2
    --查询数据表结构可以采用条件表达式不成立的方式
    select tname from teacher
    --单列查询根据列字段查询
    select tno,tname from teacher
    select distinct dname from teacher
    --加入distinct去除重复值 select disctinct 去除重复值字段 from 表 +条件
    select * from teacher where 1=2
    select tno,tname,sal,sex,age from teacher
    --多列查询加入列名以逗号分隔
    select distinct tname,sal,age from teacher
    --注意:distinct 后接多列字段时,需满足多列均相同才会去除重
    select tname,tno,sal from teacher order by tname
    -单列排序
    select tname,tno,sal from teacher order by sal desc
    --注意:null值为最小值
    select tname,sal,sex,age from teacher order by sex,age
    --多列排序,先按sex进行排序,有相同记录后再按age进行排序
    select tname,sal,sex,age from teacher order by 3,4
    --采用序号进行排序,这里的3 和4 分别代表sex第三旬,4为第四列
    select tname,sal,sex,age from teacher order by sex,age desc
    --对指定序号做反向排序:age desc 而前一列sex依然是正向排序
    select tname,sal,sex,age,dname from teacher where dname='计算机'
    select top 1* from teacher
    create view cp_view
    as select tname,sal,sex,age,dname from teacher where dname='计算机'
    select * from cp_view
    drop view cp_view
    --单条件查询
    --下面是比较运算
    select tname,dname,age,sex from teacher where age>=40 order by age desc
    --这是对数字进行比较运算
    --同样也可以对字符和时间等进行比较运逄
    select tname,dname,sal,age,sex from teacher
    where dname like '计%' order by dname,sex
    select * from teacher where sex<>'男' order by age
    select * from teacher where sex!='男' order by age
    select tname,dname,sal,age,sex from teacher
    where sal <>1200 order by sal
    /*注意:在值为null参与比较运算时得到的结果为false 则结果不会表现在查询
    结果集中*/
    --关于Between 运算范围
    select tname,dname,age,sex from teacher
    where age between 30 and 50 order by age
    --在between 之间的值结果包括等于范围集。
    select tname,dname,age,sex from teacher
    where dname between '计算机' and '生物'
    --关于null值判断
    select tname,sal,age,sex from teacher
    where sal is null order by sal

    select tname,sal,age,sex from teacher
    where sal is not null order by sal
    --注意:null值不能参与运算,只能表达为is null 或者is not null

  • 相关阅读:
    FC/NES 音乐示例程序 生日快乐歌
    FC/NES PPU卷轴滚屏示例 歌词显示
    HZK16汉字16*16点阵字库的使用及示例程序
    6502 通用宏汇编器宏汇编伪指令说明
    "在您可以登录前, 此版本的 Windows 必须被 Microsoft 激活. 你想现在激活它吗?" 在 XP SP3 中的一种解决方案
    使用 WM_COPYDATA 在进程间共享数据
    51驱动1602液晶显示器的程序
    汉字 全角字符 特殊关键字 ASCII编码区位
    actionscript(as) 项目中 使用 fla 加载 fla
    未能加载文件或程序集“WebGrease, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
  • 原文地址:https://www.cnblogs.com/ccnlc/p/7561332.html
Copyright © 2011-2022 走看看