
Using the date Datatype
Use the DATE datatype to store point-in-time values (dates and times) in a table. The DATE datatype stores the century, year, month, day, hours, minutes, and seconds.
Oracle uses its own internal format to store dates. Date data is stored in fixed-length fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second.
For input and output of dates, the standard Oracle default date format is DD-MON-YY. For example:
'13-NOV-92'
To change this default date format on an instance-wide basis, use the NLS_DATE_FORMAT parameter. To change the format during a session, use the ALTER SESSION statement. To enter dates that are not in the current default date format, use the TO_DATE function with a format mask. For example:
TO_DATE ('November 13, 1992', 'MONTH DD, YYYY')
If the date format DD-MON-YY is used, then YY indicates the year in the 20th century (for example, 31-DEC-92 is December 31, 1992). If you want to indicate years in any century other than the 20th century, then use a different format mask, as shown above.
Time is stored in 24-hour format #HH:MM:SS. By default, the time in a date field is 12:00:00 A.M. if no time portion is entered. In a time-only entry, the date portion defaults to the first day of the current month. To enter the time portion of a date, use the TO_DATE function with a format mask indicating the time portion, as in:
INSERT INTO Birthdays_tab (bname, bday) VALUES
('ANNIE',TO_DATE('13-NOV-92 10:56 A.M.','DD-MON-YY HH:MI A.M.'));
SQl standard propose addition form of date literal (that is date without time). That is also supported in Oracle. This form is the following:
DATE ‘YYYY-VV-DD’
Where
DATE – is a keyword
YYYY-MM-DD - date in the format
- YYYY – four digits of year
- MM – two digits of month$
- DD – two digits of day.
Examples: DATE ‘1968-01-17’, DATE ‘2010-01-28’
Ansi/iso Datatypes
You can define columns of tables in an Oracle database using ANSI/ISO datatypes. Oracle internally converts such datatypes to Oracle datatypes.
The ANSI datatype conversions to Oracle datatypes are shown in table bellow. The ANSI/ISO datatypes NUMERIC, DECIMAL, and DEC can specify only fixed-point numbers. For these datatypes, s defaults to 0.
ANSI SQL Datatype |
Oracle Datatype |
CHARACTER (n), CHAR (n) |
CHAR (n) |
NUMERIC (p,s), DECIMAL (p,s), DEC (p,s) |
NUMBER (p,s) |
INTEGER, INT, SMALLINT |
NUMBER (38) |
FLOAT (p) |
FLOAT (p) |
REAL |
FLOAT (63) |
DOUBLE PRECISION |
FLOAT (126) |
CHARACTER VARYING(n), CHAR VARYING(n) |
VARCHAR2 (n) |
Altering Tables
Syntax
Purpose
To alter the definition of a table. Alter a table in an Oracle database for any of the following reasons:
To add one or more new columns to the table.
To add one or more integrity constraints to a table.
To modify an existing column's definition (datatype, length, default value, and NOT NULL integrity constraint) .
To drop integrity constraints associated with the table.
To drop column.
Prerequisites
The table must be in your own schema, or you must have ALTER privilege on the table, or you must have ALTER ANY TABLE system privilege. For some operations you may also need the CREATE ANY INDEX privilege.
To enable a UNIQUE or PRIMARY KEY constraint, you must have the privileges necessary to create an index on the table. You need these privileges because Oracle creates an index on the columns of the unique or primary key in the schema containing the table.
Keywords and Parameters
The clauses described below have specialized meaning in the ALTER TABLE statement. For descriptions of the remaining keywords, see “Table creation”
schema |
is the schema containing the table. If you omit schema, Oracle assumes the table is in your own schema. | |||
table |
is the name of the table to be altered. | |||
ADD relational _properties
|
adds a column or integrity constraint. If you add a column, the initial value of each row for the new column is null. For a description of the keywords and parameters of this clause, see “Table creation”. If you previously created a view with a query that used the "SELECT *" syntax to select all columns from table, and you now add a column to table, Oracle does not automatically add the new column to the view. To add the new column to the view, re-create the view using the CREATE VIEW statement with the OR REPLACE clause. You cannot add a column with a NOT NULL constraint if table has any rows. | |||
column_constraint |
adds or removes a NOT NULL constraint to or from an existing column. You cannot use this clause to modify any other type of constraint using ALTER TABLE. See Lab2 to consult with constraint clause. | |||
table_constraint |
adds or modifies an integrity constraint on the table. See Lab2 to consult with constraint clause . | |||
MODIFY modify_column_options |
modifies the definition of an existing column. If you omit any of the optional parts of the column definition (datatype, default value, or column constraint), these parts remain unchanged.
| |||
column |
is the name of the column to be added or modified. The only type of integrity constraint that you can add to an existing column using the MODIFY clause with the column constraint syntax is a NOT NULL constraint, and only if the column contains no nulls. To define other types of integrity constraints (UNIQUE, PRIMARY KEY, referential integrity, and CHECK constraints) on existing columns, using the ADD clause and the table constraint syntax. | |||
datatype |
specifies a new datatype for an existing column. You can omit the datatype only if the statement also designates the column as part of the foreign key of a referential integrity constraint. Oracle automatically assigns the column the same datatype as the corresponding column of the referenced key of the referential integrity constraint. | |||
DEFAULT |
specifies a new default for an existing column. Oracle assigns this value to the column if a subsequent INSERT statement omits a value for the column. If you are adding a new column to the table and specify the default value, Oracle inserts the default column value into all rows of the table. The datatype of the default value must match the datatype specified for the column. The column must also be long enough to hold the default value. A DEFAULT expression cannot contain references to other columns. | |||
drop_constraint_clause |
drops an integrity constraint from the database. Oracle stops enforcing the constraint and removes it from the data dictionary. You can specify only one constraint for each drop_constraint_clause, but you can specify multiple drop_constraint_clauses in one statement. PRIMARY KEY drops the table's PRIMARY KEY constraint UNIQUE drops the UNIQUE constraint on the specified columns CONSTRAINT drops the integrity constraint named constraint CASCADE drops all other integrity constraints that depend on the dropped integrity constraint You cannot drop a UNIQUE or PRIMARY KEY constraint that is part of a referential integrity constraint without also dropping the foreign key. To drop the referenced key and the foreign key together, use the CASCADE clause. If you omit CASCADE, Oracle does not drop the PRIMARY KEY or UNIQUE constraint if any foreign key references it | |||
drop_column_clause |
lets you free space in the database by dropping columns you no longer need, or by marking them to be dropped at a future time when the demand on system resources is less. | |||
SET UNUSED |
marks one or more columns as unused. Specifying this clause does not actually remove the target columns from each row in the table (that is, it does not restore the disk space used by these columns). Therefore, the response time is faster than it would be if you execute the DROP clause. Unused columns are treated as if they were dropped, even though their column data remains in the table's rows. After a column has been marked as unused, you have no access to that column. A "SELECT *" query will not retrieve data from unused columns. In addition, the names and types of columns marked unused will not be displayed during a DESCRIBE, and you can add to the table a new column with the same name as an unused column. | |||
DROP |
removes the column descriptor and the data associated with the target column from each row in the table. If you explicitly drop a particular column, all columns currently marked as unused in the target table are dropped at the same time. When the column data is dropped:
Note: If a constraint also references a nontarget column, Oracle returns an error and does not drop the column unless you have specified the CASCADE CONSTRAINTS clause. If you have specified that clause, Oracle removes all constraints that reference any of the target columns | |||
DROP UNUSED COLUMNS
|
removes from the table all columns currently marked as unused. Use this statement when you want to reclaim the extra disk space from unused columns in the table. If the table contains no unused columns, the statement returns with no errors. | |||
column |
specifies one or more columns to be set as unused or dropped. Use the COLUMN keyword only if you are specifying only one column. If you specify a column list, it cannot contain duplicates. | |||
CASCADE CONSTRAINTS |
drops all referential integrity constraints that refer to the primary and unique keys defined on the dropped columns, and drops all multicolumn constraints defined on the dropped columns. If any constraint is referenced by columns from other tables or remaining columns in the target table, then you must specify CASCADE CONSTRAINTS. Otherwise, the statement aborts and an error is returned. | |||
INVALIDATE |
Note: Currently, Oracle executes this clause regardless of whether you specify the keyword INVALIDATE. Oracle invalidates all dependent objects, such as views, triggers, and stored program units. Object invalidation is a recursive process. Therefore, all directly dependent and indirectly dependent objects are invalidated. An object invalidated by this statement is automatically revalidated when next referenced. You must then correct any errors that exist in that object before referencing it. | |||
DROP COLUMNS CONTINUE |
continues the drop column operation from the point at which it was interrupted. Submitting this statement while the table is in a valid state results in an error. |
When altering the column definitions of a table, you can only increase the length of an existing column, unless the table has no records. You can also decrease the length of a column in an empty table. For columns of datatype CHAR, increasing the length of a column might be a time consuming operation that requires substantial additional storage, especially if the table contains many rows. This is because the CHAR value in each row must be blank-padded to satisfy the new column length.
If you change the datatype (for example, from VARCHAR2 to CHAR), then the data in the column does not change. However, the length of new CHAR columns might change, due to blank-padding requirements.
Altering a table has the following implications:
If a new column is added to a table, then the column is initially null. You can add a column with a NOT NULL constraint to a table only if the table does not contain any rows.
If a view or PL/SQL program unit depends on a base table, then the alteration of the base table might affect the dependent object, and always invalidates the dependent object.