Alter table statements in Oracle

The general structure of the create table statement is
create table tablename(fieldname typename,fieldname typename,….)

create table books(bookname varchar(50), subject varchar(50))


To view the table write desc tablename.

To add a new field.

alter table books add price int

alter table books modify price number(5,2)

alter table books add constraint bookpk primary key(bookname)

Rename a column

alter table books rename column price to rate


Rename the table.

alter table books rename to bookdetails

Add a foreign key

ALTER TABLE books
ADD CONSTRAINT fkbooks
FOREIGN KEY (bookname) REFERENCES books(bookname)

 

alter table bookdetails drop constraint bookpk
alter table bookdetails drop column rate
drop table bookdetails

Leave a Reply