Sql常用语法总结(mySql)超详细版

create datebase ipay; 创建数据库

drop datebase ipay; 删除数据库

create table t_urm_pinf(

usr_no varchar2(12) default 0 not null ,

usr_id number(12) default 0 not null

)

comment on table T_URM_PINF is '手机用户信息表'; 表名说明

comment on column T_URM_PINF.usr_no :字段说明

create table t_urm_acin like t_urm_pinf :字段加索引复制 。

create table t_urm_acin as select usr_no... from t_urm_pinf definition only ;仅仅复制字段 。

 

删除表 drop table t_urm_pinf ;

增加一列 alter table t_urm_pinf add xxx varchar2(12) default 0 not null;

e : alter table ipay.t_urm_pinf add payseq_flg varchar2(1) default 'N' not null;

 

添加主键 alter table t_urm_pinf add primary key(usr_no);

删除主键 alter table t_urm_pinf drop primary key(usr_no)

 

创建索引

e: Create Index i_deptno_job on emp(deptno,job); —>在emp表的deptno、job列建立索引。

删除索引 :drop index i_deptno_job

 

创建视图 create view my_view as select usr_no from t_urm_pinf;

删除视图 drop view my_view

 

sql常用函数关键字

desc:降序,asc:升序 

总数:select count from t_urm_pinf

求和:select sum()from t_urm_pinf

平均:select avg(usr_no) from t_urm_pinf

最大:select max(usr_no) from t_urm_pinf

最小:select min(usr_no) from t_urm_pinf

复制表 select top 0 * into b from a

拷贝表 insert into b(a, b, c) select d,e,f from b;

 

外连接查询(表名1:a 表名2:b) 

select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

 

在线视图查询:select usr_no from (select a,b,c from t_urm_pinf) t where t.a='1';

 

between语法:

select * from t_urm_pinf a where a.id between a and b ;

select * from t_urm_pinf a where a.id not between a and b;

 

in语法

select *from t_urm_pinf a where a.id [not] in('a','b','c');

 

删除主表中在副表中已经没有的数据

delect from a where not exists(select *from b where a.a = b.a);

 

四表联查问题: 

select * from a left inner join b on a.a = b.a right inner join c on a.c = c.c inner join d on a.d = d.d where ...;

 

返回两个日期之间的天数。select DATEDIFF(c_dt,e_dt)as dt c_dt和e_dt是合法日期格式。

 

mysql前十条记录 select top 10 * from t_urm_pinf;

oracle 的随机取出十条记录:

select * from table_name where rownum

select * from (select a.*,a.rownum rn from (select * from table_name) a where a.rownumn --这句就是从n到m也的数据,分为三层结构

 

选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等

select * from t_urm_pinf up where up.a = (select max(a) from t_acm_acin aa where aa.b= up.b)

 

随机取一条数据

select * from (select * from t_urm_pinf order by dbms_random.value) where rownum = 1;

 

删除重复记录 

(中间表方法)select distinic * into a from b ;

delect from b;

insert into b select * from a;

 

delect from t_urm_pinf where usr_no not in (select max(usr_no) from t_urm_pinf group by list1,list2,list3...所有列 );

所有列查找不重复的列,然后删除除不重复列以外的其他列。

 

包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表 

(select * from a)expect(select * from b)expect(select * from c)

 

日期格式

SELECT hiredate

FROM employees

WHERE hiredate between cast('1993-10-17' as datetime) and cast('1994-01-02' as datetime)

 

你可能感兴趣的:(数据库)