zoukankan      html  css  js  c++  java
  • 数据库子函数等


     create table yyy
     (
     code int ,
     name varchar(50),
     sex varchar(50),
     age int,
     hight decimal(18,1),
     weight decimal(18,1),
     idno bigint,
     address varchar(50)
     )
     insert into yyy values(1,'张三','男',20,161,61,37030219,'山东')
     insert into yyy values(2,'王八乐四','男',21,162,62,37030219,'淄博')
     insert into yyy values(6,'李科','男',22,178,75,371521,'淄博')
     insert into yyy values(3,'王五','男',22,163,63,37030219,'济南')
     insert into yyy values(4,'李科','男',23,164,64,37030219,'潍坊')
     insert into yyy values(5,'李科','男',24,165,65,37030219,'临沂')
     select *from yyy
     
     --top 关键字
     select top 3 *from yyy --显示整个表的前3行
     select top 3 *from yyy where age>=22  --先找到age>=22的 然后显示age>=22的前3行
     select top 3 name from yyy --显示这个表的前3行中的name一列
     
     --distinct
     select  distinct name from  yyy   --去除名字相同的
     
     --order  by 排序
     select *from yyy order by age asc  --age从小到大排序  asc 升序
     select *from yyy order by age desc --age从大到小排序  desc 降序
     select *from yyy where weight<70 order by weight --先筛选weight<70的 然后在按weight从小到大排序
     select *from yyy order by age,weight --先按年龄排序,然后在不改变第一次排序的基础上按体重排序
     
     
    --分组  group by 列   对哪一列进行分组,就只能显示哪一列
    select age from yyy group by age  --对name列分组,只能显示name列的信息
    select age from yyy group by age

    --查询年龄加5岁大于27的
    select *from yyy where age+5>27

    --算术运算符:+-*/%
    --比较运算符:> < >= <= != <>(不等于)   !<大于等于 !>小于等于
    --逻辑运算符 and or
    --修饰符 all any some in not

    --in 在什么参数范围之内
    select *from yyy where age in(22,23)  --相当于age=22 or age=23
    select *from yyy where age=22 or age=23 --既显示age=22,也显示age=23

    --not 不在什么参数范围之内
    select *from yyy where age not in(22,23)  --不显示age是22和23的

    --查询年龄不在身高是164的人的年龄范围之内的学生信息
    select *from yyy where hight=164
    select *from yyy where age !=23
    --子查询:使用查询语句查询一列数据出来,然后作为其他查询的查询条件中的参数来使用
    --查询身高不在年龄是22岁的人的身高范围之内的信息
    select *from yyy where age =22
    select *from yyy where hight not in(178,163)
    --简化
    select *from yyy where hight not in(select hight from yyy where age =22)--先查询age=22的所有人的体重的这一列数据的结果作为参数,然后将这列参数用于第一个函数

     select *from yyy
    --查询名字叫李科的人中年龄比code=1的张三那个人的年龄大3岁的人的信息
    select *from yyy where  age - (select age from yyy where code=1 and name='张三' )=3 and name='李科'

    --查询名字叫李科的人中年龄比姓王的人中年龄大1岁的人的信息
    select *from yyy where name ='李科' and age -1 in(select age from yyy where name like '王%')

    --外键 :受约束的表叫外键表,约束的数据源叫主键表
    --要想加外键,首先得有主键表
    --要想删主键表数据,必须先删除外键表数据
    --作为外键的数据源的列,必须要是一个唯一键(这一列必须是主键或者是unique)

    create table teacher
    (
     tcode int primary key identity(1,1) ,
     tname varchar(50)
    )
    insert into teacher(tname) values('张三')
    select *from teacher
    create table student
    (
     scode int primary key identity(1,1) ,
     sname varchar(50),
     tno int references teacher(tcode)--student表的tno项参考teacher表的tcode项,tcode项必须是主键项
     cid varchar(20) unique    --唯一列,不能重  unique
    )
    insert into student values ('学生1',null,'32134124')   --tno项只能输入null或张三的编号
    insert into student values ('学生2',null,'321434124')
    insert into student values ('学生3',null,'32153124')
    insert into student values ('学生4',1,'3215g124')
    select *from student

  • 相关阅读:
    励志
    哈佛大学用这12张图,教育了10亿人
    5分钟弄懂Docker!
    第四节:Web爬虫之pyquery解析库
    第十节:numpy之数组文件操作
    第三节:Web爬虫之BeautifulSoup解析库
    国嵌内核驱动进阶班-7-1(Ioctl设备控制)
    国嵌内核驱动进阶班-6-4 竞争与互斥(2)
    国嵌内核驱动进阶班-6-4 竞争与互斥
    C++类对象的内存结构
  • 原文地址:https://www.cnblogs.com/lk-kk/p/4442797.html
Copyright © 2011-2022 走看看