Sunteți pe pagina 1din 3

Datatypes - NEW

TIMESTAMP VARIATIONS -is an extension of the DATE data type. It stores the year,
month and day of the DATE datatype, plus hour, minute, seconds and fractional second values. CREATE TABLE new_table (employee_id NUMBER(5), first_name VARCHAR2(10), ... start_date TIMESTAMP(7)); DD-MON-YY 12.00.00.000000 start_date TIMESTAMP(7)WITH TIME ZONE); DD-MON-YY 12.00.00.000000 -5.00 start_date TIMESTAMP(7) WITH LOCAL TIME ZONE); INTERVAL '312-2' YEAR(3) TO MONTH); INTERVAL '312-2' YEAR(3) INTERVAL '300' MONTH(3)); Converting a character string to a timestamp: SELECT TO_TIMESTAMP ('1999-12-01 11:00:00', 'YYYY-MM-DD HH:MI:SS')

FROM DUAL; 01-DEC-99 11.00.00.000000000 AM

CREATE TABLE Statement

To create a table, a user must have CREATE TABLE privilege and a storage area in which to create objects. This privilege is given via the database administrator using DCL schema -who owns the table (a schema is a collection of objects that directly refer to the data in a database. Schema objects include: tables, views, synonyms, sequences, stored procedures, indexes, clusters and database links table - name of table column -name of each column datatype - the column's datatype and length DEFAULT expr -any default values not incl. in INSERT statement

1.

CREATE TABLE locations


(location_id NUMBER(4), location_name VARCHAR2(15), location_city VARCHAR2(15), .....);

DESCRIBE locations -- this statement will confirm the creation of the table 2. CREATE TABLE USING THE "AS SUBQUERY" table use another table to create a new

CREATE TABLE foreign_locations AS SELECT location_id, city .... FROM locations WHERE city IN ( 'Roma', 'London', 'Tokyo');

Create Table continued.... CREATE TABLE finance_department AS SELECT employee_id, first_name, last_name, job_id,
salary*12 ANNSAL, hire_date FROM employees WHERE department_id = 50;

Expressions must have an alias

CREATE TABLE new_without_data


(SELECT * FROM original_table WHERE 1 = 2)

ALTER TABLE Statement for columns


ADD a new column, MODIFY an existing column, define a DEFAULT value, DROP COLUMN ALTER TABLE animal ADD owner_lname VARCHAR2(15);
**you cannot specify where the column should appear

ALTER TABLE animal MODIFY (owner_lname VARCHAR2(15) DEFAULT none );


**datatype can be changed only if the column contains NULL values **size changes include increase width or precision of numeric and character columns, **decreases in column width only if the column contains only null values or if the table has no rows **default changes only affect subsequent insertions in the table **VARCHAR2 to CHAR type changes only if the column contains null values or if you do not change the size

ALTER TABLE animal DROP COLUMN owner_lname; **only one column at a time is dropped
** the table must have at least one column remaining in it after it is altered **once a column is dropped, it cannot be recovered **any other columns is the table that are marked SET UNUSED option are dropped, too.

SET UNUSED OPTION


The SET UNUSED option marks one or more columns as unused so that they can be dropped when the demand on system resources is lower. Specifying this clause does not actually remove the target columns from the table. 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 and a query to DESCRIBE the table will not show these columns. To reclaim the extra disc space use the DROP UNUSED COLUMNS command. ALTER TABLE animal2 SET UNUSED (owner_fname); ALTER TABLE animal2 DROP UNUSED COLUMNS;

DROPPING A TABLE
Dropping a table removes the definition of an Oracle table. The database loses all the data in the table and all the indexes associated with it. All data is deleted from the table, any views and synonyms remain but are invalid, all pending transactions are committed. Only the creator of the table or a user with DROP ANY TABLE privilege can remove a table. This procedure once executed, is irreversible. As all DDL, it is automatically committed. Oracle Server does NOT question the action! DROP TABLE animal2

CHANGING THE NAME OF A TABLE, VIEW, SEQUENCE OR SYNONYM


Only the owner of the object can execute the RENAME command. RENAME animal TO animal2

TRUNCATE A TABLE
Truncate a table is used to remove all rows from a table and to release the storage space used by that table. Truncate is a faster way to remove rows because it generates NO rollback information, does not fire the delete triggers of the table. If the table is the parent of a referential integrity constraint, you cannot truncate the table. Disable the constraint before issuing the TRUNCATE statement. You can also use the DELETE statement to remove a table but the DELETE does not release storage space.

COMPARISON OF DELETE AND TRUNCATE FOR ELIMINATING ROWS

DELETE
DML generates rollback because DML statements require commit uses WHERE clause can be used with SUBQUERY does not ask for confirmation can use ON DELETE CASCADE if set in column definition

TRUNCATE
DDL generates NO rollback because DDL statements are automatically committed faster does not fire the table delete triggers does not ask for confirmation table definition is left intact (indexes, triggers, and constraints)

S-ar putea să vă placă și