zoukankan      html  css  js  c++  java
  • SQL高级

    查询头几位:select top 20 * from table;(sql server) ;

                     select * from table  limit 20;

          select * from table rownum<20;

    通配符查询:select  * from table where Url not like '%localhost%';(不含有该字符串的)

          select  * from table where Url like '_h_';(以h为中心三字符)

          select  * from table where Url like '[Abc]%';(以a或者b或者c开头的)
          select  * from table where Url like  '[!Abc]%';(不是以a或者b或者c开头的)

    表连接:select * from table1 t1 inner join table2 t2 on t1.id=t2.id(普通连接保留连接失败空值,显示所有匹配不匹配行)

        select * from table1 t1 left join table2 t2 on t1.id=t2.id(左连接,以主表为主,主表显示所有行)
        select * from table1 t1 right join table2 t2 on t1.id=t2.id(右连接,以附表为主,附表显示所有行)
        select * from table1 t1 full join table2 t2 on t1.id=t2.id(返回完全匹配行)
        select * from table1 t1 left join t1 on t1.id1=t1.id2(自连接,一般用在同表内,审批流程,上下级关系比较多)

    表联合:select feild1 from table1 UNION select feild2 from table2 (把所求参数联合成一张表,注意参数类型要一致,还有就是该功能具有distinct去重功能)

        select feild1 from table1 UNIONALL select feild2 from table2 (把所求参数联合成一张表,注意参数类型要一致,还有就是该功能允许有重复值)

    备份表:select * INTO table2 from table1(查找表1的数据复制到表2上)

    高级表条件查询:select count(field1),case field2 when 1 then '第一类' else ‘其他’   end from table1 group by  EventTypeID;(高级条件过滤生成表。同样可以作用在更新或者插入语句中)

    创建数据库:create dateBase;

    创建数据表:create table table1( feild1 int identity(1,1),feild2 type2)(普通建表)

          create table table1( feild1 type1,feild2 type2)(普通建表自增)

          create table table1(feild1 type1 NOT NULL,feild2 type2 )(非空约束建表)

          create table table1(feild1 type1 NOT NULL,feild2 type2,UNIQUE(feild1) )(非空约束建表,外加唯一属性)
          create table table1(feild1 type1 NOT NULL  UNIQUE,feild2 type2 )(另外一种非空约束建表,外加唯一属性)
          create table table1(feild1 type1 ,feild2 type2, constraint c UNIQUE(type1 , type2) )(另外一种非空约束建表,外加唯一属性)
          

          create table table1(feild1 type1 NOT NULL,feild2 type2,PRIMARY KEY(feild1) )(创建具有主键的表)

          create table table1(feild1 type1 PRIMARY KEY ,feild2 type2)(创建具有主键的表) 

          create table table1(feild1 type1 PRIMARY KEY ,feild2 type2 , constraint c primary key(type1))(创建具有主键的表)

          

          create table table1(feild1 type1 NOT NULL,feild2 type2 FOREIGN KEY REFERENSES table2(id)) )(创建具有外键的表)

          create table table1(feild1 type1 PRIMARY KEY ,feild2 type2 , constraint c FOREIGN KEY table1(t2id) REFERENSES table2(id)) )(创建具有外键的表)

          create table table1(feild1 type1 NOT NULL,feild2 type2,PRIMARY KEY(feild1) )(创建具有主键的表)

          create table table1(feild1 type1 PRIMARY KEY ,feild2 type2)(创建具有主键的表) 

          create table table1(feild1 type1 PRIMARY KEY ,feild2 type2 , constraint c primary key(type1))(创建具有主键的表)

          

          create table table1(feild1 type1 ,feild2 type2, check (feild1>0))(创建具有条件的表)

          create table table1(feild1 type1  check (feild1>0),feild2 type2)(创建具有条件的表)

          create table table1(feild1 type1 PRIMARY KEY ,feild2 type2 , constraint c check (feild1>0))(创建具有条件的表)

          create table table1(feild1 type1 default '3',feild2 type2)(创建具有默认值的表)

    添加操作表:ALTER TABLE table  add UNIQUE(feild1) (增加唯一约束)

          ALTER TABLE table  add constraint c UNIQUE(type1 , type2)(增加唯一约束)

          ALTER TABLE table  add PRIMARY KEY(feild1) (增加唯一主键)

          ALTER TABLE table  add constraint c PRIMARY KEY(type1 )(增加唯一主键)

            ALTER TABLE table  add FOREIGN KEY table1(t2id) REFERENSES table2(id)) (增加唯一外键)

          ALTER TABLE table  add constraint c FOREIGN KEY table1(t2id) REFERENSES table2(id)) (增加唯一外键)

          alter table table1 alert  feild column set default '3'(在列已经存在的情况下增加默认值)

    删除操作表:alter table table1 drop  constraint c (删除所有类型约束)

              alter table table1 alert  feild column drop default (在列已经存在的情况下 去除默认值)

    
    

          

     
  • 相关阅读:
    第一天开通博客,记录自己在编程道路上的点点滴滴
    一个非常棒的jQuery 评分插件--好东西要分享
    jquery.easing的使用
    SEO优化
    nodejs(三)下之mangoDB
    nodejs(三)上之express
    nodejs(二)
    nodejs(一)
    angular(二)
    angular(一)
  • 原文地址:https://www.cnblogs.com/blackdeng/p/6731912.html
Copyright © 2011-2022 走看看