Wednesday, March 27, 2013

Export and Import tablespace on same database

export the single tablespace
expdp system/orcl directory=DATA_PUMP_DIR dumpfile=testtablespace.dmp logfile=testtablespace.log tablespaces=users


export the single tablespace 
expdp system/orcl directory=DATA_PUMP_DIR dumpfile=testtablespace.dmp logfile=testtablespace.log tablespaces=users,test


export tablespace with-out index (10x faster if we include index)**
expdp system/orcl directory=DATA_PUMP_DIR dumpfile=testtablespace.dmp logfile=testtablespace.log tablespaces=users,test exclude=indexes*

Drop the all users related to same tablespace.

If you try to import the dump, without dropping the users, then impdp fails with error
"ORA-39151. "All dependent metadata and data will be skipped due to table_exists_action of skip"




solution to above error "table_exists_action=replace"
impdp system/orcl directory=DATA_PUMP_DIR dumpfile=testtablespace.dmp logfile=testtablespace.log tablespaces=users table_exists_action=replace
or
impdp system/orcl directory=DATA_PUMP_DIR dumpfile=testtablespace.dmp logfile=testtablespace.log tablespaces=users,test  table_exists_action=replace
or
impdp system/orcl directory=DATA_PUMP_DIR dumpfile=testtablespace.dmp logfile=testtablespace.log tablespaces=users,test exclude=indexes  table_exists_action=replace



 
Note - ** soon script will provided to collect all inde.

       * collect all index to some source, either you will lost your indexes.

[sqlserver] Subqueries are not allowed in this context.

--only in case of SQLServer 2005

insert into emp values (388,'ename','last_name',4,null,(select MAX(sal) from emp),300,20);

Then following error is coming :
Msg 1046, Level 15, State 1, Line 5
Subqueries are not allowed in this context. Only scalar expressions are allowed.


Problem:
syntax error,, not supported by sqlserver ver. 2005.

Solution:
insert into emp (empno,ename , last_name , mgrno, hiredate, sal, comm , deptno) select 388,'ename','last_name',4,null, MAX(sal) ,300,20 from emp1 ;

Tuesday, March 12, 2013

Error Code: 1175 You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

It is just a warning, can hinder you,, while executing a Function or Procedure.

Error Code: 1175
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column


Problem:
It is because you tried to update/delete a table without a WHERE that uses a KEY column .

Solution:
the fix is to add SET SQL_SAFE_UPDATES=0; before your update query. Here’s the example:
SET SQL_SAFE_UPDATES=0;



OR


use below approach

Edit -> Preferences -> SQL Editor -> SQL Editor
remove Forbid UPDATE and DELETE statements without a WHERE clause (safe updates)



Saturday, March 9, 2013

Error: Explicit or implicit commit is not allowed in stored function or trigger.

Problem:
The problem is that there is a implicit commit happening somewhere .

In my case it was due to
I used "Truncate table table_name" in a function.

or

it is due to "create index idx_name on table_name(column_name);"

Solution:
Instead TRUNCATE use "delete from table_name".

or

use this
"Create table table_name (column_name [data_type] ,
                         index [index_name] (column_name));"

Procedure Hierarchical /recursive data (parent-child relationship)

drop table if exists product;

create table product
(
prod_id smallint unsigned not null auto_increment primary key,
name varchar(255) not null,
parent_id smallint unsigned null,
key (parent_id)
)engine = innodb;


insert into product (name, parent_id) values
('Products',null),
('Systems & Bundles',1),
('Components',1),
('Processors',3),
('Motherboards',3),
('AMD',5),
('Intel',5),
('Intel LGA1366',7);


delimiter ;

drop procedure if exists product_hier;

delimiter #

create procedure product_hier
(
in p_prod_id smallint unsigned
)
begin

declare v_done tinyint unsigned default 0;
declare v_depth smallint unsigned default 0;

create temporary table hier(
parent_id smallint unsigned,
prod_id smallint unsigned,
depth smallint unsigned default 0
)engine = memory;

insert into hier select parent_id, prod_id, v_depth from product where prod_id = p_prod_id;


create temporary table tmp engine=memory select * from hier;

while not v_done do

if exists( select 1 from product p inner join hier on p.parent_id = hier.prod_id and hier.depth = v_depth) then

insert into hier
select p.parent_id, p.prod_id, v_depth + 1 from product p
inner join tmp on p.parent_id = tmp.prod_id and tmp.depth = v_depth;

set v_depth = v_depth + 1;

truncate table tmp;
insert into tmp select * from hier where depth = v_depth;

else
set v_done = 1;
end if;

end while;

select
p.prod_id,
p.name as prod_name,
b.prod_id as parent_prod_id,
b.name as parent_prod_name,
hier.depth
from
hier
inner join product p on hier.prod_id = p.prod_id
inner join product b on hier.parent_id = b.prod_id
order by
hier.depth, hier.prod_id;

