zoukankan      html  css  js  c++  java
  • SQL总结

    创建数据库

    --创建MySchool数据库

    create database MySchool

    on

    (

    name='MySchool_data',                    --主数据文件的逻辑名称

    filename='E:projectMySchool_data.mdf', --主数据文件的物理名称

    size=5mb,                             --主数据文件的初始大小(最小为3mb)

    maxsize=10mb,                            --主数据文件增长的最大值

    filegrowth=10%                           --主数据文件增长率

    )

    log on    --日志文件

    (

    name='MySchool_log',

    filename='E:projectMySchool_log.ldf',

    size=5mb,

    filegrowth=10%

    )

    创建表

    --创建学生表并创建约束

    create table Student

    (

    StuNo int identity(1,1) primary key(StuNo), --identity:自增列primary Key:主键约束

    StuName nvarchar(20)  unique(StuName),      --unique:唯一约束

    Birthday datetime check(Birthday>GetDate()),   --check:检查约束

    Address nvarchar(20)

    )

    --默认约束

    alter table Student

    add constraint df_Address default ('不确定') for Address  --default for:默认约束

    --外键约束

    Alter  table Result

    Add constraint fk_StuNo foreign key(StuNo) reference Student(StuNo)

    --foreign key reference: 外键约束

     

    --删除数据库

    drop database 数据库名

    --删除表

    drop database 表名0

    --检测是否存在School数据库

    use master

    if exists(select *from sysdatabases where name='School')

    --检测是否存在Student表

    use MySchool

    if exists(select *from sysobjects where name='Student')

    --删除约束

    alter table 表名

    drop constraint 约束名

    子查询

    --利用子查询查询科目为JAVA并且成绩为90分的学生

    --利用子查询查询科目为JAVA并且成绩为90分的学生

    select StuName from Student where StuNo in

    (

    select StuNo from Result 

    inner join Subject on Result.StuNo=Subject.SubjectNo

    where Grade=90 and SubjectName='JAVA'

    )

     

     

    --表连接

    --利用表连接查询科目为JAVA并且成绩为90分的学生

    select StuName from Student

    inner join Result on Student.StuNo=Result.StuNo

    inner join Subject  on Result.StuNo=Subject.SubjectNo

    where Grade=90 and SubjectName='JAVA'

     

    局部变量

    --利用局部变量查询张三相邻二位同学的信息

    declare @name nvarchar(20)

    set @name='张三'

    select StuNo,StuName ,Birthday,Address from Student

    where StuName=@name

    declare @No int

    select @No = StuNo from Student

    where StuName=@name

    select StuNo,StuName ,Birthday,Address from Student

    where (StuNo=@No+1) or (StuNo=@No-1)

     

    --增加
    insert into 表名[列名] values (值)

    --一次插入多条数据
    1)通过union关键字合并数据插入
    insert 表名(列名)
    select 值1,值2,值3 union
    select 值1,值2,值3
    当列名为字符串类型,日期类型时,插入的数据用单引号(')引起来


    2)通过insert select 语句向表中添加数据,此表是提前创建好的
    insert into 新表名(预先创建的)
    select 列名
    from 旧表名


    3)通过select into语句将现有表中的数据添加到新表中
    select 旧表名.列名1,旧表名,列名2
    into 新表名(预先不存在)
    from 旧表名


    --删除
    delete [from] 表名 [where<删除条件>] --只删除表中的数据,结构不变,返回受影响的行数
    truncate table 表名 --删除所有(包括结构)
    --更改
    update 表名 set 列名=更新值 [where更新条件]

    --查询
    1)查询前几条记录
    select top 数字/百分数 *from表名 order by desc/asc --desc 从大到小 asc 从小到大(默认值)
    select top 数字 列名1,列名2 from 表名
    2)查询在一定范围内的:
    select *from where 条件
    select *from where 列名 between 值1 and 值2(包括临界值)
    3)查询空值与非空值
    select *from 表名 where 列名 is null --查询出空的
    select *from 表名 where liem is not null --查询出非空的
    4)模糊查询
    select *from 表名 where 列名 like 条件

     

    --聚合函数
    sum()函数返回表达式中所有数值的总和(只能用于数字类型的列) 空值将被忽略
    select sum(列名) as 别名 from 表名 where条件
    avg()函数返回表达式中所有数值的平均值
    select avg(列名) from 表名 where条件
    max()函数返回最大值,min()函数返回最小值(可以是数字型,字符型和日期裂类型的列)
    select max(列名) as 别名 ,min(列名) as 别名 from 表名 from where条件
    count()函数返回提供的组或记录集中地计数(可以使用星号(*)作为count的表达式不指特定的列计算所有的行数(包括空值行))

    select count(列名/*) as 别名 from 表名

  • 相关阅读:
    【Thymeleaf/SpringBoot/MyBatis】如何将themeleaf页面的表单数据提交到后台处理
    【Oracle】生成连续序列的两种方法
    【oracle】统计字段里逗号的数量,采用函数regexp(field,',')
    人的成长与发展不会一帆风顺,坚守理想和信念,不以一时颠簸为意,才能更好地走向未来。
    【SpringBoot/Thymeleaf】如何使SpringBoot程序显示网页
    【jQuery/Thymeleaf】在Thymeleaf页面用jQuery的Ajax方法取得后台数据显示在页面上
    JavaScript常用正則表達式
    CSS的入門
    ASP操作xml之如何生成XML数据
    ISAPI筛选器及对ASP源文件的保护
  • 原文地址:https://www.cnblogs.com/sujulin/p/6565577.html
Copyright © 2011-2022 走看看