0% found this document useful (0 votes)
9 views3 pages

Class-8 (CTAS, INSERT ALL, ALTER Command)

The document discusses different ways to create tables from existing tables and insert data into tables using Oracle SQL commands like CREATE TABLE AS SELECT, INSERT ALL, and ALTER TABLE. It covers copying table structure and data, changing column names, adding, removing and modifying columns, and inserting multiple records.

Uploaded by

Nihar Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Class-8 (CTAS, INSERT ALL, ALTER Command)

The document discusses different ways to create tables from existing tables and insert data into tables using Oracle SQL commands like CREATE TABLE AS SELECT, INSERT ALL, and ALTER TABLE. It covers copying table structure and data, changing column names, adding, removing and modifying columns, and inserting multiple records.

Uploaded by

Nihar Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Class-8(CTAS)

-------------
CTAS:(Create Table As Select)
------------------------------
> Using this method , we can create a new table based on the existing table

Create a new table which will copy the structure(not data) of the existing table
----------------------------------------------------------------------------------
Syntax
------
CREATE TABLE <Table Name> AS <Select Statement > and a <False Condition>

CREATE TABLE STUDENTS_BKP AS SELECT * FROM STUDENTS WHERE 1=2;--Table STUDENTS_BKP


created.

SELECT * FROM STUDENTS_BKP;

Create a new table which will copy the structure and data of the existing table
----------------------------------------------------------------------------------
Syntax
------
CREATE TABLE <Table Name> AS <Select Statement >

CREATE TABLE STUDENTS_BKP1 AS SELECT * FROM STUDENTS ;

SELECT * FROM STUDENTS_BKP1;

Create a new table with changing the column name


-------------------------------------------------
CREATE TABLE STUDENTS_BKP2(ROLL_NUMBER,STUDENT_NAME,AGE) AS SELECT * FROM STUDENTS;

SELECT * FROM STUDENTS_BKP2;

INSERT ALL
----------
> Using this command , we can insert multiple records into a table at a time
> It also inserts multiple records into multiple tables

Syntax
------
INSERT ALL
INTO <Table Name>(<Col 1>,<Col 2>,..,<Col n>) VALUES (<Val 1>,<Val 2>,...,<Val n>)
INTO <Table Name>(<Col 1>,<Col 2>,..,<Col n>) VALUES (<Val 1>,<Val 2>,...,<Val n>)
.....
INTO <Table Name>(<Col 1>,<Col 2>,..,<Col n>) VALUES (<Val 1>,<Val 2>,...,<Val n>)
SELECT * FROM DUAL;

INSERT ALL
INTO STUDENTS(ROLL_NO,SNAME,AGE) VALUES (1004,'Debendra',25)
INTO STUDENTS(ROLL_NO,SNAME,AGE) VALUES (1005,'Bikash',24)
INTO STUDENTS(ROLL_NO,SNAME,AGE) VALUES (1006,'Binaya',28)
SELECT * FROM DUAL;
--3 rows inserted.

COMMIT;

ALTER
-----
> It is used to change the structure of an existing table
OPerations of Alter command
----------------------------
(1) Adding a New column (Single/Multiple)
(2) Removing an Existing column (Single/Multiple)
(3) Modifing the size of a column (Single/Multiple)
(4) Modifing the data type of a Column (Single/Multiple)
(5) Renaming a column of a table (Single)
(6) Adding new constraint for a column
(7) Removing existing constraint
(8) Disabling Constraint
(9) Enabling Constraint

(1) Adding a New column (Single/Multiple)


------------------------------------------
> We need to use ADD clause with ALTER command to add single/multiple columns
Syntax
-------
ALTER TABLE <Table Name> ADD (<New Col 1> <Data type> (<Size>),
<New Col 2> <Data type> (<Size>),
...............................,
<New Col n> <Data type> (<Size>)
);

Q-1:Add Gender,Mob_no,Email_id columns to the STUDENTS table


A-1:ALTER TABLE STUDENTS ADD(GENDER CHAR(1),
MOB_NO VARCHAR2(13),
EMAIL_ID VARCHAR2(30)
);--Table STUDENTS altered.

DESC STUDENTS;
O/P
---
Name Null? Type
-------- -------- ------------
ROLL_NO NOT NULL NUMBER(4)
SNAME NOT NULL VARCHAR2(30)
AGE NUMBER
GENDER CHAR(1)
MOB_NO VARCHAR2(13)
EMAIL_ID VARCHAR2(30)

(2) Removing an Existing column (Single/Multiple)


--------------------------------------------------
> We need to use DROP clause with ALTER command to remove single/multiple columns
Syntax(For removing only one column)
-------------------------------------
ALTER TABLE <Table name> DROP COLUMN <Column Name>

Q-1:Remove EMAIL_ID column from STUDENTS table


A-1:ALTER TABLE STUDENTS DROP COLUMN EMAIL_ID;--Table STUDENTS altered.

DESC STUDENTS;
O/P
---
Name Null? Type
-------- -------- ------------
ROLL_NO NOT NULL NUMBER(4)
SNAME NOT NULL VARCHAR2(30)
AGE NUMBER
GENDER CHAR(1)
MOB_NO VARCHAR2(13)

Syntax(For removing multiple columns)


-------------------------------------
ALTER TABLE <Table Name> DROP (<Column Names>)

Q-1:Remove MOB_NO & GENDER column from STUDENTS table


A-1:ALTER TABLE STUDENTS DROP (MOB_NO,GENDER);--Table STUDENTS altered.

DESC STUDENTS;
O/P
---
Name Null? Type
-------- -------- ------------
ROLL_NO NOT NULL NUMBER(4)
SNAME NOT NULL VARCHAR2(30)
AGE NUMBER

(3) Modifing the size of a column (Single/Multiple)


---------------------------------------------------
> To increase/decrease the size of a column we need to use MODIFY clause along with
ALTER command

Syntax
------
ALTER TABLE <Table Name> MODIFY(<Col Name 1> <Data Type> (<Size>),
<Col Name 2> <Data Type> (<Size>),
.................................,
<Col Name n> <Data Type> (<Size>)
)

Q-1:Decrease the size of SNAME column from 30 to 5


A-1:ALTER TABLE STUDENTS MODIFY (SNAME VARCHAR2(5));--ORA-01441: cannot decrease
column length because some value is too big

Q-2:Increase the size of SNAME column from 30 to 35


A-2:ALTER TABLE STUDENTS MODIFY (SNAME VARCHAR2(35));--Table STUDENTS altered.

DESC STUDENTS;
O/P
---
Name Null? Type
------- -------- ------------
ROLL_NO NOT NULL NUMBER(4)
SNAME NOT NULL VARCHAR2(35)
AGE NUMBER

You might also like