Mysql
Assuming database name is test, we can find the schema details fromINFORMATION_SCHEMA.SCHEMATA
.
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'test'
INFORMATION_SCHEMA.SCHEMATA
.
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'test'
select sysdate from dual
select systimestamp from dual
select getdate();
select current_timestamp
select now()
select now()
select sysdate
select current_timestamp from dummy
select current_timestamp
select distinct current timestamp from informix.systables
call current_timestamp"
call current_timestamp()
values current timestamp
select now()
USER_TABLES
has information about the tables that you own.
SELECT 1 FROM USER_TABLES WHERE LOWER(TABLE_NAME) = 'emp';
SYS.OBJECTS
contains all the schema-scoped object that is created within a database
OBJECT_ID
- Object identification number.
SELECT 1 FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'emp') AND TYPE IN (N'U');
SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EMP'
SELECT 1 FROM INFORMATION_SCHEMA.TABLES where LOWER(TABLE_SCHEMA) = '{SCHEMA_NAME}' AND LOWER(TABLE_NAME) = 'emp'
SELECT current_schema
SELECT 1 FROM INFORMATION_SCHEMA.TABLES where LOWER(TABLE_SCHEMA) = '{SCHEMA_NAME}' AND LOWER(TABLE_NAME) = 'emp'
create sequence xyz;Since we have not specified the starting number, by default it will start with 1.
select xyz.currval from dual;Since we have not yet retrieved the sequence, trying to fetch current value will fail.
ORA-08002: sequence XYZ.CURRVAL is not yet defined in this session 08002. 00000 - "sequence %s.CURRVAL is not yet defined in this session" *Cause: sequence CURRVAL has been selected before sequence NEXTVAL *Action: select NEXTVAL from the sequence before selecting CURRVALLet's now issue
nextval
.
select xyz.nextval from dual;Result:
1Now we can get the current sequence value.
select xyz.currval from dual;Result:
1
select xyz.nextval from dual;Result:
2As you can see, by default, sequence gets incremented by 1. Let's drop the sequence and recreate it with more details.
drop sequence xyz;Result:
Sequence XYZ dropped.
create sequence xyz start with 3 increment by 2 maxvalue 10 minvalue 3;Next value:
select xyz.nextval from dual;Result:
3Next value will be 5, 7 and 9. After the sequence returns 9, we will try to fetch the next value.
select xyz.nextval from dual;
ORA-08004: sequence XYZ.NEXTVAL exceeds MAXVALUE and cannot be instantiated 08004. 00000 - "sequence %s.NEXTVAL %s %sVALUE and cannot be instantiated" *Cause: instantiating NEXTVAL would violate one of MAX/MINVALUE *Action: alter the sequence so that a new value can be requestedIt's bound to fail as the next value will be 11 which > than the maximum allowed.
cycle
. After reaching its maximum value, the sequence will generate the minimum value as the next value.
create sequence xyz start with 3 increment by 2 maxvalue 10 minvalue 3 CYCLE;Result:
Error starting at line : 3 in command - create sequence xyz start with 3 increment by 2 maxvalue 10 minvalue 3 CYCLE Error report - ORA-04013: number to CACHE must be less than one cycle 04013. 00000 - "number to CACHE must be less than one cycle" *Cause: number to CACHE given is larger than values in a cycle *Action: enlarge the cycle, or cache fewer valuesIt fails as database pre-allocates and keeps the sequences in memory for faster access and by default it caches next 20 sequences. Since our cycle consists of 10 numbers, the sequence creation fails Above sequence starts 3, skipping next 20 values, we will hit value 43, so creating sequence with 43 as maximum will work fine.
create sequence xyz start with 3 increment by 2 maxvalue 43 minvalue 3 cycle;Let's try with our previous sequence creation, see if it works with a cache of 1.
create sequence xyz start with 3 increment by 2 maxvalue 10 minvalue 3 CYCLE cache 1;Result:
Error report - ORA-04010: the number of values to CACHE must be greater than 1 04010. 00000 - "the number of values to CACHE must be greater than 1" *Cause: the value in the CACHE clause was one *Action: specify NOCACHE, or a value larger than oneFails again as the minimum cache allowed is 2.
create sequence xyz start with 3 increment by 2 maxvalue 10 minvalue 3 CYCLE cache 2;Now with cycle enabled, sequences generated will be 3, 5, 7, 9| 3, 5, .... After reaching 9, it restarts sequence generation with the minimum value.
create sequence xyz start with 3 increment by 1;Get next value:
select xyz.nextval from dual;Result:
3Let's assume we want to restart sequence generation from 7 onwards. Example, 7, 8, etc. To achieve we will first alter the sequence to reset the increment.
alter sequence xyz increment by 4;Now get the next value.
select xyz.nextval from dual;Result:
7Reset the increment to 1.
alter sequence xyz increment by 1;Get the next value.
select xyz.nextval from dual;
8