zoukankan      html  css  js  c++  java
  • SQL小知识

    1、char/nchar, varchar/nvarchar

        char(10)只能放五个中文,定长,如果只放一个字节,后面就是就是九个空格(一个中文两个字节)

        nchar(10)放十个中文,定长

        varchar(10)放五个中文,变长,如果只放一个字节, 就只放一个字节

        nvarchar(10)放十个中文,变长。。。

    2、创建一个数据库,在数据库里面创建表,以及添加表里的项

        create datebase Library

        go

        use Library

        go

        create table Users 

           (

             UID int primary key,

             userName nvarchar(20)not null

             userPwd nvarchar(20)not null

            )

         create table Books

            (BID int primary key,

             bookName nvarchar(20)not null,

             price int not null

             authors nvarchar(15),

             )

    3、select语句用法

           select*from Products--查询出Products表里面的所有信息

           select ProductID,ProductName from Products where ProductID=1--查询出ProductID=1的所有ProductID和ProductName

          use pubs

          go

          select address from authors where au id='172-31-11-1176'--先导入指定的数据库pubs,查询出authors表里面,au id为172-32-1176的address

           select productID,productName from prooducts order by productID asc--默认是asc(可以不写)从小到大排序,desc是从大大小

           select*from products where UnitPrice>20--查询出products表里面UnitPrice>20的信息

          selecrt*from Emloyeees where LastName='King'and City='London'

          use pubs

          go

          select*from emloyee where fname='Paul' and job_id=5

          select*from Products where ProductID in(4,5,6)--查询出Product表中Produc为4,5,6,所有信息。

          select*from  Product where UnitPrice<10 and UnitPricr<30 order by UnitPrice--查询出Products表中UnitPrice<10并且UnitPrice<30的所有信息,并按照UnitPrice的大小由小到大排序

          select*from  Product where  UnitPrice between 10 and 30 order by UnitPrice上面的另外一种写法

          select*from  Employees where FirstName like 'A%'--查询出Emloyees中FirstName里面的第一个字母是A的所有人信息

          select*from Emloyees where FirstName like '%A%'--查询出Emloyees中FirstName里面中间有A的所有人信息

          select*from Employees where FirstName like'%A'--查询出Employees中FirstName里面最后一个字母是A的所有人信息

          select count(*)from Employees--查询出Employees表中的所有记录数

          select min(UnitPrice)from Products--查询出Products表中的Unitprice的最小值

          select max(UnitPirce)from Products--查询出Products表中的Unitprice的最大值

          select avg(Unitpirce)from Products--查询出Products表中的Unitprice的平均值

          select sum(Unitpirce)from Product--查询出Products表中的Unitprice的总和

          select top5*from Products--查询出前五条的记录信息

          select*from Products where Unitpirce>--有子查询,查找出比平均值高的商品信息

             (

                    select avg(Unitpirce)from Products

                )

     4、insert/update

         insert into BOOK(bookName,bookPirce,bookAuthors)

         values('C#基础','35','ji')

         insert into BOOK(bookName, bookPirce,bookAuthors)

         values('VB基础','12','ni')

         insert into BOOK(bookName,bookPirce,bookAurhers)

         values('C基础','35','das')

         updadte  BOOK set bookPirce=30 where bookName='C#基础'--修改BOOK表,让bookName为C#基础的项中的boolPirce=30

          delete BOOK where bookName='C#基础'

      5、有一个student表(学号,姓名,系名,课程名,成绩),查询至少修了四门课程的学生学号,姓名以及平均成绩的SQL语句。

       select stu,sname,avg(sorce)

       from student

       group by stu,sname

       having count(*)>=4

     6、

       select distinct[name]from Category         //查出Category中不重复的name

       select count(distinct name) from Category   //查出Categpry中不重复的name的数量

     7、连接

       inner join on,left join on, right join on 区别:

       表A记录如下:

       aID                           aNum

        1                               a20050111

        2                               a20050112

        3                               a20050113

        4                               a20050114

        5                               a20050115

       表B记录如下:

        bID                              bName

          1                                 2006032401

          2                                 2006032402

          3                                 2006032403

          4                                 2006032404

          8                                 2006032408

      实验如下:

        1. left join

          sql 语句如下:

          select*fromA

          left join B

          on A aID=B.bID

          结果如下:

          aID                          aNum                               bID

           1                              a20050111                        1

           2006032401

           2                              a20050112                        2

           2006032402

           3                              a20050113                        3

           2006032403

           4                              a20050114                        4

           2006032404

           5                              a20050115                        NULL

             (所影响的行数为5行)

             结果说明:      left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join 是以左表为准的,换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为:A.aID=B.bID)      

    B表记录不足的地方均为NULL

           

       2、right join

         sql语句如下:

           select*from A

           right joinB

           on A.aID=B.bID

           结果如下:

           aID                               aNum                           bID

            1                                   a20050111                         1

             2006032401

            2                                   a20050112                          2

             2006032402

            3                                   a20050113                          3

             2006032403

            4                                   a20050114                          4

             2006032404

            NULL                            NULL                                   8

             2006032408

            (所影响的行数为5行)

             结果说明:  仔细观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.

           3.inner join

            sql语句如下:

            select*fromA

            innerjoinB

            onA.aID=B.bID

             结果如下:

             aID                                    aNum                                  bID

             bName

             1                                       a20050111                            1

              2006032401

             2                                       a20050112                            2

              2006032402

             3                                       a20050113                            3

              2006032403                     

             4                                       a20050114                            4

              2006032404      

            结果说明:

            很明显,这里只显示出了A.aID=B.bID的记录,这说明inner join并不以谁为基础。它只显示符合条件的记录。

       8、用这种查询语句就可以查出指定区间记录的数据了

           select*from

             (select id ,row_number() over(orde by id desc)as row from[entry])Entry where row between 1 and 5000

       9、给表添加一字段

         alter table   Category

         add

         age int  null

          删除表:    drop table Users

       10、约束

         1.UNIQUE约束,于PK的区别:

           一张表只能有一个PK,但可以有多个UNIQUE约束

           PK约束定义主键不能为空,但UNIQUE可以为空值

           方法:点"管理索引和键"---"添加"---"类型"选择"唯一键"---"列"添加进多个UNIQUE--关闭保存

           它的作用是,当那多个unique同时都相同的时候,就会报错,实现了实体的完整性。

           也可以用SQL语言来实现:

            //单列处理

            creat table T2

            (ID int primary key idertity(1,1),Ename nvarchar(20)unique)

            //组合处理

             creat table T3

             (ID int primary key identity(1,1)

               Ename nvarchar(20)

               Ebir datetime

               unique(Ename,Ebir)

              )            

           2.check约束,它的作用是,约束一个键值,实现域完整性

             方法:

             "管理 checK约束"---"添加"---"常规"表达式---写入你要约束的键值的表达式比如:

             laborage>=2000 and laborage<=5000 ---"关闭"保存

             这样,在输入laborage的值的时候,如果不是大于等于2000小于等于5000就会报错

            也可以用SQL语言实现

             creat table T1

            (

             工资 money not bull check(工资between 2000 and 4000)

             )

         

      11.选择出生年月日

           select year(getdate())  //获取当前的年

           select month(getdate())   // 获取当前的月

           select day(getdate())     //获取当前的天

           select*from employ where month(birthday)=8      //打印出8月份过圣体的员工的所有信息

           selcet*from employ where year(getdate())-year(birthday)>25//year(getdate())为当前年份,打印出年龄大于25岁的员工的所有信息

           select*from employ where year(birthday)=2008 and month(birthday)=8 and day(birthday)=12//打印出生日为2008-8-12日的所有员工信息

          select dateadd(yy,100,gatdate())//当天加上100年的时间,getdate()可以换成具体的某一天比如写成:'2108/12/31'

          select dateadd(mm,1,getdate()) //当天加一个月的时间

          select dateadd(dd,100,getdate()) //当天加100前天的时间 

          select datediff(yy,getdae(),'2108/12/31') //当天距离2108/12/31还有多少年

          select datediff(mm, getdate(),2108/12/31')

          select datediff(dd,getdate(),2108/12/31')

          

      Month 

           mm、m

            Dayofyear

              dy、y

            Day

               dd、d

            Week

               wk、ww

            Weekday

               dw、w

            Hour 

               Hh

           Minute

               mi、n

           Second

               ss、s

           Millisecond

              Ms

         

         12.isnull

               select title,content, isnull(categoryID,0)from news  //为null的categoryID用0显示出来

         13.case用法

        //查找categoryID=13的state,并对sate进行判断

         select state,case

         when(state=1)then'待审'

         when(state=2)then'已审'

         end as pro_state

         from category where categoryID=13

         // 查找出低级的多少个,中级的多少个,高级的多少个

            select Count(*)as[Count]from category

            group by

            case

            when categoryID<15then'低级'

            when categoryID between 15 and20 then'中级'

            else'高级'

            end

        //查出category中的CategoryID,name,以及判断每个categoryID的大小

          select CategoryID,name,

          case

          when categoryID<15 then'低级'

          when categoryID beteween 15 and 20 then'中级'

          else'高级'

          end as categoryRange from category

  • 相关阅读:
    数据库字段太多,批量快速建立实体类方法(适合大量字段建立实体类)
    SQL service 中的 ”输入SQL命令窗口“ 打开了 “属性界面” 回到 ”输入SQL命令窗口“
    计算机软件编程英语词汇集锦
    编程常用英语词汇
    svn上传和下载项目
    当启动tomcat时出现tomcat setting should be set in tomcat preference page
    Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor
    eclipse中选中一个单词 其他相同的也被选中 怎么设置
    Spring Boot的@SpringBootApplication无法引入的问题
    最全的SpringCloud视频教程
  • 原文地址:https://www.cnblogs.com/screen2015/p/5165034.html
Copyright © 2011-2022 走看看