The Section Called Formatting Functions: Source
The Section Called Formatting Functions: Source
The extract function retrieves sub-fields from date/time values, such as year or hour.
source is a value expression that evaluates to type timestamp or interval. Expressions of
type date or time will be cast to timestamp and can therefore be used in most cases.
The extract function is primarily intended for computational processing. For formatting
date/time values for display, see the Section called Formatting Functions.
field is an identifier or string that selects what field to extract from the source value.
The extract function returns values of type double precision. The following are valid
values for field:
century
Note that the result for the century field is simply the year field divided by 100,
and not the conventional definition, which considers most years in the 1900's to
be in the twentieth century..
day
epoch
For date and timestamp values, the number of seconds since 1970-01-01 00:00:00
(Result may be negative.); for interval values, the total number of seconds in the
interval
millennium
Note that this is not really the millennium that the date is in.
milliseconds
minute
For timestamp values, the number of the month within the year (1 - 12) ; for
interval values the number of months, modulo 12 (0 - 11)
second
Note that a value of 60 may be valid if leap seconds are implemented by the
operating system
week
From a timestamp value, calculate the number of the week of the year that the day
is in. By definition (ISO 8601), the first week of a year contains January 4 of that
year. (The ISO week starts on Monday.) In other words, the first Thursday of a
year is in week 1 of that year.
date_part
The date_part function is the traditional PostgreSQL equivalent to the SQL-function
extract:
date_part('field', source)
Note that here the field value needs to be a string. The valid field values for date_part
are the same as for extract.
date_trunc
The function date_trunc is conceptually similar to the trunc function for numbers.
date_trunc('field', source)
source is a value expression of type timestamp (values of type date and time are cast
automatically). field selects to which precision to truncate the time stamp value. The
return value is of type timestamp with all fields that are less than the selected one set to
zero (or one, for day and month).
microseconds
milliseconds
second
minute
hour
day
month
year
decade
century
millennium
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00+00
Current Date/Time
The following functions are available to obtain the current date and/or time:
CURRENT_TIME
CURRENT_DATE
CURRENT_TIMESTAMP
Note that because of the requirements of the SQL standard, these functions must not be
called with trailing parentheses.
SELECT CURRENT_TIME;
19:07:32
SELECT CURRENT_DATE;
2001-02-17
SELECT CURRENT_TIMESTAMP;
2001-02-17 19:07:32-05
There is also timeofday(), which returns current time to higher precision than the
CURRENT_TIMESTAMP family does:
SELECT timeofday();
Sat Feb 17 19:07:32.000126 2001 EST
timeofday() uses the operating system call gettimeofday(2), which may have
resolution as good as microseconds (depending on your platform); the other functions
rely on time(2) which is restricted to one-second resolution. For historical reasons,
timeofday() returns its result as a text string rather than a timestamp value.
It is quite important to realize that CURRENT_TIMESTAMP and related functions all return
the time as of the start of the current transaction; their values do not increment while a
transaction is running. But timeofday() returns the actual current time.
All the date/time datatypes also accept the special literal value now to specify the current
date and time. Thus, the following three all return the same result:
SELECT CURRENT_TIMESTAMP;
SELECT now();
SELECT TIMESTAMP 'now';
DCL commands are used to enforce database security in a multiple user database
environment. Two types of DCL commands are GRANT and REVOTE. Only Database
Administrator's or owner's of the database object can provide/remove privileges on a
databse object.
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
privilege_name is the access right or privilege granted to the user. Some of the
access rights are ALL, EXECUTE, and SELECT.
object_name is the name of an database object like TABLE, VIEW, STORED
PROC and SEQUENCE.
user_name is the name of the user to whom an access right is being granted.
user_name is the name of the user to whom an access right is being granted.
PUBLIC is used to grant access rights to all users.
ROLES are a set of privileges grouped together.
WITH GRANT OPTION - allows a user to grant access rights to other users.
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
1) System privileges - This allows the user to CREATE, ALTER, or DROP database
objects.
2) Object privileges - This allows the user to EXECUTE, SELECT, INSERT, UPDATE,
or DELETE data from database objects to which the privileges apply.
System
Description
Privileges
allows users to create the specified
CREATE object
object in their own schema.
CREATE ANY allows users to create the specified
object object in any schema.
The above rules also apply for ALTER and DROP system privileges.
Object Description
Privileges
INSERT allows users to insert rows into a table.
allows users to select data from a
SELECT
database object.
UPDATE allows user to update data in a table.
allows user to execute a stored
EXECUTE
procedure or a function.
Roles: Roles are a collection of privileges or access rights. When there are many users in
a database it becomes difficult to grant or revoke privileges to users. Therefore, if you
define roles, you can grant or revoke privileges to users, thereby automatically granting
or revoking privileges. You can either create Roles or use the system roles pre-defined by
oracle.
Some of the privileges granted to the system roles are as given below:
Creating Roles:
The Syntax to create a role is:
For example: To create a role called "developer" with password as "pwd",the code will
be as follows
It's easier to GRANT or REVOKE privileges to the users through a role rather than
assigning a privilege direclty to every user. If a role is identified by a password, then,
when you GRANT or REVOKE privileges to the role, you definetely have to identify it
with the password.
Second, grant a CREATE TABLE privilege to the ROLE testing. You can add more
privileges to the ROLE.
To revoke a CREATE TABLE privilege from testing ROLE, you can write: