zoukankan      html  css  js  c++  java
  • SQL代码整理

    --SQL代码整理:

    create database mingzi--创建数据库
    go--连接符(可省略)
    create table biao--创建表
    (
    lieming1 int not null,--定义列名
    lieming2 varchar(20),
    lieming3 int primary key identity(1,1)
    --设置主键,并且从1开始自动添加
    lieming4 varchar(20) references 表名(列名)
    --设置外键
    lieming5 decimal(18,2) check(lieming5>=0 and
    leiming5<=10)
    --设置约束
    )
    insert into biao values(1,'ee')--给表添加数据

    select *from biao--查询表

    drop table biao--删除表

    drop database mingzi--删除数据库

    update biao set lieming2='ed' where lieming1=1
    --更改表内的数据
    delete from biao where lieming='ee'
    --删除表内的指定数据
    alter table 表名 add 列名 varchar(5) unique
    --添加表中列的属性;unique是唯一性
    alter table 表名 drop column(列的意思) 列名
    --删除表中的一列


    select *from 表名--查询表中所有数据
    select '123'+'234'--字符串可以的连接
    select top 2 *from 表名--top筛选表中的前2条

    where确定查询条件:
    select *from 表名 where 列名=‘’
    --like模糊查询
    select *from 表名 where 列名 like '%宝马%'
    --含有“宝马”的数据
    select *from 表名 where 列名 like '宝马__'
    --含有“宝马后有两个字符的”
    select *from 表名 where 列名 like '宝马[1-9]_'
    --宝马后字符必须是1-9之间
    select *from 表名 where 列名 like '宝马[^1-6]_'
    --宝马后字符必须不在1-6之间
    where后面可以跟筛选
    --比较运算符 > < >= <= !=
    --逻辑运算符 and or

    select distinct 列名 from 表名
    --去除重复

    select *from 表名 order by 列名 asc
    --按照顺序排列(asc升序默认;desc降序)


    聚合函数:只能返回一个值;
    min;max;sum;count;avg
    例子:
    select min(oil),max(oil),sum(price)from car
    由于count是给表中的列数统计
    select count(*)from car


    日期时间函数:
    select sysdatetime()--获取系统时间
    print sysdatetime()--获取系统时间(在消息框显示)
    select getdate()--获取现在时间
    select year(getdate())--获取当前时间的年份
    select datename(year,'2014-3-3')--获取日期的年份(返回
    字符串类型)
    select datepart(yeat,'2014-4-3')--获取日期的年份(返回
    的是int类型)
    select dateadd(day,50,getdate())--增加日期50天

    字符串函数:
    select STR(123.45,3,1)--第一个是数值数据,3是长度,1是小
    数点后几位;并且是四舍五入;
    select replace('asdfgheas','asd','xxx')--替换
    select LEFT('asddgh',3)--返回从左边开始指定长度的字符
    select RIGHT('asdfgh',3)--返回从右边开始指定长度的字符
    select SUBSTRING('asdfdg',3,2)--截取字符串,索引从1开始
    select LEN('asdfghj')--返回字符串长度
    select REVERSE('asdfgh')--翻转字符串
    select UPPER('asdf')--大写
    select LOWER('ASDFG')--小写
    select LTRIM(' asd ')--去除作空格
    select RTRIM(' eere ')---去除有空格


    类型转换函数:
    select cast('123' as int)
    select convert(int,'123')


    --数学函数:
    select abs(-2.1)--绝对值(负数变正数);
    select rand()--随机数(返回一个介于0-1之间的伪随机数)
    select round(4.1,0)--四舍五入(0代表小数点后位数)
    select floor(5.56)--下限
    select ceiling(5.2)--上限(返回大于或等于制定数值表达式的
    最小整数)
    select pi()--圆周率
    select sqrt(4)--开根号
    select square(5)--平方


    合并数据集:将两个或多个查询结果合成一个结果集
    union:
    中的所有选择列表必须具有相同的列数,相似的数据类型和相同的
    顺序;
    结果集的列名来自第一个select 语句


    相关子查询:
    select *from score as a where degree<
    (select avg(degree)from score as b where b.cno=a.cno)
    --从自己的表中查询;where b.cno=a.cno类似分组了;

  • 相关阅读:
    day20(顺时针打印矩阵)
    day18(树的子结构)
    JAVA WEB应用
    hexo 写作
    解决
    Github构建个人主页之写作
    Github构建个人主页之建站
    hive HQL数据库操作笔记02
    python scrapy爬虫笔记01
    spark笔记01
  • 原文地址:https://www.cnblogs.com/ziyandeyanhuo/p/4874760.html
Copyright © 2011-2022 走看看