You are on page 1of 7

what is diff b/w DDL and DML ?

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

in DDL is auto commit but DML is not auto commit which means its doesnot save the
values permanantly !!

The command of DML is not auto-committed that means it can't permanently save all
the changes in the database.

They can be rollback.

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

DDL COMMANDS:
------------
we can create , alterating and we can delete a table with help of DDL statement

CREATE
---------
Syntax: create table table_name(column_name data type);

RENAME
-------

Syntax: RENAME OLD_TABLE_NAME TO NEW_TABLE_NAME

copy existing data to new data


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

Syntax: CREATE TABLE NEW_TABLE_NAME AS SELECT * FROM OLD_TABLE_NAME

ALTER:�
------

To add a new column in the table


---------------------------------
Syntax: ALTER�TABLE�table_name�ADD�column_name�datatype;����

To modify existing column in the table:


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

Syntax: ALTER�TABLE�TABLE NAME MODIFY(COLUMN NAME NEW DATATYPE�);�

To drop the column in the table:


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

Syntax: alter table table_name DROP column column_name;

TRUNCATE:�
---------

Syntax: TRUNCATE�TABLE�table_name;

DROP:�
-----

Syntax: drop table table_name

----------------------------------------------------------------
DML COMMANDS:
-------------
Using DML commands we can modify the database.and then It is responsible for all
changes in the database

INSERT:�
-------

INSERT�INTO�TABLE_NAME Values(&col1,�&col2,�&col3,....�&col�N)��

UPDATE:�
--------

UPDATE�table_name�SET�[column_name1=�value1,...column_nameN�=�valueN]�[WHERE�CONDIT
ION]���

SQL> update Item set ProductStock=76 where ProductID=3;


1 row updated��

DELETE:�
-------

Syntax: DELETE�FROM�table_name�[WHERE�condition];�

Transaction Control Language


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

TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.
((These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.))

COMMIT:
-------

Syntax: COMMIT;��

ROLLBACK:
---------
Syntax: ROLLBACK;

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

Data Control Language


-----------------------
DCL commands are used to grant and take back authority from any database user.

Grant
------

The GRANT command is used for providing the authorization to the users and then
REVOKE command is used for withdrawing the authorization.

It allows us to access table from different schemas.

It used to give privileges to the group of users.

PRIVILEGE:
----------
Provides permission to users.

Types of Privileges are.....

* Object Privileges
* System Privileges

Object Privileges:
------------------

It allows to perform certain action on the database objects.


Example : INSERT, UPDATE, DELETE, SELECT, EXECUTE.

System privileges:
------------------

It allows to perform certain action with in the database.

Example : CREATE VIEW, CREATE TABLE, CREATE SESSION

PROVIDING AUTHORIZATION:

The GRANT command is used for providing the authorization to the users...some of
the grant commands are....

to log into the user...

SQL>grant create session to username ;

to create a table...

SQL>grant create table to username;

To give privileges on tablespace for 'USERS'...

SQL>>grant resource to username;

//grant list_of_privileges on object_name to user_name;

SQL>grant insert, update, delete, select on emp to username ;

///To insert into the table on otheruser...

SQL>insert into test.emp values(8,'nim') ///

________________________________________________________________

Prevent Permissions:
--------------------
REVOKE command is used for withdrawing the authorization.

SQL>revoke create session,create table from santhosh;

To Drop the username & the table:

drop user username cascade;


________________________________________________________________

Roles:
------

Role is an object created with set of privileges and password in order to maintain
user permissions effectively.

SQL>create role developer identified by peri123;

SQL>grant create session,create table to developer;

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

Pseudo column:
--------------

EX: Rowid
SQL> select name,rowid from mytable;
rownum:
SQL> select name,rowid,rownum from mytable;

DUAL:
----

Pre defined table

desc dual;

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

SINGLE ROW FUNCTIONS


--------------------
select chr(65) from dual;
select mod (24,4) from dual;
select last_day(sysdate) from dual;
select to_char(sysdate,'month yyyy dd' )from dual;

CONVERSION FUNCTION
--------------------

TO_DATE (): Converts the string to a DATE format.


-------------------------------------------------
Syntax: select to_date ('01/jan/2009', 'dd/mon/yyyy') from dual;

TO_CHAR(): Converts the datetime or number to a String format.


--------------------------------------------------------------
Syntax: select to_char(to_date('01/jan/2009', 'dd/mon/yyyy') )from dual;

DATE TIME FUNCTION


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

ADD_MONTHS
Syntax: select add_months('01-jan-2009', 3) from dual;

LAST_DAY(x):
Syntax: select next_day('01-jan-2009', 'Sunday')from dual;

ROUND:
select round (to_date ('01-dec- 2009'), 'yyyy') from dual;

