12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Pseudocolumns, Sequences, and <strong>SQL</strong> Functions6.5.2 Using SequencesA sequence is a database object similar to a pseudocolumn that generates uniquesequential values, often used for primary and unique keys. You can refer to sequencevalues in <strong>SQL</strong> statements with the CURRVAL and NEXTVAL pseudocolumns.To generate a sequence number, you call the sequence using the CURRVAL or NEXTVALkeywords. You must qualify CURRVAL and NEXTVAL with the name of the sequence,such as employees_seq.CURRVAL or employees_seq.NEXTVAL. Before you useCURRVAL for a sequence in your session, you must first initialize the sequence withNEXTVAL.Example 6–14 shows an example of the use of the employees_seq sequence with theemployee_id of the employees table. The employees_seq sequence is part of theHR schema and had been created for use with the employees table. When a sequenceis intended to be used with a specific table, it is a good practice to include the name ofthe table in the sequence name.Example 6–14 Using Sequences-- first initialize the employees_seq sequence with NEXTVALSELECT employees_seq.NEXTVAL FROM DUAL;-- after initializing the sequence, use CURRVAL as the next value in the sequenceINSERT INTO employees VALUES(employees_seq.CURRVAL, 'Belinda', 'Vernal', 'belinda.vernal', '555.111.2342','15-AUG-05', 'ST_CLERK', 6000, NULL, 124, 50);-- query the employees table to check the current value of the sequence-- which was inserted used as employee_id in the previous INSERT statementSELECT employee_id, last_name FROM employees WHERE last_name = 'Vernal';6.5.3 Using Character Functions<strong>Oracle</strong> Database provides a set of character functions that you can use in your <strong>SQL</strong>statements to customize the character values. With character functions, you canperform operations that upper case, lower case, trim blanks from, and concatenatecharacter data.Example 6–15 shows how to use character functions on character data.Example 6–15Using Character Functions-- you can use the UPPER function to display uppercase data, LOWER for lowercaseSELECT employee_id, UPPER(last_name), LOWER(first_name) FROM employees;-- you can use CONCAT function to concatenate character dataSELECT CONCAT('Last name: ', last_name) FROM employees;-- you can use RTRIM and LTRIM to remove spaces from the beginning or end of-- character data. Note the use of concatenation operator ||SELECT employee_id, RTRIM(first_name) || ' ' || LTRIM(last_name) FROM employees;-- you can TRIM to remove spaces from both the beginning and endSELECT employee_id, TRIM(last_name) || ', ' || TRIM(first_name) FROM employees;-- you can format the system date (SYSDATE) as a character string-- with various format masks and then display-- the following displays September 21 2005<strong>SQL</strong>: Usage Information 6-11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!