常用sql相关

来源:互联网 发布:360手机 知乎 编辑:程序博客网 时间:2024/05/01 10:24

1修改基本表

 添加列 alter table tableName add<新列名><数据类型>[完整性约束]

 删除列 alter table tableName drop<新列名><数据类型>[完整性约束]

2创建

  建表create table tableName(

id primary key AUTO_INCREMENT(mysql);

id primary key identity(1,1)(sql server)

)

   建立索引

  create index OID_IDX on 表名(number);
  create unique index oin_idx on 表名(number);创建唯一索引

3 删除

   删除索引 drop index <索引名>

   删除表     drop table <表名>

4 sql功能

   数据查询 select

   数据定义 create drop alter

   数据操控 insert update delete

   数据控制 grant(授权) revoke(收回权限)

(1) 数据查询

     select * from tableName where <条件表达式> group by<列名>[having<条件>]  order by<列名>[ASC DESC] 

     select distinct Sno from sc 查询数据去掉重复行

     常见的查询条件

     比较 = ,>,<,<=,<=,!=,<>,!<,!>,

     确定范围 between and |not between and

     确定集合 in ,not in

     字符匹配 like ,not like like '<匹配符>' 匹配符可以包含通配符%和_

     空值       is null ,is not null

     多重条件(逻辑运算) and,or,not

     select  a.name,a.mialbox from a where EXISTS(select b.name from b where a.name=b.name)

     联接查询join

     select a.number,a,name,b.age,b.sort from a join b on a.number=b.number

     inner join 内联接 用于返回两个表中要查询的列数据 left join right join

     常见运算以及聚集函数

         ABS(x)返回绝对值
         sing(x) 为负时返回-1为0是返回0,为正是返回1
         mod(x,y) x除以y的余数。跟x%y一样
         floor(x) 返回小于等于x的最大整数
         ceiling ceil(x)返回大于等于x的最小整数
         power(x,y)返回x的y次方的数值
         round(x,d)返回小数点熟为d的接近于x的数
         sort(x)返回x的平方根

         count(*)统计元素个数

         sum() 列值和

         avg()列值平均数

         min() max()

      连接查询union all (连接查询时,两条查询语句所查询的列必须相同)

  (2) update tableName set <列名>=<表达式>。。where<条件>

  (3) insert into tableName (列名) values(表达式)

  (4) delete from tableName 删除表中数据(不释放表空间)

        truncate table 删除表中所有数据的快速方法(收回表空间,快速 无法恢复)

 

 

原创粉丝点击