Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Oracle ALTER TABLE operator

18 August 2020

Oracle ALTER TABLE operator

The Oracle ALTER TABLE statement is used to add a column, change a column, delete a column, rename a column or rename a table (with syntax, examples and practical exercises).

The Oracle/PLSQL ALTER TABLE statement is used to add, modify or delete a column in a table. The Oracle/PLSQL ALTER TABLE operator is also used to rename tables.

Add a column to a table (ALTER TABLE)

ALTER TABLE syntax for adding a column to an Oracle table:

ALTER TABLE table_name
ADD column_name column-definition;

Let’s take an example of how to add a column to an Oracle table using the ALTER TABLE operator.
For example:

ALTER TABLE customers
ADD customer_name varchar2(45);

In this example, the ALTER TABLE operator will add a customer_name column to the customers table.

Adding multiple columns to the table

ALTER TABLE syntax to add multiple columns to an existing Oracle table:

ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,

column_n column_definition);

Consider an example that shows how to add multiple columns to an Oracle table using the ALTER TABLE operator. For example:

ALTER TABLE customers
ADD (customer_name varchar2(45),
city varchar2(40));

In this example, ALTER TABLE will add two columns, customer_name varchar2 (45) and city varchar2 (40).

Change the column in the table

ALTER TABLE syntax for changing a column in an existing Oracle table:

ALTER TABLE table_name
MODIFY column_name column_type;

Consider an example that shows how to change a column in an Oracle table using the ALTER TABLE operator.
For example:

ALTER TABLE customers
MODIFY customer_name varchar2(100) not null;

In this example, ALTER TABLE will modify the customer_name column to VARCHAR2(100) data type and indicate that the column does not accept NULL values.

Modifying several columns in a table

ALTER TABLE syntax for editing multiple columns in an existing Oracle table:

ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,

column_n column_type);

Consider an example that shows how to change multiple columns in an Oracle table using the ALTER TABLE operator. For example:

ALTER TABLE customers
MODIFY (customer_name varchar2(100) not null,
city varchar2(75));

In this example, ALTER TABLE modifies the customer_name and city columns.

Removing a column from a table

ALTER TABLE syntax to delete a column in an existing Oracle table:

ALTER TABLE table_name
DROP COLUMN column_name;

Consider an example that shows how to remove a column from an Oracle table using the ALTER TABLE operator. For example:

ALTER TABLE customers
DROP COLUMN customer_name;

In this example, Oracle/PLSQL ALTER TABLE will remove the customer_name column from the customers table.

Rename the column in the table

As of Oracle 9i Release 2, you can rename the column. ALTER TABLE syntax to rename a column in an existing Oracle table:

ALTER TABLE table_name
RENAME COLUMN old_name to new_name;

Consider an example that shows how to rename a column in an Oracle table using the ALTER TABLE operator. For example:

ALTER TABLE customers
RENAME COLUMN customer_name to cname;

In this example, Oracle/PLSQL ALTER TABLE renames the column customer_name to cname.

Rename the table

ALTER TABLE syntax for renaming the Oracle table:

ALTER TABLE table_name
RENAME TO new_table_name;

Consider an example that shows how to rename a table in Oracle using the ALTER TABLE operator. For example:

ALTER TABLE customers
RENAME TO contacts;

In this example, Oracle/PLSQL ALTER TABLE renames the customer table to contacts.

Practical Exercise #1

Based on the departments table below, rename the departments table as depts.

CREATE TABLE
( department_id number(10) not null,
Department_name varchar2(50) not null,
CONSTRAINT departments_pk PRIMARY KEY (department_id)
);

The solution for practical exercise #1:

The next Oracle/PLSQL ALTER TABLE operator will rename the table as depts:

ALTER TABLE
RENAME TO depts;

Practical Exercise #2

Based on the table below, add a column with the bonus name and data type number (6).

CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
department_id number(10),
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);

The solution for practical exercise #2:

The next Oracle/PLSQL ALTER TABLE operator will add a bonus column to the employees table:

ALTER TABLE employees
ADD bonus number(6);

Practical Exercise #3

Based on the table below, add two columns. One column with the name contact_name and data type VARCHAR2 (50), the second column with the name last_contacted and data type DATE.

CREATE TABLE customers
( customer_id number(10) not null,
customer_name varchar2(50) not null,
address varchar2(50),
city varchar2(50),
state varchar2(25),
zip_code varchar2(10),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);

The solution for practical exercise #3:

The next Oracle/PLSQL ALTER TABLE operator will add the contact_name and last_contacted columns to the customers table:

ALTER TABLE customers
ADD (contact_name varchar2(50),
last_contacted date);

Practical Exercise #4

Based on the employees table below, modify the employee_name column data type to varchar2 (80).

CREATE TABLE employees
( employee_number number(10) not null,
employee_name >varchar2(50) not null,
department_id number(10),
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);

The solution for practical exercise #4:

The next Oracle/PLSQL ALTER TABLE operator modifies the data type for the employee_name column in varchar2 (80):

ALTER TABLE employees
MODIFY employee_name varchar2(80);

Practical Exercise #5

Based on the table below, modify the customer_name column so that it cannot take NULL values and change the data type in the state column to varchar2 (2).

CREATE TABLE customers
( customer_id number(10) not null,
customer_name varchar2(50),
address varchar2(50),
city varchar2(50),
state varchar2(25),
zip_code varchar2(10),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);

The solution for practical exercise #5:

The following Oracle/PLSQL ALTER TABLE operator modifies the customer_name and state columns in the customers table:

ALTER TABLE customers
MODIFY (customer_name varchar2(50) not null,
state varchar2(2));

Practical Exercise #6

Based on the table below, remove the column.

CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);

The solution for practical exercise #6:

The next Oracle/PLSQL ALTER TABLE operator will remove the salary column from the employees table:

ALTER TABLE employees
DROP COLUMN salary;

Practical Exercise #7

Based on the table below, rename the column department_name to dept_name.

CREATE TABLE
( department_id number(10) not null,
Department_name varchar2(50) not null,
CONSTRAINT departments_pk PRIMARY KEY (department_id)
);

The solution for practical exercise #7

The next Oracle/PLSQL ALTER TABLE operator will rename the department_name column to dept_name in the departments table:

ALTER TABLE
RENAME COLUMN department_name to dept_name;

Oracle – SQL – Alter Table

 
Tags: , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...