drop temporary table if exists hier;
drop temporary table if exists tmp;

end #

delimiter ;

call product_hier(3);

call product_hier(5);


http://stackoverflow.com/questions/3812621/handling-tree-in-a-mysql-procedure


-------------===========-----------------=========------------------==========
same thing is modified by me in the form of FUNCTION recursive

drop function if exists product_hier;

delimiter #

create function product_hier
(
p_prod_id int
)
returns int

begin

declare v_done tinyint unsigned default 0;
declare v_depth smallint unsigned default 0;
declare v_check int;


create temporary table hier(
parent_id smallint unsigned,
prod_id smallint unsigned,
depth smallint unsigned default 0
)engine = memory;

insert into hier select parent_id, prod_id, v_depth from product where prod_id = p_prod_id;


create temporary table tmp engine=memory select * from hier;

while not v_done do

if exists( select 1 from product p inner join hier on p.parent_id = hier.prod_id and hier.depth = v_depth) then

insert into hier
select p.parent_id, p.prod_id, v_depth + 1 from product p
inner join tmp on p.parent_id = tmp.prod_id and tmp.depth = v_depth;

set v_depth = v_depth + 1;

delete from tmp;
insert into tmp select * from hier where depth = v_depth;

else
set v_done = 1;
end if;

end while;


set v_check= (select count(*) from
hier
inner join product p on hier.prod_id = p.prod_id
inner join product b on hier.parent_id = b.prod_id);


drop temporary table if exists hier;
drop temporary table if exists tmp;

return v_check ;
end #

delimiter ;



mysql> select product_hier(5);
mysql> select product_hier(24);





Friday, March 8, 2013

Connection with MYSQL from a remote machine

1. Connection with root user
     cmd> mysql -p -u root -h ip_of_the_machine


2. receive the error:
ERROR 1044 (42000): Access denied for user 'root'@'%' to database ' 

enviornment:->
mysql is installed on 192.168.1.10
need to connect from  192.168.1.20

command execute on "192.168.1.10"

create database remote;




--chose any one pattern from below(i worked with second)
CREATE USER 'g'@'%' IDENTIFIED BY 'g';
CREATE USER 'g'@'localhost' IDENTIFIED BY 'g';
CREATE USER 'g'@'192.168.1.20' IDENTIFIED BY 'g';

grant all on *.* to g@'%';
grant all on *.* to g@'localhost';
grant all on *.* to g@'192.168.1.20';

grant all on database_name.* to g@'localhost';

update mysql.db set host='192.168.1.20' where db='g';
UPDATE mysql.USER SET Host='192.168.1.20' WHERE USER='g';


--RESTART THE services for MYSQL

command execute on "192.168.1.20"
cmd> mysql -p  -u l -h 192.168.1.10
mysql> select current_user(); 


:::::::::::::::::::::::::::::::::::::::::::::::: W O R K I N G ::::::::::::::::::::::::::::::::::::::::::::::::::

ORA-12154: TNS:could not resolve the connect identifier specified

Error while connecting to the remote machine database
ORA-12154: TNS:could not resolve the connect identifier specified

Problem :- service name not correct in connection identifier
                                                     OR
                 Service identifier is not available in "tnsname.ora" file

example -- sqlplus name/password@service_name_identifier

Solution:-- add a "service-name" using "Net Configuration Assistant" by click on option "Local Net service        name configuration"

below is the example of tnsnames.ora file
ORCL* =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = IP_of_system)(PORT = port_number_1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl**)
    )
  )


note*- service name identifier can be same as the service name of database
          **- service name of database
or
check the "service_name_identifier" matches with service name in "tnsname.ora" file

Thursday, March 7, 2013

Few regular used commands Mysql

1. How to create a database
mysql> create database db_name
mysql> use db_name                         //change database
mysql> show database                       // will show all databases
mysql> select user()                           // see connect user
mysql> SELECT user, host FROM mysql.user;                   // see all users
mysql>select current_user();
mysql> show function status;              // show function in mysql database
mysql> show procedure status;          // show procedure in mysql database
mysql> SELECT * FROM mysql.user WHERE User='root'\user-name
mysql> show create table table_name;    //display ddl for table (create table ddl)

Let' us a new start with Mysql

Forgot password   :(
Don't Worry

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Solution:

(First start the mysql server instance or daemon with the --skip-grant-tables option. (security setting)  --) ignorable

 Follow this

open cmd
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('your_new_password') where USER='root';
mysql> FLUSH PRIVILEGES;

 Then, restart the instance    
 /daemon without the --skip-grant-tables option. 

 Now you can able to connect with your new password.

# mysql -u root -p
Enter password: your_new_password



web stats