Class01 DDL DML TCL
Class01 DDL DML TCL
----------------------------------------------------------------------------------------
https://livesql.oracle.com/apex/f?p=590:1:28733181935303::NO:::
<Create or account here and use this free tool with latest oracle version available here>
Types of SQL Statement :
DDL --> create, alter, drop, truncate --> No commit or rollback . It is autocommit
DML --> Insert, update, delete
TCL --> commit, rollback, savepoint
Question: I am altering a table and adding a column. Now I want to remove it. Can I do rollback?
No, DDL is autocommit, not required commit or rollback
Datatypes:
varchar2(10) --> 10 is the capacity of that column. it can maimum store the 10 character string.
--> syntax --> means how the programming language talk to system.
--> Insert syntax
--> Insert Into <tablename> Values(the value you want to insert);
it gives me error, because the capacity of empname column is 10 and we are trying to insert more
than 10 character strings.
varchar2(10) --> indicates the total capacity of this column is 10. we can not insert any data which
length is more than 10
--> In varchar2 columns we can insert number values , but in number columns we can not insert
varchar2 values
even we insert number values in varchar2 column, but oracle treated them as characters only. so if
we do where empname = 5, will give us error because empname column is varchar column and it expects
values in quotes, so where empname = '5' will work.
2 It is used to store character string of fixed length It is used to store character string of variable length
Char will pad the spaces to the right side to fill the length VarChar will not pad the spaces to the right side to fill the
4 specified during the Declaration length specified during Declaration.
Note : There is no difference between VarChar and VarChar2 in Oracle. However, it is advised not to use VarChar for
storing data as it is reserved for future use for storing some other type of variable. Hence, always use VarChar2 in place of
VarChar.
Hence it is advised to use Char datatype when the length of the character string is fixed and will not change in the future.
If the length of the character string is not fixed, then VarChar2 is preferred
-------------------------------------------------------------------------------
Insert Syantax:
--> The number of columns in table = the number of values we passed to insert [else it gives error
'not enough values']
Add Column:
--> add a column in table
alter table <table name > add <column nmame> <data type>;
if the table already has records, then if we add a new column then what happens.
the new column comes with a NULL value for old records.
if we want to add a new column and that column can not be null ?
alter table <tablename> add <column name > <datatype> default <value>;
=========================================================
DROP COLUMN
But beware!
This is an expensive operation. On tables storing millions of rows it will take a long time to run.
And it blocks other changes to the table. So your application may be unusable while this runs!
This is an instant operation. No matter how many rows are in the table, it will take the same amount
of time.
This is because it doesn't physically remove the columns from the database. It marks them as
unavailable. The data still exists. You just can't get to it!
If you're hoping to reclaim the space these columns used, you need to wipe them from the table. Do
this with the following command:
As you're now doing the work of deleting the data, this can take a long time. The key difference
here is dropping unused columns is non-blocking. Your application can continue to run as normal.
Whichever method you use, take care when removing columns. Dropping columns or setting them unused
are both one-way operations. There is no "undrop column" command. If you made a mistake, you'll have
to recover the table from a backup!
============================================================
One more question on DDL command:
Answer: 2 records as we inserted on Step 1 and step 2. because on Step 3 we fired DDL and this is
the property of a DDL command to do auto commit of everything what is happened before that DDL.