SQL语句大全罗列(持续更新)

1.修改数据库某表数据

select * from tabdata_live_cycle for update

----在某个字段后面加一个字符(单位)

update tabdata_live_cycle set source_storage_time = CONCAT(source_storage_time,'M')
WHERE  source_storage_time not like ('%M%') and source_storage_time not like('%D%')

2.新增字段

--a.oracle

alter table 表名 add column 字段名 varchar(64)
alter table TABDATA_LIVE_CYCLE add column  ISFILE varchar(64);

--b.oracle(没有column,删除也一样)

alter table TABDATA_LIVE_CYCLE add  ISFILE varchar(64) default '1';

3.删除字段

alter table table_name drop column column_name  --非oracle
alter table table_name drop column_name  --oracle
alter table  TABDATA_LIVE_CYCLE  drop (ISFILE);

--删除数据

delete from mon_warnrule WHERE "id" <9114 

4.修改字段

--a.修改字段类型

alter table table_name ALTER COLUMN column_name new_data_type;

--记住,该处要是原先数据是null,会存在约束,

--所以会存在直接使用上面sql报错,所以将默认值改为0

alter table db_conn alter column db_type
set data type int4 using db_type :: int4,
alter column db_type set default 0;

--b.修改字段长度

alter table db_conn alter column db_type  type int8;
alter table db_conn alter column db_type  type varchar(64);

--c.在某个字段后面加一个字符(单位)

update tabdata_live_cycle set source_storage_time = CONCAT(source_storage_time,'M')
 WHERE  source_storage_time not like ('%M%') and source_storage_time not like('%D%')

5.sql的类型转化,将varchar转化成int

--(主要用在表定义的类型和入参时候的类型不一致的情况下)

update system_menu set
leaf = 'false'
where menucode = cast('23000' as int)

6.Mybatis sql if的判断,是否以某字符开始,结束或者包含

<if test="username != null and username.indexOf('ji') == 0"> </if>?<!-- 是否以什么开头 -->
<if test="username != null and username.indexOf('ji') >= 0"> </if>?<!-- 是否包含某字符 -->
<if test="username != null and username.lastIndexOf('ji') > 0"></if> ?<!-- 是否以什么结尾 -->

7.根据某个字段去重

Select DISTINCT device_id from table where 1=1

--添加注释

comment on column TABDATA_LIVE_CYCLE.ISFILE is '是否需要归档-1:是-归档;

猜你喜欢

转载自www.cnblogs.com/b-code/p/9086894.html
今日推荐