zoukankan      html  css  js  c++  java
  • sql server数据库操作

     1 --插入整行数据
     2 insert into [dbo].[person] values('Hasaki', 35, '1983-08-29', 'A', 'A', 'A')
     3 
     4 --插入部分列数据
     5 insert into [dbo].[person] (name, age, birth) values('Hasaki', 35, '1983-08-29')
     6 
     7 --删除行记录
     8 delete from person where name = 'John'
     9 
    10 --更新数据库
    11 update person set study = 'A' where name = 'Amy'
    12 
    13 --去除重复行
    14 select distinct name from [dbo].[person] 
    15 
    16 --TOP 选择前n条记录
    17 select top 1 * from [dbo].[person]
    18 
    19 --TOP 选择前百分n条记录
    20 select top 50 percent * from [dbo].[person]
    21 
    22 --LIKE 通配符'_'匹配一个任意字符
    23 select * from [dbo].[person] where name like 'T_m'
    24 
    25 --LIKE 通配符'%'匹配0个或多个任意字符
    26 select * from [dbo].[person] where name like '%Tom'
    27 
    28 --LIKE 通配符'[]'匹配括号里任一字符
    29 select * from [dbo].[person] where name like '[TJA]%'
    30 
    31 --LIKE 通配符'[^]'匹配不在括号里的任一字符
    32 select * from [dbo].[person] where name like '[^TJA]%'

    连接

      连接操作是是通过笛卡尔积运算进行的。例如下面两个分别有三条记录的表student和course连接时,实际就是两两匹配,产生9条记录。

    name age
    Hasaki 14
    Noki 16
    Yummy 15
    course name
    chinese Hasaki
    english Yummy
    math Kilo

      

    笛卡尔积结果为:

    name age name course
    Hasaki 14 Hasaki chinese
    Hasaki 14 Yummy english
    Hasaki 14 Kilo math
    Noki 16 Hasaki chinese
    Noki 16 Yummy english
    Noki 16 Kilo math
    Yummy 15 Hasaki chinese
    Yummy 15 Yummy english
    Yummy 15 Kilo math

      数据库的连接操作结果是笛卡尔积运算后经过筛选剩余的部分。

    --内连接
    select * from Course join person on person.name = course.name

      提取 course.name 和 person.name 相等的记录,结果:

    name age name course
    Hasaki 14 Hasaki chinese
    Yummy 15 Yummy english
    --左连接
    select * from course left join person on person.name = course.name

      提取 course.name(左表) 和 person.name(右表) 相等的记录,而且对于左表的每一条记录,在结果数据集中最少出现一次。

    对于左表中一条记录,如果在右表不存在匹配记录,那么结果集中右表部分的数据为NULL,结果:

    name course name age
    Hasaki chinese Hasaki 14
    Yummy english Yummy 15
    Kilo math NULL NULL
    --右连接
    select * from Course right join person on Course.pName = person.name

      情况和左连接相反,结果:

    name course name age
    Hasaki chinese Hasaki 14
    Yummy english Yummy 15
    NULL NULL Noki 16

        

    --全连接
    select * from Course full join person on Course.pName = person.name

      提取 course.name(左表) 和 person.name(右表) 相等的记录,而且对于左表、右表的每一条记录,在结果数据集中最少出现一次。对于左表中一条记录,如果在右表不存在匹配记录,那么结果集中右表部分的数据为NULL。对于右表中一条记录,如果在左表不存在匹配记录,那么结果集中左表部分的数据为NULL。结果:

    name course name age
    Hasaki chinese Hasaki 14
    Yummy english Yummy 15
    NULL NULL Noki 16
    Kilo math NULL NULL
    生活不止眼前的苟且 还有诗和远方的田野
  • 相关阅读:
    初賽
    SA
    高斯-约旦消元法
    AC自动机
    KMP
    关于scanf
    网络流
    常用SQL语句
    Java开发中的23种设计模式详解(转)
    generatorConfig.xml
  • 原文地址:https://www.cnblogs.com/jc-nogame/p/6065496.html
Copyright © 2011-2022 走看看