zoukankan      html  css  js  c++  java
  • Sql Server学习笔记

    1、指定路径创建数据库

    create database student
    on--创建库的时候必须写
    (
    name=student,
    filename='E:databasestudent.mdf'--关于存放路径问题与本计算机的系统有关,最好创建在文件夹下

    2、彻底删除数据库

    drop database student

    3、创建表

    --identity 自动增长列

    --primary key 主建

    use student
    go
    create table stuinfo
    (
    stuId int identity primary key,
    stuName nvarchar(20) not null,
    stuSex nvarchar(1) not null,
    stuAge int not null 
    )

    4、添加数据

    insert into --添加
    --stuinfo 表格
    --values(添加的内容)
    --添加的内容规则:内容数量与表结构列名一致,顺序一致
    --添加的内容:特别的地方、自动增长列除外
    --insert into stuinfo values(添加内容)
    insert into stuinfo values('sss','',22)
    insert into stuinfo
    select 'aa','','23' union
    select 'aa','','23' union
    select 'aa','','23' union
    select 'aa','','23' 

    5、查询

    select * from stuinfo

    6、删除表里面的内容(保存表的结构)

    delete stuinfo --删除表的所有数据
    delete stuinfo where stuAge>18 --删除年龄大于18岁的学生
    truncate table stuinfo --当数据库清空时,才有必要,把自动增长列归0

    7、修改表里面的内容

    update stuinfo set  Stuage=15 where  Stuage>18 --将学生年龄大于18的改为15岁

    8、查询前5的数据

    select top 5 stuName as '前5名的学生' from stuinfo

    9、查询该表前50%的数据

    select  top 50 percent * from stuinfo

    10、查询该表ID 2-5之间的数据

    select * from stuinfo stuID between 2 and 5

    11、查询姓名中有”李“字的学生

    select * from stuinfo where stuName like '%李%'

     12、删除某个字段为空的所在行

     delete  [SS_OLMS_SZ].[dbo].[SZ_SCADAPoint] where 序号 is  NULL

     13、将一个表的字段值对应字段插入到另一个表中

    INSERT INTO SS_MonitorStation(M_ID,M_Name,GISLongitude,GISLatitude,M_StationNo,M_Description,M_StationTypeID)
    SELECT 序号,测点名称1,[北坐标(X)],[东坐标(Y)],数据,备注,TypeID FROM SZ_SCADAPoint

     14、修改一个表的字段值,通过另一个表对应的字段值一一对应修改

    update a set a.TagName = b.TagName from b where a.DataID=b.DataID

     15、循环插入语句(便于测试)

    declare @num int
     set @num =1
     while(@num<5)
     begin 
     set @num = @num+1
      insert student (name,age,address) values('a',@num,'b')
     end

     16、删除数据库里面的所有的表

    use student
    GO
    declare @sql varchar(8000)
    while (select count(*) from sysobjects where type='U')>0
    begin SELECT @sql='drop table ' + name FROM sysobjects
    WHERE (type = 'U') ORDER BY 'drop table ' + name exec(@sql)
    end

     17、按时间(单位为年),分组计数

    select DATEPART(YEAR,Birthday),COUNT(*)FROM stuinfo  GROUP BY DATEPART(YEAR,Birthday)

    查询效果:

    18、修改列名

    sp_rename '表名.旧列名','新列名','column'

     19、删除有规律的表

    declare @i int
    declare @s nvarchar(100)
    set @i=129
    while @i<143
    begin
    Set @s='drop table dbo.Initial_'+cast(@i as varchar)
    print @s
    exec(@s)
    set @i=@i+1
    end

    执行效果如下:

     20、从已有的表创建一个不存在的表

    select * into 新表 from 旧表
  • 相关阅读:
    poj 3243 Clever Y(BabyStep GiantStep)
    poj 2417 Discrete Logging
    poj 3481 Double Queue
    hdu 4046 Panda
    hdu 2896 病毒侵袭
    poj 1442 Black Box
    hdu 2815 Mod Tree
    hdu 3065 病毒侵袭持续中
    hdu 1576 A/B
    所有控件
  • 原文地址:https://www.cnblogs.com/GIScore/p/5008632.html
Copyright © 2011-2022 走看看