0% found this document useful (0 votes)
157 views10 pages

Class01 DDL DML TCL

The document provides notes from a meeting discussing SQL. Key points include: 1) A live SQL practice tool is provided as some may not have their own tool. 2) The main types of SQL statements - DDL, DML, TCL - are explained along with examples like COMMIT, ROLLBACK, SAVEPOINT. 3) Data types like VARCHAR2 and NUMBER are demonstrated along with syntax examples for INSERT, SELECT, and altering tables by adding or dropping columns.

Uploaded by

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

Class01 DDL DML TCL

The document provides notes from a meeting discussing SQL. Key points include: 1) A live SQL practice tool is provided as some may not have their own tool. 2) The main types of SQL statements - DDL, DML, TCL - are explained along with examples like COMMIT, ROLLBACK, SAVEPOINT. 3) Data types like VARCHAR2 and NUMBER are demonstrated along with syntax examples for INSERT, SELECT, and altering tables by adding or dropping columns.

Uploaded by

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

Please find the below notes from all what we discussed in our today's 

meeting. We will meet again on


Friday 3rd Sep 7:15 AM IST. Notes will be provided after every class.

----------------------------------------------------------------------------------------

For live sql practice, if you do not have tool:

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

COMMIT: Commit command is used to permanently save any transaction


            into the database.
ROLLBACK: This command restores the database to last committed state.
            It is also used with savepoint command to jump to a savepoint
            in a transaction.
SAVEPOINT: Savepoint command is used to temporarily save a transaction so
            that you can rollback to that point whenever necessary.

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:

create table employee(


EMPID number,
EMPNAME varchar2(10),
age number,
sex char(4)
);
varchar is for string.  
number is for number.

  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);

insert into employee values (1,'hsgsggs',100,'M');

select * from employee;  * means we want to see all data of table.

If I am trying to insert values more than 10 character like:

insert into employee values (1,'hsgsyeyyeetggs',100,'M');

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.

Difference between char and varchar ?


The short answer is: VARCHAR is variable length, while CHAR is fixed length. CHAR is a fixed length
string data type, so any remaining space in the field is padded with blanks. CHAR takes up 1 byte
per character. ... VARCHAR is a variable length string data type, so it holds only the characters
you assign to it.

Prefer to use varchar2

Difference between char, varchar and VARCHAR2 in Oracle :


Sno Char VarChar/VarChar2

1 Char stands for “Character” VarChar/VarChar2 stands for Variable Character

2 It is used to store character string of fixed length It is used to store character string of variable length

It has a Maximum Size of 


3 2000 Bytes It has a Maximum Size of 4000 Bytes  

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. 

6 It is Static Datatype(i.e Fixed Length) It is Dynamic Datatype(i.e Variable Length)

7 It can lead to memory wastage  It manages Memory efficiently

8 It is 50% much faster than VarChar/VarChar2  It is relatively slower as compared to Char

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:

Insert Into <Tablename> Values ();

insert into employee values (1,'vdffd','F');


image.png

--> The number of columns in table = the number of values we passed to insert [else it gives error
'not enough values']

insert into employee (empid,empname) values (5,'vdffd')


 --> so if the situation is to insert less values, we have to specify the column names too.

Add Column:
--> add a column in table
alter table <table name > add <column nmame> <data type>;

alter table employee add address varchar2(100);

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>;

alter table employee add address_type varchar2(20) Default 'Residential';


If you want to add more than one column, you can list them out in the add clause:

alter table <tablename> add (


  column1    datatype,
  column2  datatype
);

=========================================================
DROP COLUMN

  alter table <tablename> drop column <column name >  

alter table employee drop column ADDRESS;

/* Advance concepts Good for 6+ years exp */

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!

A better option is to set the column unused:

alter table <tablename> set unused column <columnname>;

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:

ALTER TABLE <tablename> DROP UNUSED COLUMNS;

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:

I have a table Test and that table have id column.

Step 1:  Inserted records with id = 1


Step 2: Inserted records with id = 2
Step 3: Create Index on another table Test2
Step 4: Rollback;

How many records in table test as we did the rollback.

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.

You might also like