create sequence seq_common_id
increment 1
minvalue 1
maxvalue 99999999
start 1
cache 20;
select nextval('seq_common_id') ;
select currval('seq_common_id') ;
查询PG当前数据库中的所有的表
SELECT T.TABLENAME FROM PG_TABLES T WHERE SCHEMANAME = 'public'
根据表名,查询表的所有的字段
select a.attname
from pg_class c, pg_attribute a
where c.relname = 'table_name'
and a.attrelid = c.oid
and a.attnum > 0
order by a.attnum ;
没有序列这个说法
但是可以设置 主键为 auto-increment
也可以结合数据库表和函数来实现 序列相同的效果
创建一张表来存序列相关信息(包括:序列名称、当前序列值、步长)
CREATE TABLE `tab_sequence` (
`sequence_name` varchar(255) NOT NULL COMMENT '序列名称',
`current_value` int(255) NOT NULL COMMENT '序列当前值',
`increment` int(255) DEFAULT NULL COMMENT '步长,每次递增多大'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入一个序列(就是一条表记录)
INSERT INTO tab_sequence(sequence_name, current_value, increment) VALUES ('seq_common_id', 0, 1);
创建一个函数 current_value ,取序列的当前值
CREATE FUNCTION current_value(seq_name varchar(50)) RETURNS int(10)
begin
declare retValue int ;
set retValue = 0 ;
select current_value into retValue from tab_sequence where sequence_name = seq_name ;
return retValue;
end
使用方式:
SELECT current_value('seq_common_id') ;
创建一个函数 next_value,取序列的下一个值
CREATE FUNCTION next_value(seq_name varchar(50)) RETURNS int(10)
BEGIN
update tab_sequence set current_value = current_value + increment
where sequence_name = seq_name ;
RETURN current_value(seq_name);
END
调用方式:
SELECT next_value('seq_common_id') ;
select table_name
from information_schema.TABLES
where TABLE_SCHEMA = '数据库名'
and TABLE_TYPE = 'BASE TABLE' ;
select COLUMN_NAME
from information_schema.COLUMNS
where TABLE_NAME = '表名';
oracle 建序列的语句不一样,有些小出入
create sequence seq_common_id
minvalue 1
maxvalue 9999999999
increment by 1
start with 1
cache 20
noorder
nocycle ;
select seq_common_id.nextval from dual ;
SELECT seq_common_id.currval from dual ;
查询当前数据库中所有的表
SELECT t.TABLE_NAME FROM USER_TABLES t ;
根据表名,查询表的所有的字段
SELECT T.COLUMN_NAME
FROM USER_TAB_COLUMNS T
WHERE T.TABLE_NAME = 'table_name'
ORDER BY T.COLUMN_ID ;
H2数据库
create sequence seq_common_id
increment 1
minvalue 1
maxvalue 99999999
start 1
cache 20;
select SEQ_COMMON_ID.nextval ;
select SEQ_COMMON_ID.currval;
SELECT t.table_name
FROM INFORMATION_SCHEMA.TABLES t
where t.table_schema = 'PUBLIC'
and t.table_type = 'TABLE'
SELECT t.column_name
FROM INFORMATION_SCHEMA.COLUMNS t
where t.table_name = 'table_name'
order by t.ordinal_position
欢迎关注,谢谢!
刚开始写微信公众号,请多多关注,欢迎,多谢!
微信公众号:《Java学习积累》
请关注一下,多谢!!!