SQL 语言知识点总结

一、知识点总括

sql语言:

    数据定义语言(DDL)

            1.建数据库

            2.修改数据库 ALTER

            3.使用数据库 USE

            4. 删除数据库 DROP

            5.表(创建、修改、其他表创建新表select into 、删除)

   数据操纵语言(DML)

            1、插入语句 insert into    values

            2.  修改

            3.   删除

    数据查询语言(DQL)

           1. 投影查询

           2. 选择查询

           3. 连接查询

           4. 统计计算

           5. 排序查询

           6. 子查询

           7. 其他子查询(union/except/intersect/into/cte/from)

 

二、详细知识点

1、数据定义语言(DDL)

    创建数据库:

create database stsc //stsc是数据库的名称
on //数据库文件和文件组的属性
{
     name = 'stsc',
     filename = 'C:\programfiles\microsoft sql sever'//保存路径
     size = 3MB,
     Maxsize = 30MB,
    filegrowth = 1MB
 }
log on //日志文件属性
{
     name = 'stsc_log',
     filename = 'C:\',
     size = 1MB,
     Maxsize = 10MB,
     fllegrowth = 10%
 };
Code

    修改数据库:

       ADD LOG FILE //增加日志文件

       REMOVE FILE //要删除的数据文件

       modify file //要更改的文件属性

       MODIFY NAME//重命名数据库

alter database test
add file//要增加的数据库文件
{
    name = 'testadd',
    filename ,
    size,
    maxsize,
    filegrowth
}
Code

    使用数据库

        use database_name(数据库名称)

    删除数据库

         drop database_name(数据库名称)

    创建表

use stsc//使用数据库
create table student(表名)
{
    stno char(4) not null primary key,
    stname char(8) not null,
    stsex char(2) not null,
    stbirthday data not null,
    specialist char(12) null,
    tc int nullgo
Code

    有其他表创建新表

       例如:在数据库中由student表创建student1表

          use stsc

          select stno,stname,stbirthday into student1

          from student

     修改表

         use stsc

         alter table student1 add remarks(10)

     删除表

         use stsc

          drop table student1

2、数据操纵语言(DML)

      插入语句(insert)

           values子句:包含各列需要插入的数据,数据的顺序要与列的顺序相对应。

           values子句中的值有以下三种:

              1.default:指定为该列的默认值,要求定义表时必须指定该列的默认值

              2.null: 空值

              3. expression:可以是一个变量、常量或一个表达式,其值的数据类型要与列的数据类型一致(表达式中不能有select和execute语句)

  use stsc

  insert into student values('数据',‘信息’);

  go

        修改语句

use stsc 
updata clients
set address = '北大街120号'
where cid = 1
Code

       删除语句

 use stsc

delete student

where stno = '2006'

3、数据查询语言(DQL)

   3.1.投影查询

      3.1.1投影指定的列

           使用select语句可选择表中的一个列或多个列,如果是多个列,各列之间要用逗号分开。

            select colum_name

            from table_name

            where search_condition

      3.1.2投影全部列

             在select子句指定列的位置上使用*,表示查询表中的所有列。

      3.1.3修改查询结果的列标题

             为了改变查询结果中显示的列标题,可以在列名后使用as子句。

      3.1.4去掉重复行

              使用distinct去掉结果中重复行

                use stsc

                select distinct specialist

                from student

  3.2选择查询

  where子句的常见查询条件

查询条件                              谓词
    比较                        <=,<,=,>=,>,!=,!<,!>
  指定范围         between and   /   not between  and   /  in   
  确定集合                                  in   not in
  字符匹配                                    like   not like
   空值                               is null       is not null
多重条件                                   and or

3.3连接查询

    join

   inner表示内链接

   outer表示外连接

   cross表示交叉连接

3.4统计计算

     3.4.1 聚合函数

            AVG(平均值)、 Count (项数)、MAX、MIN、SUM、STDEV(偏差)、STDEVP(标准偏差)、VAR(方差)、VARP(填冲统计方差)

     3.4.2group by 子

             用于将查询结果表示按某一列或多列值进行分组

     3.4.3having子句

3.5排序查询

     order  by (ASC升序 DESC降序)

3.6子查询

     in子查询用于判断一个给定值是否在查询结果集中,查询结果若为Null则返回false,否则返回true.

3.7其他子句

    3.7.1 union:可以将两个或多个select的结果合并成一个结果集

    3.7.2 except 和 intersect:比较两个查询结果返回非重复值

    3.7.3 into子句:用于创建新表并将查询所得的结果插入到新表中

    3.7.4 cte子句:用于指定临时结果集,这些结果称为公用表表达式

   3.7.5 from子句

 

   

 

posted @ 2017-06-14 11:41  sahe  阅读(437)  评论(0)    收藏  举报