zoukankan      html  css  js  c++  java
  • SQL常用语句

    -SQL常用命令
    --数据的增删改查
      --增加数据(插入数据)
       insert into [表名]([字段],[字段]..) values(值,值,值..) --按需要字段填写
       insert into [表名] values(值,值,值..) --插入全部字段,自动增长列不写

      --删除数据(记得有外键先删除主键表里的相应内容)
       --删除表里的内容
        delete from [表名]
       --要是想删某一行就加where条件
        delete from [表名] where [字段]=值

      --修改数据(更新数据)
       --修改整个表内的数据
        update [表名] set [字段]=值
       --要是想修改某一行就加where条件
        update [表名] set [字段1]=值1 where [字段2]=值2

      --查询
        select [字段],[字段] from [表名]
       --条件查询
        select [字段],[字段] from [表名] where [字段]=值

       --升降序(一般加在查询语句末尾)
       --升序(默认)
        order by [字段] asc
       --降序
        order by [字段] desc
       --例子
        select [字段],[字段] from [表名] order by [字段] desc


    --设置使用的数据库
    use [数据库名]

    --建立命令
     --建立数据库
       create database [数据库名]
       on
       (--数据库信息
        NAME='aaa', --逻辑名称aaa
        FILENAME='d:bb.mdf', --物理名称bbb(写全路径)
        SIZE=5mb, --数据库初始大小
        MAXSIZE=100mb, --增长的最大值
        FILEGROWTH=15%, --增长率
       )
       log on
       (--日志信息
        NAME='aaa', --逻辑名称aaa
        FILENAME='d:bb.ldf', --物理名称bbb(写全路径)
        SIZE=2mb, --数据库初始大小
        FILEGROWTH=1mb, --增长率
       )
       
     --建立表
       create table [表名]
       (
        --[字段] [数据类型] [列的特征],
        id int identity(1,1) not null,--identity(1,1) 是自动增长(起始值,递增值) ,not null 是不许为空(默认允许为空)
        name varchar(20) not null,
        )
     --给表添加约束
       alter table [表名]
       add constraint [约束名]
      --添加主键(pk) PriMary key([字段名])
        --唯一约束(uq) Unique ([字段名])
        --默认约束(df) default('默认文字') for [字段名]
        --检查约束(ck) check([字段名] between 15 and 40) --填写的数字在15-40之间
        --外键约束(fk) foreign key([字段名]) references [表名]([字段名])


    --删除数据库
     --exists检查是否存在
       if exists(select * from sysdatabases where name = '[要删除的数据库名]')
       drop database [要删除的数据库名]

       if exists(select * from sysobjects where name = '[要删除的储存过程名]')
       drop database [要删除的储存过程名]

    --变量的使用 (声明和使用要一起进行不然会找不到变量)
      declare @[变量名] [数据类型]
      --如 declare @name varchar(8)
       declare @age int

     --变量赋值
      set @name =值
      select @name =值

      --例子
        --查询信息 查找张三的信息
         declare @name varchar(8)
         set @name = '张三'
         select * from [表名] where [字段] = @name
     
        --赋值查询 查询与张三同龄的人
         declare @name varchar(8)
         declare @age int
         set @name = '张三'
         select @age = [字段] from [表名] where [字段]=@name
         select * from [表名] where [字段]=@age


     --全局变量 (两个@)
      @@error  最后一个T-SQL错误的错误号
      @@identity 最后一次插入的标识值
      @@rowcount 上一个sql语句影响行数


    --事务
     --开始
      begin transaction
     --提交
      commit transaction
     --回滚
      rollback transaction

     --例子
       begin transaction
       declare @errorsum int --纪录错误
       @errorsum = 0    --初始化没有错误
       --SQL语句
       set @errorsum=@errorsum+@@error --累计是否出错
       --SQL语句
       set @errorsum=@errorsum+@@error
       --SQL语句
       set @errorsum=@errorsum+@@error
       if @errorsum <>0 --如果有错
         begin
         rollback transaction
         end
       else
         begin
         commit transaction
         end
       GO

    --储存过程
     --建立
       create procedure [储存过程名]
        as
       declare @xx int ,
       declare @yy varchar(8) output (带有output为输出参数,否则视为输入参数)
       --SQL语句
       --SQL语句
       --SQL语句
       select @yy=值 ...
        GO
     --调用储存过程
       --无参数
        exec [储存过程名]  
       --有参数(顺序不能变,变量在储存过程内部的值为默认值)
        exec [储存过程名] @xx=[参数],@yy=[参数]
       --有输出参
        declare @yy varchar(8)
        exec [储存过程名] 值,@yy output

  • 相关阅读:
    Spring boot unable to determine jdbc url from datasouce
    Unable to create initial connections of pool. spring boot mysql
    spring boot MySQL Public Key Retrieval is not allowed
    spring boot no identifier specified for entity
    Establishing SSL connection without server's identity verification is not recommended
    eclipse unable to start within 45 seconds
    Oracle 数据库,远程访问 ora-12541:TNS:无监听程序
    macOS 下安装tomcat
    在macOS 上添加 JAVA_HOME 环境变量
    Maven2: Missing artifact but jars are in place
  • 原文地址:https://www.cnblogs.com/oo_o/p/5529852.html
Copyright © 2011-2022 走看看