项目中用到的SQL

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoleizhanghahaha/article/details/84673209

添加字段

ALTER TABLE 表名 ADD 字段名 数据库该字段类型(integer);
alter table 表名 add column 字段名 varchar(255);

添加字段 并设置默认值

ALTER TABLE 表名 ADD 添加的字段名 字段类型varchar(64) 默认值null;
ALTER TABLE 表名 ADD 字段名 boolean default false;
ALTER TABLE 表名 ADD 字段名 int default 1;

修改字段类型

ALTER TABLE 表名 ALTER COLUMN 字段 TYPE 字段类型 varchar(256);
ALTER TABLE 表名 ALTER column 字段 TYPE character varying(255);

修改字段值

update 表名 set 字段1=字段2 where 条件(案例:type=1);
UPDATE 表名 SET name=user_name;
UPDATE 表名 SET 字段1 = false, 字段2 = false;
update 表名 set 字段1=0,字段2=false where 条件(案例:type=1);

按条件更新值

update 表名 a set 字段1=(select 字段1 from 表名 b where type=1 and a.ship_no=b.ship_no ) where type=0;

创建表

CREATE TABLE 表名 (
字段名(id) 字段类型(int8) 默认值(NOT NULL),
created_by int8 NULL,
date_created timestamp NULL,
date_modified timestamp NULL,
modified_by int8 NULL,
“version” int4 NULL,
-----外键
CONSTRAINT 表名_pkey PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS 表名 (
字段名(id) 字段类型(int8) 默认值(NOT NULL),
created_by int8 NULL,
date_created timestamp NULL,
date_modified timestamp NULL,
modified_by int8 NULL,
“version” int4 NULL,
-----外键
CONSTRAINT 表名_pkey PRIMARY KEY (id)
);

创建索引

CREATE INDEX 索引名字 ON 表名 (按那个字段排序 DESC) ;
CREATE INDEX 索引名字 ON 表名 (字段) ;

删除字段

ALTER TABLE 表名 DROP COLUMN 字段 ;
alter table 表名 drop column 字段 ;

删除索引

DROP INDEX 表名_字段_idx ;

批量按条件修改字段值

update 表名 set 字段00=
case
when (字段1 is not null and 字段2 is not null) then 字段1+字段2
when (字段1 is not null and 字段2 is null) then 字段1
when (字段1 is null and 字段2 is not null) then 字段1
else null end,

字段01=
case when (字段1 is not null and 字段2 is not null) then 字段1+字段2
when (字段1 is not null and 字段2 is null) then 字段1
when (字段1 is null and 字段2 is not null) then 字段2
else null end,

字段02=
case when (字段1 is not null and 字段2 is not null) then 字段1+字段2
when (字段1 is not null and 字段2 is null) then 字段1
when (字段1 is null and 字段2 is not null) then 字段2
else null end;

创建序列化

CREATE SEQUENCE seq_itv_collection
INCREMENT BY 1 – 每次加几个
START WITH 1399 – 从1开始计数
NOMAXVALUE – 不设置最大值
NOCYCLE – 一直累加,不循环
CACHE 10; --设置缓存cache个序列,如果系统down掉了或者其它情况将会导致序列不连续,也可以设置为---------NOCACHE

修改序列化

Alter Sequence
.如果想要改变 start值,必须 drop sequence 再 re-create .
Alter sequence 的例子
ALTER SEQUENCE emp_sequence
INCREMENT BY 10 也可以是1
MINVALUE 1
MAXVALUE 10000 也可以是9223372036854775807
CYCLE – 到10000后从头开始
NOCACHE ;

删除序列化:

DROP SEQUENCE seq_itv_collection;
需要注意的是:想改变序列化的minvalue必须删除序列化后再重新建立序列化。不可以修改序列化

序列化使用

CurrVal:返回 sequence的当前值     NextVal:增加sequence的值,然后返回 增加后sequence值 

SELECT Sequence名称.CurrVal FROM DUAL; 得到值 select emp_sequence.currval from dual

insert into 表名(id,name)values(seqtest.Nextval,‘sequence 插入测试’);

alter sequence SEQTEST maxvalue 9999999;

触发器 中使用序列

create sequence SEQ_ID
minvalue 1
maxvalue 99999999
start with 1
increment by 1
nocache
order;

建解发器代码为

create or replace trigger tri_test_id
before insert on S_Depart --S_Depart 是表名
for each row
declare
nextid number;
begin
IF :new.DepartId IS NULLor :new.DepartId=0 THEN --DepartId是列名
select SEQ_ID.nextval --SEQ_ID正是刚才创建的
into nextid
from sys.dual;
:new.DepartId:=nextid;
end if;
end tri_test_id;

函数

首先我们创建一个名为employee的表,添加相应的数据:
INSERT INTO employee (Id, Salary)
VALUES(3, 300),(2, 200),(1, 100);
声明一个Function,名为getNthHighestSalary() 调用时通过输入的参数来执行相应结果的SQL,我这里返回的是一个INT类型的结果字段
DELIMITER //
CREATE FUNCTION getNthHighestSalary(N INT)
RETURNS INT
BEGIN
RETURN(SELECT MAX(Salary) as Salary FROM Employee E1 WHERE N-1 = (SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2 WHERE E2.Salary > E1.Salary));
END//
DELIMITER ;
在相应的ide可以查看到Function的名字 接下来我们就可以调用了,就可以看到返回的结果
select getNthHighestSalary(2);
函数中DELIMITER // 和 END结尾的 // 是告诉mysql解析器让他执行一整段的sql 语句。(一般sql解析器会遇到分行就执行相应的语句)

现实案例

CREATE FUNCTION nextseq(configkey character varying, seqname character varying) RETURNS bigint
LANGUAGE plpgsql
AS $ $
DECLARE preVal NUMERIC;
DECLARE nextVal NUMERIC;
BEGIN
SELECT value INTO preVal FROM otms_sequence WHERE key = configKey;
IF preVal IS NULL then
EXECUTE ‘ALTER SEQUENCE ’ || seqName || ’ RESTART’;
END IF;
SELECT NEXTVAL(seqName)::text INTO nextVal;
IF preVal IS NULL THEN
INSERT INTO otms_sequence(key, seq_name, value) VALUES (configKey, seqName, nextVal);
ELSE
UPDATE otms_sequence SET value = nextVal WHERE key = configKey;
END IF;
RETURN nextVal;
END;
$ $;
–格式
CREATE TABLE IF NOT EXISTS [Table Definition];

–示例
CREATE TABLE IF NOT EXISTS student(id int unsigned not null primary key,name varchar(32) not null);

drop table if exists [tableName];
CREATE TABLE IF NOT EXISTS [tableName] SELECT…

猜你喜欢

转载自blog.csdn.net/xiaoleizhanghahaha/article/details/84673209