Difference between revisions of "Oracle:DDL Alter"
(Created page with "== Making changes to existing tables == Once a table has been created you may wish to alter it to add new columns, amend the size of existing ones, or drop them completely. T...") |
|||
Line 8: | Line 8: | ||
<code> | <code> | ||
− | ALTER TABLE TABLENAME | + | ALTER TABLE TABLENAME MODIFY (COLUMN DEFINITION); |
− | |||
</code> | </code> | ||
Line 27: | Line 26: | ||
MODIFY (BUDGET NULL); | MODIFY (BUDGET NULL); | ||
− | You can not do the reverse if some of the rows in the column affected contain null values. | + | You can not do the reverse if some of the rows in the column affected contain null values. |
+ | |||
+ | |||
+ | == Exercise 3.5 == | ||
+ | |||
+ | Why would this command fail: | ||
ALTER TABLE EMP | ALTER TABLE EMP | ||
Line 38: | Line 42: | ||
<code> | <code> | ||
− | ALTER TABLE TABLENAME | + | ALTER TABLE TABLENAME ADD (COLUMN DEFINITION) |
− | |||
</code> | </code> | ||
Line 52: | Line 55: | ||
All fields in the new column will be initially set to NULL. To make a column contain only non-NULL values: | All fields in the new column will be initially set to NULL. To make a column contain only non-NULL values: | ||
− | + | # Add the new column | |
− | + | # Add values to every field, so there are no non-NULL values | |
− | + | # Alter the table to make the column NOT-NULL | |
+ | |||
+ | A business rule is that all projects must have a manager, so the MGRPROJNO column should really be set to NOT NULL. To do this we need to update the projects so they now have a manager. | ||
+ | |||
+ | First check how many projects there are: | ||
+ | |||
+ | SELECT * FROM PROJ; | ||
+ | |||
+ | Your results may vary depending on whether you have carried out the previous exercises: | ||
+ | |||
+ | <pre style="color: blue"> | ||
+ | PROJNO PNAME BUDGET MGRPROJNO | ||
+ | ---------- -------------------------------- ---------- ---------- | ||
+ | 1 ORACLE VERSION 10 15000 | ||
+ | 2 ORACLE DEVELOPER 10000 | ||
+ | 3 ORACLE VERSION 12C 8000 | ||
+ | </pre> | ||
+ | |||
+ | Assuming you have the above 3 records, now update the MGRPROJNO column (amend to match your own data). | ||
+ | |||
+ | Update the PROJ table so that BLAKE is the manager of project 1: | ||
+ | |||
+ | UPDATE proj SET mgrprojno = 7698 WHERE projno = 1; | ||
+ | |||
+ | Update the PROJ table so that JONES is the manager of project 2: | ||
+ | UPDATE PROJ SET mgrprojno = 7566 WHERE projno = 2; | ||
+ | |||
+ | == Exercise 3.6 == | ||
+ | |||
+ | Update the PROJ table so that CLARK is the manager of project 3. | ||
+ | |||
+ | UPDATE proj SET mgrprojno = 7782 WHERE projno = 3; |
Revision as of 15:18, 2 March 2016
Making changes to existing tables
Once a table has been created you may wish to alter it to add new columns, amend the size of existing ones, or drop them completely. This is done using the ALTER TABLE command.
Altering Columns
Existing columns in a table can be altered by using the MODIFY clause:
ALTER TABLE TABLENAME MODIFY (COLUMN DEFINITION);
For example, to alter the BUDGET column of PROJ to accept numbers up to nine digits:
ALTER TABLE PROJ MODIFY (BUDGET NUMBER(9,2));
The system will respond with: - Table altered
Note, there are restrictions on altering an existing column. The width of a column can be decreased and the data type changed, but only if the column contains NULL values. If there is existing data in the column, the only change allowed is to increase the width.
A column can be changed from being NOT NULL to allowing NULL values, by adding the NULL clause at the end of the column specification. For example :
ALTER TABLE PROJ MODIFY (BUDGET NULL);
You can not do the reverse if some of the rows in the column affected contain null values.
Exercise 3.5
Why would this command fail:
ALTER TABLE EMP MODIFY (COMM NOT NULL);
Adding a Column
Extra columns can be added to a table using the ADD clause:
ALTER TABLE TABLENAME ADD (COLUMN DEFINITION)
For example, add a column MGRPROJNO to the table PROJ (the manager of the project):
ALTER TABLE PROJ ADD (MGRPROJNO NUMBER constraint fk_empmgr REFERENCES EMP(empno));
The system replies with: - Table altered.
All fields in the new column will be initially set to NULL. To make a column contain only non-NULL values:
- Add the new column
- Add values to every field, so there are no non-NULL values
- Alter the table to make the column NOT-NULL
A business rule is that all projects must have a manager, so the MGRPROJNO column should really be set to NOT NULL. To do this we need to update the projects so they now have a manager.
First check how many projects there are:
SELECT * FROM PROJ;
Your results may vary depending on whether you have carried out the previous exercises:
PROJNO PNAME BUDGET MGRPROJNO ---------- -------------------------------- ---------- ---------- 1 ORACLE VERSION 10 15000 2 ORACLE DEVELOPER 10000 3 ORACLE VERSION 12C 8000
Assuming you have the above 3 records, now update the MGRPROJNO column (amend to match your own data).
Update the PROJ table so that BLAKE is the manager of project 1:
UPDATE proj SET mgrprojno = 7698 WHERE projno = 1;
Update the PROJ table so that JONES is the manager of project 2:
UPDATE PROJ SET mgrprojno = 7566 WHERE projno = 2;
Exercise 3.6
Update the PROJ table so that CLARK is the manager of project 3.
UPDATE proj SET mgrprojno = 7782 WHERE projno = 3;