Site icon IT Tutorial

Oracle Alter Table Add Column

I will explain Oracle Alter Table Add Column in this post.

 

Alter Table Add Column in Oracle

Mostly we are using the add column to the existing table. To add new column to the existing table, you should use the alter table add column command as follows.

 

ALTER TABLE table_name
  ADD (column_1 column_definition,
       column_2 column_definition,
       column_3 column_definition,
       ...
       column_n column_definition);



You can add new column as follows.

ALTER TABLE People ADD DateOfBirth date;

You can add new column, drop existing column and modify column as follows.

SQL> alter table msdeveci.mytest add city varchar2(24);

Table altered.

ALTER TABLE msdeveci.mytest ADD (name varchar2(30), city varchar2(50) DEFAULT 'Istanbul');




 

 

You can drop an existing column as follows.

ALTER TABLE People DROP COLUMN DateOfBirth;

You can rename column name as follows using alter command.

 alter table hr.employees rename column employee_id to emp_id;

 

You can convert table into read-only and read write mode as follows.

alter table hr.employees read only;


SQL> alter table msdeveci.mytest read only;

Table altered.

SQL> alter table msdeveci.mytest read write;

Table altered.

SQL>

 

You can rename an existing table as follows.

alter table hr.employees rename to hr.myemployee;

 

You can add new partition to existing table as follows.

alter table hr.employees add partition part values (‘G3_201407_07');

 

You can add primary key or constraint as follows.

ALTER TABLE HR.EMPLOYEES ADD (CONSTRAINT EMP_EMP_ID_PK PRIMARY KEY (EMP_ID));

 

You can disable and enable existing contraint as follows.

 ALTER TABLE HR.DEPARTMENTS DISABLE CONSTRAINT DEPT_LOC_FK;


ALTER TABLE HR.DEPARTMENTS ENABLE CONSTRAINT DEPT_LOC_FK;

 

You can rename an existing table as follows.

SQL> alter table msdeveci.test_table rename to mytest;   

Table altered.

SQL>

 

You can add new column, drop existing column and modify column as follows.

SQL> alter table msdeveci.mytest add city varchar2(24);

Table altered.

SQL> alter table msdeveci.mytest modify city varchar2(36);

Table altered.



SQL> alter table msdeveci.mytest drop column city;

Table altered.

SQL>

 

 

Dou want to learn Oracle SQL Tutorial for Beginners, then read the following articles.

Oracle SQL Tutorials For Beginners – Learn Oracle SQL from scratch with Oracle SQL Online Course

Exit mobile version