• 20141030--SQL2008常用命令-1


     1 create database biao2--创建新的数据库
     2 go 
     3 use biao2
     4 go
     5 create table shuiguo--创建表shuiguo ,create table创建表
     6 (
     7  序号 int,
     8  名字 varchar(10),--()小括号内是类型的字符长度
     9  价格 decimal(18,2)
    10 )
    11 use biao2
    12 go
    13 select *from shuiguo --查询语句, select查询
    14 insert into shuiguo values (1,'苹果','1.0')--insert into 插入数据
    15 insert into shuiguo values (2,'橘子','5.0')--values值 
    16 insert into shuiguo values (3,'香蕉','3.0') 
    17 
    18 use shujuku--这是使用指定的数据库的操作
    19 go
    20 --修改表,新加入列,不可添加数据不能为空的列。
    21 --修改表 alter table ,add添加列,drop column 删除列
    22 alter table 表1 add [int] varchar(10)--添加列,add 列与关键字相同时用中括号[]包裹
    23 alter table 表1 add fenshu varchar(10)
    24 alter table 表1 add nianling varchar(10)
    25 alter table 表1 drop column [int]--删除列,drop . column是约束条件
    26 alter table 表1 alter column fenshu int--修改列的类型
    27 --改 更新 update
    28 update 表1 set fenshu=80,nianling=19 where cood=2--更改 表1 中列fenshu=80,和列nianling=18 在列cood=1(where之后是定位)。值用表达式=连接
    29 update 表1 set fenshu=80,nianling=18 --没有条件where  将修改所有的
    30 
    31 sp_renamedb biao2,shujuku01 --修改数据库的名字 旧名,新名
    32 sp_rename biao1,表1--修改表的名字  旧名,新名
    33 --查询语句  select 查询什么 from 从  from后跟 表名字 -where 在哪
    34 select *from 表1 --查询语句
    35 select 姓名 from biao1--查询一列
    36 select 姓名,性别 from 表1--查询两列
    37 select *from 表1 where cood=2--查询条件
    38 select 姓名,性别 from 表1 where cood=3--按条件查询两列
    39 select distinct 姓名 from 表1--查询时自动去重复得,并不删除,针对一列去重显示
    40 select *from 表1 where 姓名='李四' and nianling=18--查询 列姓名='李四'并且nianling=18的所有数据
    41 select *from 表1 where 姓名='李四' or nianling=18--查询 列姓名='李四'并且nianling=18的所有数据
    42 select *from 表1 where nianling in (18,20)--查询列nianling的值是18,20所有数据
    43 --between 在...之间
    44 select *from 表1 where nianling between 18 and 20--查询列nianling的值是18--20所有数据
    45 select *from 表1 where 姓名 not in ('李四','赵六')--查询 列姓名 的值不是'李四','赵六' 的所有数据
    46 --模糊查询 
    47 --like 像,
    48 select *from 表1 where 姓名 like '%四%'--百分号代表可以为任何字符,可为多个,名称为通配符
    49 select *from 表1 where 姓名 like '张_'--下划线代表任意一个字符,
    50 select *from 表1 where 姓名 like '_[李四,赵六]'--引号里面括号外加下划线,意思为[]内任意的一个值
    51 --order 顺序
    52 select *from 表1 order by nianling asc--按照年龄排序,升序,asc可不写,默认为升序
    53 select *from 表1 order by nianling desc--按年龄排序,降序,desc为后缀
    54 --top 顶端
    55 select top 3 *from 表1 order by nianling --查找按照nianling排序之后开头的三个的所有的数据
    56 select top 3 *from 表1 order by fenshu desc --查询按照fenshu降序排列的前三名的所有数据
    57 select *from 表1 where 姓名='李四' order by fenshu desc--查询 姓名 值为李四的所有数据并按照分数降序排列
    58 select top 1 *from 表1 where 姓名='李四' order by fenshu desc--查询姓名为李四的所有数据,按照分数降序排列并选出第一名
    59 --插入数据 insert into  
    60 insert into 表1 values(5,'张三丰','1989-1-2','',175,70,0,0,0);--插入数据
    61 insert into 表1 values(3,'王五','1989-1-2','',168,65,0);--插入数据
    62 insert into 表1(cood,姓名,性别) values(4,'马六','')--按字段插入数据
    63 update 表1 set 出生日期='1990-3-3'where cood=3--修改表,按照条件修改,没有条件修改所有的
    64 --删除 
    65 -- delete 删除表的内容(结构在),会产生日志,新写入的数据将会继续按照原有的序号往下增加
    66 -- truncate 将表清空,不产生日志,写入时将从头开始写入
    67 delete from 表1--删除,从表1删除
    68 delete from 表1 where cood=4--按条件删除,只要符合条件的都删除
    69 --多条件时 可以添加or或  and和 between在..之间  in(符合括号内的值得选项,隔开)
    70 delete from 表1 where cood=4 or 姓名=5--删除 列cood的值是4或 列姓名的值是5的
    71 delete from 表1 where cood=4 and 姓名=5--删除 列cood的值是4并且 列姓名的值是5的
    72 delete from 表1 where cood between 3 and 5--删除 列cood的值是3--5之间的
    73 delete from 表1 where cood in (1,3)--删除 列cood的值是1或3的
  • 相关阅读:
    547. Friend Circles
    399. Evaluate Division
    684. Redundant Connection
    327. Count of Range Sum
    LeetCode 130 被围绕的区域
    LeetCode 696 计数二进制子串
    LeetCode 116 填充每个节点的下一个右侧节点
    LeetCode 101 对称二叉树
    LeetCode 111 二叉树最小深度
    LeetCode 59 螺旋矩阵II
  • 原文地址:https://www.cnblogs.com/Tirisfal/p/4063057.html
Copyright © 2020-2023  润新知