0% found this document useful (0 votes)
41 views

SQL Alter Table Add (,, - . - . - . - . - . .) Table Altered

The ALTER statement in SQL is used to modify the structure of an existing table. It allows adding, dropping, and renaming columns. It can also modify column data types. Common operations include adding a new column, dropping an existing column, and renaming a column. The ALTER statement provides flexibility to modify table structures after the table is created.

Uploaded by

eswar090911
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

SQL Alter Table Add (,, - . - . - . - . - . .) Table Altered

The ALTER statement in SQL is used to modify the structure of an existing table. It allows adding, dropping, and renaming columns. It can also modify column data types. Common operations include adding a new column, dropping an existing column, and renaming a column. The ALTER statement provides flexibility to modify table structures after the table is created.

Uploaded by

eswar090911
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

ALTER

It is a DDL statement use to alter the column definitions of a existing table.

Using alter statement we can perform the following operations on a table.


(i) adding of new columns
(ii) dropping of existing columns
(iii) renaming of a column name
(iv) modifying of column data type

(i) adding of new columns:

syntax:

sql> alter table <table name> add


(
<column1 definition>,
<column2 definition>,
. . . . . . . . . . .
);

Table altered.

Note:
(i) By default a new column will added only at the end of the exiting columns.
(ii) By default the new column contains NULL.

eg:

SQL> desc emp


Name Null? Type
----------------------------------------------------- -------- ---------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

SQL> alter table emp add


2 (
3 tel_num varchar2(10)
4 );

Table altered.

SQL> desc emp


Name Null? Type
----------------------------------------------------- -------- -------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
TEL_NUM VARCHAR2(10)

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO TEL_NUM


---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO TEL_NUM


---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10

14 rows selected.

Eg:

SQL> alter table emp add


2 (
3 mobile number(10),
4 b_date date
5 );

SQL> desc emp


Name Null? Type
----------------------------------------------------- -------- -------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
TEL_NUM VARCHAR2(10)
MOBILE NUMBER(10)
B_DATE DATE

to the update the values:

SQL>update emp set


tel_num=’2356723445’,
mobile=987654321,
b_date=’03-dec-81’
where empno=7369;

1 row updated.

To insert a new record.

SQL>insert into emp


values(1001,'xyz','clerk',7566,sysdate,2500,null,20,'8373637993',985674321,'10-feb-80');

1 row created.

(ii) dropping of existing columns:

Single column dropping:

Sql> alter table <table name> drop column <column name>;

Table altered.

Multi column dropping:

Sql> alter table <table name> drop


(
<column name>,<column name>, <column name>,.. . . . . .
);
Table altered

Eg:

Note:
(i) columns can be dropped from any position in any order.
(ii) If a column is dropped automatically the data in that column is also deleted.

Eg:

SQL> alter table emp drop column comm;

Table altered.

SQL> alter table emp drop


2 (
3 job,b_date,sal,tel_num
4 );

Table altered.

SQL> desc emp


Name Null? Type
----------------------------------------------------- -------- ---------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
MGR NUMBER(4)
HIREDATE DATE
DEPTNO NUMBER(2)
MOBILE NUMBER(10)

SQL> alter table emp drop


2 (
3 mgr,mobile,hiredate
4 );

Table altered.

SQL> desc emp


Name Null? Type
----------------------------------------------------- -------- ---------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
DEPTNO NUMBER(2)

SQL> alter table emp drop


2 (
3 ename,deptno
4 );

Table altered.

SQL> desc emp


Name Null? Type
----------------------------------------------------- -------- ----------
EMPNO NOT NULL NUMBER(4)

SQL> alter table emp drop column empno;


alter table emp drop column empno
*
ERROR at line 1:
ORA-12983: cannot drop all columns in a table

SQL> select * from emp;

EMPNO
----------
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876

EMPNO
----------
7900
7902
7934

14 rows selected.

(iii) renaming of column name

syntax:

sql> alter table <table name> rename column <column name> to <new column name>;

table altered.

SQL> desc emp


Name Null? Type
----------------------------------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

SQL> alter table emp rename column sal to salary;

Table altered.

SQL> desc emp


Name Null? Type
----------------------------------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SALARY NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

NOTE:
Renaming the column name by using the select statement is possible from the version of
oracle 9i second release. If the column name is renamed the data type remains the same.

(iv) modifying of column data type

modifying the column data type is of 3 types.


(i) increase in the size of the column
(ii) decrease in the size of the column
(iii) complete change of data type
syntax:

sql> alter table <table name> modify


(
<column name> <new data type>,
<column name> <new data type>,
. . . . . . . . . . . . . . . .
);

Table altered.

(i) increase in the size of the data type.

SQL> desc emp


Name Null? Type
----------------------------------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

SQL> alter table emp modify


2 (
3 ename varchar2(15)
4 );

Table altered.

SQL> desc emp


Name Null? Type
----------------------------------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(15)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

Note: increase in the size of the column is independent of the data existing in the column.

(ii) decrease in the size of the column

SQL> desc emp


Name Null? Type
----------------------------------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

SQL> alter table emp modify


2 (
3 ename varchar2(7)
4 );

Table altered.

SQL> desc emp


Name Null? Type
----------------------------------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(7)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

Note: to decrease the size of the column the column should not contain any existing data
which are exceeding the specified size.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ------- --------- ---------- --------- ---------- ---------- ----------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10

14 rows selected.

SQL> alter table emp modify


2 (
3 ename varchar2(5)
4 );
ename varchar2(5)
*
ERROR at line 3:
ORA-01441: cannot decrease column length because some value is too big

(iii) complete change of data type

note: to change the data type of a column the column must be empty.

SQL> desc emp


Name Null? Type
----------------------------------------------------- -------- -------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(7)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

SQL> alter table emp modify


2 (
3 sal varchar2(8)
4 );
sal varchar2(8)
*
ERROR at line 3:
ORA-01439: column to be modified must be empty to change datatype

You might also like