SQL学习之三

建立复杂的查询:

             在查询里结合表,利用where子句来完成,结合的类型分为等直结合(内部结合)、自然结合、非等值结合、外部结合、自结合。

             等值结合的语法:select   table1.column1,table2.column2

                                                           from  table1,table2  [,table3]

                                                           where   table1.column_name =  table2.column_name

                                                                          [and  table1.column_name =  table3.column_name];

                                             利用inner  join 来提高可读性:

                                             select   table1.column1,table2.column2

                                                           from  table1

                                                           inner  join  table2

                                                          on    table1.column_name =  table2.column_name

                                                                          [and  table1.column_name =  table2.column_name];

           自然结合:      

                                          select   table1.*,table2.column_name [table3.column_name]

                                                             from  table1,table2  [,table3]

                                                            where   table1.column_name =  table2.column_name

                                                                          [and  table1.column_name =  table3.column_name];

           使用表的别名:两个表的别名不能一致。

             不等值结合(同一个字段在不同的表里的值不相等):

                                from  table1,table2[,table3]

                                          where  table1.column_name != table2.column_name

                                         [and  table1.column_name != table3.column_name];

             外部结合:from  table1

                              {right|left|full}  [outer]    join

                              on table2;

             自结合:select A.column_name , B.column_name ,[C.column_name]

                           from  table1 A, table2 B   [ table3 C]

                           where  A.column_name =  B.column_name

                             [  and  A.column_name =  C .column_name ];

                 结合多个主键,使用基表,笛卡尔积。


使用子查询来定义未确定数据:

                  子查询可以用于主查询的where或者having语句。逻辑和关系操作符可以用于子查询里。子查询的select子句里只能有一个字段,除非查询里有多个字段让子查询进行比较。子查询里不能使用ORDER  BY 子句,在子查询里可以使用group by实现ORDER BY的功能。返回多条记录的子查询只能与多值操作符(in)配合使用。select列表里不能引用任何blob、array、clob或nclob类型的值。子查询不能包含在函数里,操作符between不能用于子查询,但在子查询内部可以使用它。

                   基本语法:select column_name  

                                                    from  table

                                                     where column_name =  (select  column_name 

                                                                                                   from  table 

                                                                                                   where conditions);

                  between用于子查询的正确范例:select column_name 

                                                                                              from  table

                                                                                               where  column_name  operator  (select  column_name

                                                                                                                                                             from  table

                                                                                                                                                            where   value  between  value);

                 子查询与select语句:     select  column_name  [ ,column_name]

                                                                           from   table1 [,table2]

                                                                            where  column_name  operator

                                                                                                (select  column_name [,column_name]

                                                                                                 from  table1 [,table2]

                                                                                                  [where] );

                  子查询与insert语句:

                                                                        insert  into  table_name  [(column1 [,column2])]

                                                                        select [*|column1[,column2]]

                                                                         from table1,[,taable2]

                                                                         [where value  operator  ];

                  子查询与update、delete 语句等,见SQL入门经典的page 169.

                  嵌套的子查询的基本语法:select  column_name  [,column_name]

                                            from table1 [,talbe2]

                          where  column_name operator (select  column_name

    from  table

  where  column_name operator

  (select  column_name

      from table

            [where  column_name operator  value]));

 组合多个查询:

                            组合查询操作符之union:select  column1 [ ,column2]   from  table1 [,table2] [where ] union [all] select column1 [,column2] from table1 [,table2] [where];          intersect:select column1 [,column2]  from table1 [table2]  [where ]  intersect  select column1 [,column2]  from  table1  [,table2]  [where];

        except : select column1 [,column2] from table1 [,table2] [where] except select column1 [,column2]  from table1 [,table2] [where];

                             组合查询使用order  by: select column1 [,column2]  from table1 [,table2]  [where]  operator {union |except| intersect|union all}

                                           select  column1  [,column2]  from table1  [,table2]  [where ]  []order by];

                             组合查询使用group by: select column1 [,column2]  from table1 [,table2]  [where]  [group  by ]  [having]  

                              operator  {union|except | intersect|union all} 

                                 select  column1 [,column2]   from table1  [,table2] [where]  [group  by ] [having] [order by ];

利用索引改善性能;   

   索引相当于一个指针,指向表里的数据。但是,索引有时候占用的物理内存比表本身的数据占用的还大。

                         语法:create [  UNIQUE  ]   index   index_name  on  table_name (COLUMN_NAME);

                     create  index index_name  on table(column1, column2 );
                         隐含索引:其是数据库服务程序在创建对象是自动创建的,例如数据库会给主键约束和唯一性约束自动创建索引。唯一索引隐含的与主键共同实现主键的功能,外键经常与父表结合,也适合设置索引。一般的来说,大多数用于表结合的字段都应该设置索引。   如果对表的关系、查询和事务需求、数据本身有这透彻的了解才能有效的使用索引,所以要提前规划好。索引不应该用于小规模的表,当字段用于where子句作为过滤器会返回表里的大部分记录是,字段不适合建立索引。经常批量更新的表可以具有索引,但是效率不是太高。包含大量的NULL值的字段不设置索引。

                        删除索引:drop index   index_name;

改善数据库的性能:涉及到数据库调整、SQL语句调整、格式化SQL语句、from子句里的表、结合条件测次序、全表扫描、使用LIKE操作符合通配符、避免使用or操作符、避免使用HAVING语句、避免大规模的数据库操作、使用存储过程、在批量加载是关闭索引、使用性能工具。                         

                                                              



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值