sql总结

1.创建数据库
    --exec xp_cmdshell 'mkdir d:/project'--调用DOS命令创建文件夹,使用此句需要启动SQL的外围工具
    if exists(select * from sysdatabases where name='数据库名')
     drop database 数据库名
    set nocount on   --取消受影响行数
    create database 数据库名
    on
    (
     name='',
     filename='',--  .mdf .ldf
     size=10mb,
     maxsize=
     filename=    --数据库大小增长量
    )
    log on
    (
    )
    go
    use 数据库名
--创建登录用户
exec sp_addlogin '用户名','密码'

--去掉受影响行数
set nocount on

exec sp_grantdbaccess 'adminMaster','adminMaster'
--设置用户对表的权限
grant select,delete,update on bbstopic to 用户名


2.创建表及添加约束
    create table 表
    (
    )

    alter table 表 add
    constraint 约束名 primary key(列名)  --主键约束  identity
    constraint 约束名 check(len(列名))  --check约束
    constraint 约束名 unique(列名) --唯一约束
    constraint 约束名 default(默认内容) for 列名
    constraint 约束名 foreign key(外键名)
    references 主键数据库名(主键名)
    ck:check约束
    fk:外键约束
    df:default默认
  注:alter 改变, 更改
    add 加入, 增加, 添加
    constraint 强制,限制, 约束
    foreign key 外键
    references 引用

          

3.sql中的关键字:
    sysdatabases 总库
    exists  判断是否存在
    create  创建
    drop  删除
    default   默认
    constraint  约束
    proc  存储过程
    view 试图
    trigger  触发器     

4.sql中的特殊符号
    like 与字符串进行等值比较
    [0-9] 0到9之间
    or 或者
    and 和
    in 多者之间
    = 等于、!=和<>都是不等于,可以用于varchar或int之间的等值判断
5.sql中的变量
    declare @a 变量类型,@b 类型 定义变量
    print 打印、输出
    convert(转换为的类型,将转的值)
    cast(将转的值,转换为的类型)
6.sql中的系统定义
    @@identity 上一句sql语句自增长的值
    @@rowcount 上一句sql语句受影响行数
    @@error 出错行数
7.事务
    begin tran 开始事务
    commit tran 提交事务
    rollback tran 回滚事务
8.存储过程
    if exists(select * from sysobjects where name='存储过程名')
     drop proc 存储过程名
    go
    create proc 存储过程名
    @name 数据类型(多个之间用,分割) 参数列表 输出参数后加out
    as
    begin
     sql语句
    end
    go
    exec 存储过程名 传入参数


9.有关时间的操作
    getdate() 获取当前时间
    datepart(yy/mm/dd,要提取的时间)   --提取时间(比较时用in)
    datediff(yy/mm/dd(相差类别),时间1,时间2)    ---时间1与时间2相差
    print datepart(dw,getdate())-1---找出当前星期
10.随机数
    select @r=rand((datepart(mm,getdate())*100000)+(datepart(ss,getdate())*1000)+datepart(ms,getdate()))
11.截取字符串   
    substring(要提取的目标字符串,开始位,截取个数)
12.sql中的逻辑语句
    while(条件)
    if(条件)
    else
 case
 when bool then 值
 when ...  then 值
 else 值
 end
13.索引
    -------------给transinfo的cardid添加索引
    if exists(select * from sysindexes where name='index_cardid')
     drop index transinfo.index_cardid
    go
    create nonclustered index index_cardid
     on transinfo(cardid)
     with fillfactor=30
    go
14.试图
    create view 试图名
    as
    试图所要显示的内容
    select * from 试图名  ---调用试图    
15.基本语句
    insert into 表名[列名] values()
    delete 列名 from 表名 [where 条件]
    update 列名=值 set 表名 [where 条件]
    select [列名] from 表名 [where 条件]
    select [列名] into 新表名 from 表名
    (根据索引查找)
    select * from 表名 with(index=索引名) where 索引满足条件 条件
16.其它
    表、存储过程、试图等在sql中的父类都是sysobjects
    数据库的父类是sysdatabases
    索引的父类是sysindexes
    begin end 相当一对大括号
    distinct去掉重复
17.
    declare @id int
    select @id=uid from bbsusers where uname='455142'
    print @id
    if (@id is null)
    print '---------'
    else
    print '55555555555555'

    变量没有查找到付给它的值时,默认为null
18.分页的sql语句
    select top 每页显示的行数 * from 表  where id not in(select top 当前页起始索引 id from 表)
    
19.
    char() 的最大字符是4000
    varchar() 的最大字符是8000
20.用SqlParameter模糊查询时(sql中字符串也可以用+进行拼接)
 sql="select * from 表名 where uName like '%'+@name+'%'";

 like '%/%%' escape'/' 转义符(表示“/”后的第一个字符是一个字符串)
 也可用 like '%[%]%' 其中[]中表示

 

 

21.sql中的函数
 avg() 求平均值
 sum() 求和
 count(列) 总个数
 max(列)  最大值
 min(列)  最小值
 len(字符串) 字符串的长度

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值