oracle sql and dba tutorial logo

Tutorial for Oracle DBA


Oracle DBA Interview Questions

Most asked Oracle DBA Interview Questions.

60 Technical Questions

42 Backup & Recovery Questions

Unix For Oracle DBA 20 Questions


 

Oracle SQL Tutorial Contents

Introduction to Databases

CODD'S Rules

Datatypes and Create Table

Oracle SELECT Statement

Formatting in SQL*Plus

UNION, INTERSECT, MINUS Operators and Sorting Query Result

 

Oracle ALTER TABLE ADD COLUMN

Oracle ALTER TABLE MODIFY COLUMN

Oracle SQL Functions

Oracle NVL() Function with Examples

Oracle SUBSTR() function with Examples

Oracle TO_DATE() with complete examples

Oracle DECODE function

Oracle INSTR() function with examples

Oracle TO_CHAR() function

Oracle TO_TIMESTAMP

Number Functions (Math Functions)

Character Functions

Miscellaneous Functions

Aggregate Functions

Date and Time Functions

Oracle Join Queries

GROUP BY Queries, SUB Queries

CUBE, ROLLUP Functions

Oracle DML (INSERT, UPDATE, DELETE...)

Oracle DDL (CREATE, ALTER, DROP...)

COMMIT, ROLLBACK,SAVEPOINT

Data Control Language (GRANT, REVOKE)

 

 

Integrity Constraints (PRIMARY KEY, NOT NULL...)

DEFAULT Values

Dropping Constraints

Disabling and Enabling

Differing Constraints Check

View Info about Constraints

Working with Dates

Oracle Views

Oracle Sequences

Oracle Synonyms

Indexes and Clusters

Table Partitioning

Altering Partition Tables

Dropping Partitions

Merging Partitions

Splitting Partitions

Coalescing Partitions

Oracle Objects and Object Types

 

 

 

 

 

 

 

 

 

Oracle ALTER TABLE ADD COLUMN explained with examples

We use ALTER TABLE ADD COLUMN command to add columns to an existing table. Using this command we can add a single column or multiple columns at once. We can even specify NOT NULL clause as well as DEFAULT clause.

You can add columns to an table using  ALTER TABLE command only if you are the owner of the table. You can modify structure of other users table only if the table's owner has granted you the permission to modify structure of his tables. Otherwise Oracle will return insufficient privileges error.

EXAMPLES

How to add a single column to an existing table.

Let's say suppose we have a table by name "emp" with the following structure.

emp table for altering

Now we want to add a column "city" to this table. To do that, we will issue the following command

alter table emp add city varchar2(20);

After issuing the above command the table structure will look like this

emp table structure after alter table

To fill the values in CITY column we need to use UPDATE command to set city values for different employees. Like

update emp set city='New York' where empno=7782
update emp set city='Dallas' where empno=7934

How to alter table add multiple columns to an existing table

If you want to add multiple columns to employee table you can give the following statement

For example, to add  addr,  pin, ph, fax to employees table we will give the following command

alter table emp add (addr varchar2(20),
      pin varchar2(10),ph varchar2(20));

How about adding columns with NOT NULL constraint

As we have seen above, when we add a column, the column will be empty and we need to issue UPDATE statement to fill the values in that column. So the question is, Can we add a column with NOT NULL constraint? The answer is yes, we can add a column with NOT NULL constraint,  but, there must 2 conditions which needs to be met 

The table must either be empty or if the table is not empty then, you must also specify the DEFAULT value.

If you give the following command when table is not empty, you will get the following error

alter table emp add (city varchar2(100) not null);

alter table error

However, if you also specify default value then it will succeed

alter table emp add (city varchar2(100) default "Paris' not null);

alter table with default value

Alter table adding a PRIMARY KEY column

You can also add a column to an existing table with PRIMARY KEY constraint only if the table is empty and if there is no PRIMARY KEY already existing in the table.

To add a column with PRIMARY KEY constraint, give the following command

alter table emp add (EmpID varchar2(20) constraint emp_pk primary key);

If you try to add a column with PRIMARY KEY constraint and if the table is not empty then, it will through the following error

error adding not null constraint

 or if there is already a column with primary key constraint then, Oracle will through the following error

error adding primary key column

How to ALTER TABLE MODIFY columns in Oracle

How to ALTER TABLE DROP column in Oracle

 

 

 

 


HomeContact Us

Data Loader

Data Loader is a simple yet powerful tool to
export and import Data between many common database formats


Forms Data Loader

Tool to load data into Oracle E-Business Suite R12 / Oracle Apps using Macros and Forms Record and Playback

Interface Computers Academy © 2007-2017 All Rights Reserved