Sunteți pe pagina 1din 8

Oracle Errors and Tips

1.select to_char(sysdate,'hh24') from dual;


o/p 17---> gives the hour in 24 hours format
2.select to_char(sysdate,'hh12') from dual;
o/p 05---> gives the hour in 12 hours format
3.write a query to display the employees whose name containing 'A' ?
select * from emp where INSTR(name,'A',1,2)<1 and
INSTR(name,'A',1,1)>0;
or
select * from emp where name like '%A%' and name not like '%A%A
%';
4.Alter two columns at a time
Alter table salary_det_new modify (basic default null ,hra default null);
ALTER TABLE supplier
MODIFY (supplier_name char(100) NOT NULL,
city char(75));

5.Alter table two add multiple columns;


ALTER TABLE supplier ADD (supplier_name char(50), city char
Note:we can change the datatype of a column provided the column is empty;
6.How to insert new columns at specific position ?
I don't believe you can add a column anywhere but at the end of the table
once the table is created. One solution might be to try this:
CREATE TABLE MY_TEMP_TABLE AS
SELECT *
FROM TABLE_TO_CHANGE;
Drop the table you want to add columns to:

DROP TABLE TABLE_TO_CHANGE;


It's at the point you could rebuild the existing table from scratch adding in
the columns where you wish. Let's assume for this exercise you want to
add the columns named "COL2 and COL3".
Now insert the data back into the new table:
INSERT INTO TABLE_TO_CHANGE (COL1, COL2, COL3, COL4)
SELECT COL1, 'Foo', 'Bar', COL4
FROM MY_TEMP_TABLE;
When the data is inserted into your "new-old" table, you can drop the
temp table.
DROP TABLE MY_TEMP_TABLE;

Note:For adding a foreign key constraint to a table with alter ky constraint


name is mandatory
alter table employee_leave add constraint fk foreign key(empid)
references salary_det1(empid) on delete cascade;
NOTE: oracle doesnt allow primary and unique key to be defined on the
same table.

Primary Key: Doesnt allow null and duplicate values


unique key : allows null but not duplicate values
Note: Natural join doesnt work properly with where clause when we give
column name;
FOR EX:
select empid,ename from salary_det1 NATURAL JOIN employee_leave
NATURAL JOIN leave_policy
where LEAVE_NAME='Casual leave' and LEAVE_DATE_FROM > '01-01-2012'
and LEAVE_DATE_TO < '01-09-2012' ;
O/p: Here it doesn't check for the where condition
EA12345
Geetha S
EA34567
Jayganesh
SAP1234Mohan V

But when we do
select * from salary_det1 NATURAL JOIN employee_leave NATURAL JOIN
leave_policy
where LEAVE_NAME='Casual leave' and LEAVE_DATE_FROM > '01-01-2012' and
LEAVE_DATE_TO < '01-09-2012' ;
O/p It checks for the where condition
1
EA12345
Geetha S
09-07-12
0
Casual leave
1
EA34567
Jayganesh
02-12
5
Casual leave 3

60500
3
3
20000
3
3

2750
10-07-10
3
3
n
500 01-07-13
M
3
n

07-07-12

18-01-12

10-

So to resolve it,modify the select query as below,


select empid,ename from(select * from salary_det1 NATURAL JOIN
employee_leave NATURAL JOIN leave_policy)
where LEAVE_NAME='Casual leave' and LEAVE_DATE_FROM > '01-01-2012' and
LEAVE_DATE_TO < '01-09-2012' ;
O/P
EA12345
EA34567

Geetha S
Jayganesh

Natural join is generally used when column names match in


both the tables.
JOIN ON clause is normally used for tables that do not have
similar column names.
7.compare values of one column in a table with another table
ranges with no common column(cross join)
select rate as tax from salary_det1,tax where basic
>=range1 and basic<=range2;

ORA-02449: unique/primary keys in table referenced by foreign keys


Sometimes when dropping a table in Oracle database by executing DROP TABLE
SQL statement, Oracle may return the error ORA-02449 as below:
ORA-02449: unique/primary keys in table referenced by foreign keys

The solution and workaround for the error when you want to drop tables
referenced by child tables, is to use the CASCADE CONSTRAINTS option in the
DROP TABLE statement. For example:
DROP TABLE table_name CASCADE CONSTRAINTS;
The CASCADE CONSTRAINTS option in the DROP TABLE SQL statement will drop
the FOREIGN KEY constraints of the child tables referenced.

Alternatively, you can manually drop and remove the foreign key key constraints
in other tables before performing the DROP TABLE operations on the parent table,
drop the foreign key constraints in other tables. To check what constraints are
referencing a table in Oracle, use the following command:
SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "table_name";
To drop and delete the user constraints in Oracle use the following command in
SQL*Plus, Toad or other SQL tools:
ALTER TABLE table_name DROP CONSTRAINT constraint_name;

S-ar putea să vă placă și