Академический Документы
Профессиональный Документы
Культура Документы
1.- Renaming a Column: Example The following example renames the credit_limit
column of the sample table oe.customers to credit_amount:
CREATE TABLE t1 (
pk NUMBER PRIMARY KEY,
fk NUMBER,
c1 NUMBER,
c2 NUMBER,
CONSTRAINT ri FOREIGN KEY (fk) REFERENCES t1,
CONSTRAINT ck1 CHECK (pk > 0 and c1 > 0),
CONSTRAINT ck2 CHECK (c2 > 0)
);
Submitting the following statement drops column pk, the primary key constraint, the
foreign key constraint, ri, and the check constraint, ck1:
If all columns referenced by the constraints defined on the dropped columns are also
dropped, then CASCADE CONSTRAINTS is not required. For example, assuming that no
other referential constraints from other tables refer to column pk, then it is valid to
submit the following statement without the CASCADE CONSTRAINTS clause:
3.- Disabling a CHECK Constraint: Example The following statement defines and
disables a CHECK constraint on the employees table:
1
4.- We have a table PERSON with this structure:
Table altered.
The requirement:
To move the new column ADDRESS after NAME and before SAL i.e. at position #3,
shifting SAL to position #4.
The solution:
Table renamed.
Table created.
2
----------------- -------- ------------
ID NUMBER(3)
NAME VARCHAR2(15)
ADDRESS VARCHAR2(40)
SAL NUMBER(6)
5.- To ADD A COLUMN in a table, the Oracle ALTER TABLE syntax is:
Example
Let's look at an example that shows how to modify multiple columns in an Oracle table
using the ALTER TABLE statement.
For example:
3
7. - Rename column in table
To RENAME A COLUMN in an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
Example
Let's look at an example that shows how to rename a column in an Oracle table using
the ALTER TABLE statement.
For example:
This Oracle ALTER TABLE example will rename the column called customer_name to
cname.
Let's look at an example that shows how to rename a table in Oracle using the ALTER
TABLE statement.
For example:
This Oracle ALTER TABLE example will rename the customers table to contacts.
4
EJERCICIOS RESUELTOS
Based on the departments table below, rename the departments table to depts.
The following Oracle ALTER TABLE statement would rename the departments table
to depts:
Based on the employees table below, add a column called bonus that is a number(6)
datatype.
The following Oracle ALTER TABLE statement would add a bonus column to the
employees table:
Based on the customers table below, add two columns - one column called
contact_name that is a varchar2(50) datatype and one column called last_contacted that
is a date datatype.
5
);
Solution for Practice Exercise #3:
The following Oracle ALTER TABLE statement would add the contact_name and
last_contacted columns to the customers table:
The following Oracle ALTER TABLE statement would change the datatype for the
employee_name column to varchar2(75):
Based on the customers table below, change the customer_name column to NOT allow
null values and change the state column to a varchar2(2) datatype.
The following Oracle ALTER TABLE statement would modify the customer_name and
state columns accordingly in the customers table:
6
Practice Exercise #6:
The following Oracle ALTER TABLE statement would drop the salary column from
the employees table:
The following Oracle ALTER TABLE statement would rename the department_name
column to dept_name in the departments table:
Bibliografa:
https://www.techonthenet.com/oracle/index.php
https://www.techonthenet.com/mysql/tables/alter_table.php