SQL Tutorial (Chap3)
SQL Tutorial (Chap3)
While creating a table we provide the basic information for each column together with their data
type and sizes.
For Example: If we want to create the employee table, the statement would be like
Name Valid?
Employee_details Yes
Employee details No – should not contain a blank space in between the name
Example:-
SQL>ALTER TABLE emp ADD na_column DATE;
Once the table is successfully altered then we will receive a message saying “Table altered”
Let’s check the “emp” table structure.
SQL>SELECT * FROM emp;
– Before we run the alter command lets first check the emp table description
SQL>DESC emp;
We can now observe that ENAME column data size is changed to 25.
Similarly we can decrease the precision of column but to decrease the precision, column should
be empty.
System does AUTO commit after the deletion and hence the data deleted cannot be rolled back.
Using Truncate we cannot delete partial records from the database. Once we run truncate
command, system will delete all the records permanently from the database.
Example:-
Once the table is successfully truncated then we will receive a message saying ‘Table Truncated’
– Lets run the emp table after truncate
SELECT * FROM emp;
At a later stage even if we wanted we will not be able to get the table or its data, as it is
permanently removed.
Example:-
Error at line 1:
ORA-00942: table or view does not exist
From Oracle 10g version on wards we can get back the table which we dropped using FLASH
BACK , here is the following syntax for that:
Example:-
Now if we try to use the SELECT command we can see the data from ‘emp’ table.
This process is similar like our recycle bin in our Operating System’s. It means from Oracle 10g
on wards the DROP command will not remove our table permanently it will store in Recycle bin.
So therefore if we want to delete the table permanently here is the following syntax which will
drop the table permanently and which cannot be retrieved back.
Example:-
SQL>DROP TABLE emp PURGE;
Now if we try to select the emp table then system throws an error like :
Error at line 1:
ORA-00942: table or view does not exist
Example:-
Once the table is successfully renamed then we will receive a message saying ‘Table renamed’.
– after rename if we try to access ‘emp’ table system will not recognize the table.
Output:-