Académique Documents
Professionnel Documents
Culture Documents
Altering Tables
Alteration in Table Structure After you have created the tables, there may be
time when you want to change the structure of
the table either you want to add a column or
Presented By change the definition of the existing column .we
Dr. Parteek Bhatia can do this with the help of ALTER TABLE
Sr. Lecturer statement . To alter a table, the table must be
CSED
contained in your schema, or you must have either
Thapar University, Patiala
the ALTER object privilege for the table or the
ALTER ANY TABLE system privilege. A table in
an Oracle database can be altered for the
following reasons:
m
er as
co
eH w
o.
Altering Tables
Altering Tables
rs e
ou urc
• to add or drop one or more columns to or from the
table
You can increase the length of an existing
• to add or modify an integrity constraint on a table
column. However, you cannot decrease it unless
• to modify an existing column's definition (datatype, there are no rows in the table.
length, default value, and NOT NULL integrity
constraint) Furthermore, if you are modifying a table to
o
• to drop integrity constraints associated with the operation and may require substantial additional
vi y re
https://www.coursehero.com/file/38600201/SQL-Alteration-in-Table-Structurepdf/
1
8/31/2015
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
sh is
Syntax:
Th
or
SQL> Alter table emp
add foreign key (deptno) REFERENCES dept(deptno) ;
https://www.coursehero.com/file/38600201/SQL-Alteration-in-Table-Structurepdf/
2
8/31/2015
To MODIFY Columns
m
er as
co
eH w
o.
To MODIFY Columns To MODIFY Columns
rs e
ou urc
The following list details the allowable The Modify option changes the following of
operations that decrease various aspects of the the existing column
database: • Datatype
•Reducing the number of columns in a table • Column width
o
https://www.coursehero.com/file/38600201/SQL-Alteration-in-Table-Structurepdf/
3
8/31/2015
This option removes the constraints from a table. When a Alter table command can also be used to drop the existing
constraint is dropped, any associated index with that
columns of the tables, but this option is available only in
constraint (if there is one) is also dropped.
To remove constraints Oracle 8i or versions after that.
The syntax is:
Alter table < tablename > Syntax:
[DROP <constraints > …………….] ;
ALTER TABLE <table_name> DROP COLUMN
• Remove the primary key constraint from the emp
table. <column_name>;
SQL> Alter table emp
DROP primary key; Example
• Remove the constraint on the deptno field of the • Drop the column ename of emp table.
dept table.
Alter table dept
DROP constraint pk_deptno ; SQL> ALTER TABLE emp DROP COLUMN ename;
m
er as
co
eH w
o.
rs e
ou urc
To remove Tables However, dropping tables may not always be that
In order to delete a table from the database, the easy. Recall from the earlier lesson that when
drop table command must be executed. you disable constraints like primary keys that
o
https://www.coursehero.com/file/38600201/SQL-Alteration-in-Table-Structurepdf/
4
8/31/2015
m
er as
co
eH w
o.
rs e
ou urc
References
Kalyani Publishers
aC s
By
vi y re
Parteek Bhatia
ed d
ar stu
sh is
Th
https://www.coursehero.com/file/38600201/SQL-Alteration-in-Table-Structurepdf/
5
Powered by TCPDF (www.tcpdf.org)