zoukankan      html  css  js  c++  java
  • SQl Server T-sql语句学习

    T-sql语句就是通过代码来代替鼠标完成一些操作,使用起来要比鼠标方便很多。

    创建数据库   careate  database  +数据库名。 数据库名不能为中文,不能以数字开头。

    use  数据库名   选择数据库。  创建表之前要先选择要建表的数据库,不然表就建到别的库去了

    careate table  +名称    创建表,在表中创建列要用小括号,并用逗号隔开每个列。

    create database ninini
    use ninini
    create table student
    (
    ID int identity(1,1), --该列自动增长,由1开始每次增加是1。
    Code nvarchar(50)primary key, --主键约束
    Name nvarchar(50)unique not null, --唯一键约束   不许为空
    Sex bit,
    Nation nvarchar(50),
    Score decimal(5,2), --最大5位数,2位小数。
    Birthday datetime
    
    )

    删除表: drop table 表名称    drop 就是删除之类的意思

       
    添加列:alter table 表名称 add 列名称  数据类型     alter table  修改表的意思,add就是添加


    删除列:altet table 表名称 drop  column  列名称     column  列的意思

    添加数据:

    insert into student values('s001','张三',1,'null',99.99,'1996.3.6')   添加字符串用单引号括起来,用逗号隔开

    修改数据:update 表名称  set 列表名=要改的数据

    条件修改:update  表名称 set 列名称 = 值  where 列 = 值  (where理解为当的意思,当xxxx的时候,把表的列改为 值)

    删除数据:delete from 表名(逐行删除,速度慢)    /    truncate table 表名(直接清除,更为彻底,并且不可恢复

    条件删除: delete form 表明 where 列= 值    (同上)

    查询数据:select *from 表名

    +条件查询
       把* 改为要查询的列名,多个列的话可以用逗号,然后再 from 表名 
       如过有查询的范围限制,可以在后面用 where 然后 列名  ><=比较符  有多个条件的可以用and 或者or 来连接
    +模糊查询
         select *from 表 where  列名 like '%值%'
    +排序查询
        select  *from 表 where reader by  列  asc/desc 
    +去重查询
       select  dectinct  列 *from 表
    +分组查询
       select 列名 from 表名 group by 对应的列名    group by就是分组的意思

      group by 之后要添加条件的话 用 having 不用 where。
    +子查询

    把一条查询语句,当做值来使用
    子句的查询结果必须是一列
    子句可以返回多行数据,但必须是一列

    外键约束

    alter table 外键表名 add constraint 约束名称 foreign key(外键字段) references 主键表名(约束列名)

    如表A中的Ids是主键,要约束表B中的Aid列,那么语句应该是:

    alter table B add constraint A_B_Ids foreign key(Aid) references A(Ids)

  • 相关阅读:
    selenium
    python第三方模块的安装
    程序员学习网站
    python 数据较大 性能分析
    linux ~/ 和 /
    VMWare虚拟机 window文件传递
    vi命令
    os.system
    win10系统进入BIOS
    pyinstaller将python脚本生成exe
  • 原文地址:https://www.cnblogs.com/big-lll/p/6530352.html
Copyright © 2011-2022 走看看