I Want To Display 1 To 10 Numbers Using One Select Statement
I Want To Display 1 To 10 Numbers Using One Select Statement
LEVEL
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
OR
Select rownum from USER_OBJECTS where rownum <= 10 order by rownum ASC;
ROWNUM
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
How to delete duplicate rows from a specified table (only single table) how
do you know which join is need to be used.
DELETE FROM emp a WHERE ROWID> (SELECT MIN (ROWID) FROM emp b WHERE
A.empno=b.empno);
Static queries are the queries we normally use like SELECT * FROM EMPLOYEE.
Where as dynamic queries can be built and executed dynamically. sp_executesql
system stored procedure is used to execute dynamic sql statements.
Dynamic sql queries will take more time to execute when compared to static queries.
Master, MSDB and TempDB these are the inbuilt Database which r provided by
SQL SERVER. All have their own functionality & responsibility like.
I have doubt that any one tell ref cursor comes in sql pl/sql? pls clarify?
Ref cursor comes in PL/SQL. You can return a ref cursor in a function.
A ref cursor is useful while returning more values.
It will use in PL/SQL. Reusability of Cursor is nothing but "REF CURSOR”. These
are of 2 types.
If I have a table T with 4 rows & 2 columns A & B. A has values 1,2,3,4. And
B has 10,20,30,40. Write an Update SQL query which can Swap the values of
A & B for all records. (Do not use a sub-query)?
What is a REF CURSOR? Compare strong and week ref cursor types.
For the strong ref cursor the returning columns with data type and length need
to be known at compile time.
For the weak ref cursor the structure does not need to be known at compile time.
Select * from emp where (rowid, 1) in (select rowid, mod (rownum, 2) from emp);
Output:-
1
3
5
Select * from emp where (rowid, 0) in (select rowid, mod (rownum, 2) from emp);
Output:-
2
4
6
For n’ th number,
Select * from emp where sal = (select max (sal) from emp);
--> Here a sub query is also there
Select * from emp outer where sal= (select avg (sal) from emp e
Where dept.e=dept.outer) --> corelated query
Or
And
Co-related query is sub one of sub query (sub query means--whose returning values
are filtering the condition of the main query)
SELECT * FROM EMP E WHERE E.SAL> (SELECT AVG (SAL) FROM EMP F
WHERE E.DEPTNO= F.DEPTNO);
AND CO RELATED SUB QUERY ALWAYS USE ALIAS NAME FOR TABLE, IT SIMILAR
LIKE JAIN (SELF JOIN)
Use of IN/ANY/ALL
SQL - select distinct employeeid from orders where orderid in (select orderid from
order details where discount >= 10)
ANY - used in case of relational queries to compare result with any of the key.
SQL - select custID from orders where regionID != "E" and discount > any (select
discount from orders where regionID = "E" and discount > 3)
ALL - used in case of relational queries to compare result with all of the keys.
SQL - select custID from orders where regionID != "E" and discount > all (select
discount from orders where regionID = "E" and discount > 3)
Or
ANY-It will compare with any value that has been returned by the parameter;
Select * from emp where salary > any (select salary from emp where deptno=10)
The salary will be compared with any value that has been returned by the subquery.
ALL-It will compare with max/min value that has been returned by the subquery;
Select * from emp where salary > all (select salary from emp where deptno=10)
The salary will be compared with the longest value that has been returned by the sub
query.
Which prevents the user from entering the duplicating into tables or views is
called integrity constraint.
Ex: primary key constraint, foreign key constraint, unique constraint, checks
constraint.
Or
Data integrity allows defining certain data quality requirements that the data
in the database needs to meet. If a user tries to insert data that doesn't meet these
requirements, Oracle will not allow so.
Constraint types
Not Null
A column in a table can be specified not null. It's not possible to insert a null
in such a column. The default is null. So, in the following create table statement, a
null can be inserted into the column named c.
The first to records can be inserted; the third cannot, throw a ORA-01400:
cannot insert NULL into ("RENE"."RI_NOT_NULL"."A").
The not null/null constraint can be altered with alter table ri_not_null modify a null;
Unique Key
However, if a column is not explicitely defined as not null, nulls can be inserted
multiple times:
Primary Key
Primary keys can explicitly be named. The following create table statement
creates a table with a primary key whose name is pk_name.
Foreign Key
Check
The following table allows only numbers that are between 0 and 100 in the column a;
It is also possible to state a check constraint that checks the value of more
than one column. The following example makes sure that the value of begin_ is
smaller than the value of end_.
1 EMP_NO EMP_NAME
--------- --------- -------------------------
1 100 SELVA
1 101 RAJ
1 102 A S KALA
1 103 JESLIN FANTA MALAR
1 104 ANITA
1 105 MURUGU
1 106 SRIVATSAN
1 107 SARABOOT
1 108 KARTHI SIR
1 109 SUDHA
1 110 MERCHI
1 111 SAVI
12 rows selected.
No, View is a logical table associated with a query. It will not store any data
in it. Only the query with which the view is created is stored in the database.
NO, the view is a logical table based on one or more tables or views. A
view in practicality contains no data by itself. it contains only select statements.
If i perform any operation on views such as insert, delete etc will my base
table get affected?????
Or
It will affect ur base table only when u having a simple view and if you put null
in the field which is primary key of the base table then also the base table will not
affected. And if you are using the compute view then u r not able to insert or delete
the records.
STUD-NAME SUBJECT
--------- ------
STUD1 A
STUD2 B
STUD2 A
STUD1 A
In this structure 1 row is duplicated in 4’th. So we can fetch the student name
using the below query.
If you need to take tha backup of the database on ur hard disk then u can use the
following query:
Database is logically divided into three tablespaces. Once creates the database
then automatically create tablespace is also called SYSTEM tablepace.tablespace
contain data dictionary of all data.
Or
A Table space is a logical group of data files in a database. A typical data base
consists at least one table space, and usually two or more. In a database a table
space plays a role similar to that of a folder on the hard drive of a computer.
What is SGA?
A pseudo column is an item of data which does not belong in any particular
table but which can be treated as if it did. Any SELECT list of columns can include
these pseudo columns.
Or
User, UserID
RowID, RowNum
Level
CurrVal, NextVal
Sysdate
Or
Delete from emp where rowid not in (select min (rowid) from
emp group by empno;
Delete the emps whose salaries are lowest sals of their own dept.
Or
Example:
Declare
Cursor cl is select empname,salary,(salary*12)annual
renumeration
Begin
For i in cl
Loop
if i.annual renumeration > 30000 then
dbmsoutput.putline(empname);
End if
End;
Select empno from emp where empno is not null group by empno
Having count (*) >1;
Yes, a table may have more than 1 primary keys but, of them we only
choose 1 column as identifiable column called primary key. The other columns are
called surrogate keys or candidate key.
Use the PURGE statement to remove a table or index from your recycle bin
and release all of the space associated with the object, or to remove the entire
recycle bin, or to remove part of all of a dropped tablespace from the recycle bin.
PURGE RECYCLEBIN;
Or
This command will clear away the table from database as well as from the
recycle bin. After firing of purge command you cannot retrive the table using
flashback query.
2) DBMS organised the data in any formmate but RDBMS allows only in row and
column format.
3) In DBMS we can not create the the relationshs but in RDBMS we can not create
the relationship between the tables
Declare
Cursor cur_t is select * from test;
var_t test%rowtype;
Begin
Open cur_t;
Loop
Fetch cur_t into var_t;
Exit when cur_t%notfound;
dbms_output.put_line('a: ' || var_t.a || ', b: ' ||
var_t.b);
End loop;
End;
/
View: View is a virtual table, a query attached to it. Actually it has not
stored query results. It will execute and returns rows.
I want to display the employees who have joined in last two months. (It
should be executed randomly means If I execute the query in March it
should display Jan and Feb joined employees. Same query if i execute in
Feb, 2007 it should display dec, 2006 and Jan 2007 joined employees.
OR
Select * from af where months_between(sysdate,hiredate)<=2
OR
select * from emp a ,(select empno,count(*) from emp group by empno having
count(*) > 1) b where a.empno = b.empno;
ROWTYPE is declared at the record level and the TYPE is declared for the
column level. (TABLE Level).
%rowtype is used to declare a record with the same types as found in the
specified database table, view or a cursor.
%Rowtype is used to declare a record that represents a particular row of a table.
%type is used to declare a field with the same type as found in the specified
table's column. (COLUMN Level).
%Type is used to change the size of any datatype but only for one record.
8 rows selected.
SQL> declare
2 v_EMP_ID empn.EMP_ID%type;
3 v_FIRST_NAME empn.FIRST_NAME%type;
4 cursor empn_cursor is
5 select EMP_ID,FIRST_NAME from empn;
6 begin
7 open empn_cursor;
8 loop
9 fetch empn_cursor into v_EMP_ID,v_FIRST_NAME;
10 exit when empn_cursor%rowcount>7;
11 dbms_output.put_line(to_char(v_EMP_ID)||' '||v_FIRST_NAME);
12 end loop;
13 close empn_cursor;
14 end;
15 /
101 SELVA
102 NANDHINI
103 JESLIN
104 MERCY
105 SATHISH
110 A S KALA
121 MALAR
What is an index and types of indexes? How many numbers of indexes can
be used per table?
What is normalization?
Normalization is a rule applied on the database table in order to remove
redundancy of data.
1NF Eliminate Repeating Groups - Make a separate table for each set of related
attributes, and give each table a primary key.
ONF Optimal Normal Form - a model limited to only simple (elemental) facts, as
expressed in Object Role Model notation.
DKNF Domain-Key Normal Form - a model free from all modification anomalies.
Instead of trigger is used to update the database tables associated with the
view instead of updating the view directly.
Or
Or
Select empname,sal from (select empname,sal from dh1 order by sal desc)
where rownum < =4 order by sal desc
Or
Select sal from emp a where 3> (select Count (distinct (sal)) from emp b where
a.sal<b.sal);
Union is used to select distinct values from two tables where as union all
is used to select all values including Duplicates from the tables.
Union will return only distinct values from two tables. Union all will return
duplicate values from the two tables.
When inner subquery has a reference to outer query then this is know as
Correlated sub-query.
Save points are used to mark our transactions. Then later on, down the line,
we can roll back to that particular Transaction and roll back can be issued to that
particular point.
INSTR is used to find the position of any particular character in a word which
returns numeric value.
Assign the ON DELETE CASCADE clause while creating the foreign key
If a View on a single base table is manipulated will the changes be reflected
on the base table?
Yes, definitely. Since the view is based on a single table and falls within the
category of simple view. Any change / Modification on the records will be reflected
in the base table.
CYCLE statement is given when the sequence has to repeat its number
generation between START WITH and MAX VALUE.
How to access the current value and next value from a sequence?
The start with... connect by clause can be used to select Data that has a
hierarchical relationship (usually some sort of parent->child (boss->employee or
thing->parts).
Constraints:
1. Primary Key
2. Unique Key
3. Not Null
4. Referential Integrity (Foreign Key)
5. Check Constraints
Integrity Constraints prevent invalid data entry into the Table. These
constraints set a range and any violation takes Place oracle prevents the user
from performing manipulation on the table.
1) Varchar2 ()
2) Date
4) Long
5) Integer
6) Number
7) Char
8) clob
9) Raw ()
10) pls_integer
11) binary_integer
12) Nvarchar2 ()
13) bfile
1. Data security: complex view can not be modified i.e. base tables cannot be visible.
2. It provides easy way to query data from different data sources like a single table.
a) Data security - no need to give permission on the table, intact a view can be
created, having only selected number of columns in its definition. So user will only be
able to see those columns.
c) Removes dependency - Can be very helpful to remove the Dependency from the
underlying tables. Suppose a view is created by joining several tables. After some
time, there are some changes on the tables, so only definition of view can be
changed and there is no need to change all the code where view is used.
We can use user_tables view of data dictionary. Create a cursor based on query
Select * from user_tables and then use use this cursor in for loop
DUPLICATE?
DECLARE
SD DATE;
ED DATE DEFAULT SYSDATE;
Y NUMBER;
M NUMBER;
D NUMBER;
BEGIN
SD:='&SD';
Y:=trunc( months_between( ED, SD ) /12 );
M:=mod( trunc( months_between( ED, SD ) ), 12 );
D:=ED - add_months(SD,trunc( months_between( ED, SD ) ));
DBMS_OUTPUT.PUT_LINE(Y || ' Years'||',' || M || ' Months' ||','|| D || ' Days');
END;
OUTPUT :