age of a person in months


---------------------------
select round(MONTHS_BETWEEN(sysdate,to_date('09-JAN-01','DD-MON-YY')))||' Months'
from dual

the age of a person in Years :


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

select round(MONTHS_BETWEEN(sysdate,to_date('09-JAN-01','DD-MON-YY'))/12,1)||' Yrs'


from dual

SYSDATE ():
----------

Syntax: select sysdate from dual;

TRUNC:
select trunc (to_date ('06-jun-2009'), 'yyyy') from dual;

Changing the Session Time Zone:


-------------------------------
Syntax: alter session set time_zone = 'PST';
Syntax: select sessiontimezonefrom dual;

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

AGGREGATE FUNCTIONS � It can take many inputs and return sigle -------------------
output

Various Aggregate Functions:

1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()

GROUP BY
--------

Syntax:
-------
SELECT�column��
FROM�table_name��
WHERE�conditions���
GROUP�BY�column��
ORDER�BY�column�

examp:

SQL> select ProductName, sum(ProductCost) from Item group by ProductName;

SQL> select ProductName,count(*)from Product group by ProductName;

HAVING
------
Syntax:
SELECT�column1,�column2���
FROM�table_name��
WHERE�conditions���
GROUP�BY�column1,�column2���
HAVING�conditions��
ORDER�BY�column1,�column2;�

ORDER BY
---------
Syntax:
SELECT�column1,�column2��
FROM�table_name��
WHERE�condition��
ORDER�BY�column1,�column2...�ASC|DESC;��

Diff b/w where and having


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

where cause is processed before GROUP BY clause


HAVING clause is executed after groups are created.

WHERE can be used to filter on table columns while HAVING can be used to filter on
aggregate function like count, sum, avg, min, and max

If filtering can be done without aggregate function then you must do it on WHERE
clause because it improves performance because counting and sorting will be done on
a much smaller set. If you filter same rows after grouping, you unnecessarily bear
the cost of sorting, which is not used.

What is Save Points in Oracle database?


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

Oracle database Interview Questions and Answers in SQL


Answer : SAVE POINTS are used to divide a transaction into smaller parts. It
enables rolling back part of a transaction.

Whenever we encounter error we can rollback from the point where we set our
SAVEPOINT

What is Materialized View in a Database?


-------------------------------------------
Materialized views are also the logical view of our data-driven by the select query
but the result of the query will get stored in the table or disk, also the
definition of the query will also store in the database.

When we see the performance of Materialized view it is better than normal View
because the data of materialized view will be stored in table and table may be
indexed so faster for joining also joining is done at the time of materialized
views refresh time so no need to every time fire join statement as in case of view.

What is View in a Database?


----------------------------
Views are a logical virtual table created by �select query� but the result is not
stored anywhere in the disk and every time we need to fire the query when we need
data, so always we get updated or latest data from original tables.
The performance of the view depends on our select query. If we want to improve the
performance of view we should avoid using join statements in our query or if we
need multiple joins between tables always try to use the index-based column for
joining as we know index-based columns are faster than a non-index-based column.

View also allows storing the definition of the query in the database itself.

Difference between View vs Materialized View in database


Based upon on our understanding of View and Materialized View, Let�s see, some
short difference between them :

1) The first difference between View and materialized view is that In Views query
result is not stored in the disk or database but Materialized view allow to store
the query result in disk or table.

2) Another difference between View vs materialized view is that, when we create a


view using any table, rowid of view is the same as the original table but in the
case of Materialized view rowid is different.

3) One more difference between the View and materialized view in the database is
that In the case of View we always get the latest data but in the case of the
Materialized view we need to refresh the view for getting the latest data.

4) Performance of View is less than Materialized view.

5) This is the continuation of the first difference between View and Materialized
View, In the case of view it's only the logical view of the table no separate copy
of the table but in the case of Materialized view we get a physically separate copy
of the table

6) Last difference between View vs Materialized View is that In the case of


Materialized view we need an extra trigger or some automatic method so that we can
keep MV refreshed, this is not required for views in the database. You can further
see Oracle SQL Performance Tuning Masterclass for more details on the materialized
view in Oracle.

When to Use View vs Materialized View in SQL


Mostly in an application, we use views because they are a more feasible, only
logical representation of table data no extra space needed.

We easily get a replica of data and we can perform our operation on that data
without affecting actual table data but when we see a performance that is crucial
for a large application they use a materialized view where Query Response time
matters.

So, Materialized views are used mostly with data warehousing or business
intelligence application.

Read more: https://www.java67.com/2012/11/what-is-difference-between-view-vs-


materialized-view-database-sql.html#ixzz6tHDDpf9S

You might also like