Thursday, July 11, 2013

Importing data from one Dev DB server to prod DB Server using DBLinks


Tool used - Oracle SQl Developer

Process
=======

1. Make DBLink
2. Take backup of existing prod table, verify same
3. truncate prod table, verify same
4. transfer data from dev to prod table through DB Link

Planned Commands for same
=========================

--make a DBLInk
create database link linkname_value connect to Schemaname_value identified by username_value using  'SID_name';

--source - SCHEMA1.ABC
--target - SCHEMA1.ABC_BACKUP (make the backup table of same specifications as that of master table, better take the same query as SQl DDL to make the table)

INSERT INTO SCHEMA1.ABC_BACKUP SELECT * FROM SCHEMA1.ABC;

--Please confirm that these two tables are identical by following 2 commands (that they have same number of rows)
SELECT COUNT(*) FROM SCHEMA1.ABC_BACKUP;
SELECT COUNT(*) FROM SCHEMA1.ABC;

--Output of above two commands should be equal, this will confirm that backup of the table has been taken successfully

--truncate source_table
TRUNCATE TABLE SCHEMA1.ABC;

-- confirm that the table is truncated
select * from SCHEMA1.ABC;
--this should show zero rows in the table

--Now in next command, we have to import the data from development server to production server
insert into SCHEMA1.ABC select * from SCHEMA1.ABC@linkname_value ;


Practical Problem in REAL SCHNERIO
=============================
--source - ABC
--target - ABC_BACKUP
INSERT INTO SCHEMA1.ABC_BACKUP SELECT * FROM SCHEMA1.ABC;
--Please confirm that these two tables are identical by following 2 commands (that they have same number of rows)
SELECT COUNT(*) FROM SCHEMA1.ABC_BACKUP;
SELECT COUNT(*) FROM SCHEMA1.ABC;
--Output of above two commands should be equal, this will confirm that backup of the table has been taken successfully

--truncate source_table
TRUNCATE TABLE SCHEMA1.ABC;


Error starting at line 12 in command:
TRUNCATE TABLE SCHEMA1.ABC

PROBLEM 1
=========
Error report:
SQL Error: ORA-02266: unique/primary keys in table referenced by enabled foreign keys
02266. 00000 -  "unique/primary keys in table referenced by enabled foreign keys"
*Cause:    An attempt was made to truncate a table with unique or
  primary keys referenced by foreign keys enabled in another table.
  Other operations not allowed are dropping/truncating a partition of a
  partitioned table or an ALTER TABLE EXCHANGE PARTITION.
*Action:   Before performing the above operations the table, disable the
  foreign key constraints in other tables. You can see what
  constraints are referencing a table by issuing the following
  command:
  SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "tabnam";

SOLUTION 1
==========

SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "ABC";

It shows 3 constraints:

CONSTRAINT_NAME  CONSTRAINT_TYPE SEARCH_CONDITION
SYS_C005497          Check                     "ID" IS NOT NULL
SYS_C005498          Check                     "ABC_NAME" IS NOT NULL
SYS_C005499          Primary_KEY           (null)


run these commands TO DISABLE THESE CONSTRAINTS

ALTER TABLE SCHEMA1.ABC  DISABLE CONSTRAINT SYS_C005497;
ALTER TABLE SCHEMA1.ABC  DISABLE CONSTRAINT SYS_C005498;
ALTER TABLE SCHEMA1.ABC  DISABLE CONSTRAINT SYS_C005499;

--same constaraints can also be seen oracle sql developer constraints tab of table information. confirm that the constraints are disabled, you can do that but looking into the constraints tabs in the table. When the constraint will be disabled it will show "disabled" in the status.

PROBLEM 2
=========

ALTER TABLE SCHEMA1.ABC DISABLE CONSTRAINT SYS_C005499

Error report:
SQL Error: ORA-02297: cannot disable constraint (SCHEMA1.SYS_C005499) - dependencies exist
02297. 00000 - "cannot disable constraint (%s.%s) - dependencies exist"
*Cause:    an alter table disable constraint failed becuase the table has
    foriegn keys that are dpendent on this constraint.
*Action:   Either disable the foreign key constraints or use disable cascade

SOLUTION 2.1
===========

-- find all the constraints in which the primary key of this table is used as foreign key, following command is adopted from other site)

SELECT
p.table_name "Parent Table",
c.table_name "Child Table",    
p.constraint_name "Parent Constraint",
c.constraint_name "Child Constraint"    
FROM user_constraints p    
JOIN user_constraints c
ON(p.constraint_name=c.r_constraint_name)    
WHERE (p.constraint_type = 'P' OR p.constraint_type = 'U')    
AND c.constraint_type = 'R'    
AND p.table_name = UPPER('&table_name');


This will prompt for the table name, please enter "SCHEMA1.ABC" in it.

--Suppose the output is XYZ table names with constraint name

All the constraints in resulting list should be disabled

ALTER TABLE SCHEMA1.XYZ DISABLE CONSTRAINT SYS_C007163;

SOLUTION 2.2
============

--OR simply go with following query but this will not allow you to enable constraints with same fashion

ALTER TABLE SCHEMA1.ABC DISABLE CONSTRAINT SYS_C005499 CASCADE

-- confirm that the constraints are disabled, you can do that but looking into the constraints tabs in the table (see following screen shot). When the constraint will be disabled it will show "disabled" in the status.

TRUNCATE TABLE SCHEMA1.ABC;

-- confirm that the table is truncated

select * from SCHEMA1.ABC;

--this should show zero rows in the table

--enable all the constraints disabled by you

ALTER TABLE SCHEMA1.ABC ENABLE CONSTRAINT SYS_C005497;
ALTER TABLE SCHEMA1.ABC ENABLE CONSTRAINT SYS_C005498;
ALTER TABLE SCHEMA1.ABC ENABLE CONSTRAINT SYS_C005499;


ALTER TABLE SCHEMA1.XYZ ENABLE CONSTRAINT SYS_C007163;

PROBLEM 3
=========
ALTER TABLE SCHEMA1.XYZ ENABLE CONSTRAINT SYS_C007163
Error report:
SQL Error: ORA-02298: cannot validate (SCHEMA1.SYS_C007163) - parent keys not found
02298. 00000 - "cannot validate (%s.%s) - parent keys not found"
*Cause:    an alter table validating constraint failed because the table has
       child records.
*Action:   Obvious

SOLUTION 3
==========
This problem is due to mismatch of the value set of primary key and the foreign key, it can solved by either altering the primary key value set of foreign key value set.

--Now in next command, we have to import the data from development server to production server

insert into SCHEMA1.ABC select * from SCHEMA1.ABC@linkname_value;


ALTER TABLE SCHEMA1.XYZ ENABLE CONSTRAINT SYS_C007163;


Monday, April 22, 2013

Working on decommission of a server and installing instance on other server

Working on decommission of a server and installing instance on other server


Suppose

Server D= server to be decommissioned

Server S= server to be setup

1. Check all the sudo access present on sever D should also be allotted to admin on server S.

2. Copy all the scripts and links connecting apache.

3. Copy the apache tar from the sever D to server S.

4. Set up all the links and scripts eg. Apachectl directing to correct path.

5. Compare if both server belong to same subnet. If not, get the application configured for new subnet.

Setting up the Tomcat and apache instance on the server

Checking the perquisites

1. Check if the following user resides on the server

• (Tomcat) user name

• Group name (group that will own the tomcat)

• Any username that should be owning the application.(Application team should have a sudo excess for this particular username)

If not, raise my access request for the creating the particular group/ user.



2. Make sure that all the apache modules (needed for the tomcat setup to work properly) are already present on the server, if not get them installed on the server by the UNIX team.



3. Make sure that correct java version is present on the server. the java version can be checked by javac command or in /usr path

Installing tomcat

1. Get the set up of tomcat (probably the tar of the other tomcats on the server)

2. Get that tar transferred to the place where tomcat has to be setup.
3. Extract whole tar

4. Make the required changes in server.xml example connector port available according to the server, the application links at various places.

5. Check the scripts like start-tomcat, stop-tomcat and Catalina.sh in the present in the applicationpath/bin folder, in case any customization of tomcat startup is needed.

6. Once changes are done. Start the tomcat by going to path.. applicationpath/bin and ./start-tomcat

7. Check the path http://servername:portnumber/ , to see if tomcat is correctly installed. It should look something like this.



Installing the apache

1. Get the set up of apache (probably the tar of the other apaches on the other server)

2. Extract that tar into one folder

3. Make the changes in the ./conf.http.conf file to make it the specific to this server

Example

server root

ServerRoot "/export/apps/apache/apache-app1"



server admin

ServerAdmin apache@servername.lvst.owner.com



DocumentRoot

DocumentRoot "/project/app/apache/htdocs/app1"



4. make changes in workers.properties in conf folder

worker.tomcat1.host=servername.lvst.owner.com

ssl.conf

Listen 12.34.56.789:111
NameVirtualHost 12.34.56.789:111
SSLPassPhraseDialog builtin

SSLPassPhraseDialog exec:/export/apps/apache/apache-app1/conf/passkey.sh



ServerName  app1.lvst.owner.com

#ServerAlias app1.lvst.owner.com

DocumentRoot "/project/app1/apache/htdocs/app1"

SSLEngine on

SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

SSLCertificateFile /export/apps/apache/apache-app1/cert/app1.crt

SSLCertificateKeyFile /export/apps/apache/apache-app1/cert/app1.key

SSLCertificateChainFile /export/apps/apache/apache-app1/cert/geotrust_intermediate.crt

NOTE- Read the document for setting up the apache for more knowledge on setting up the apache.

Thursday, April 18, 2013

JOINS in SQL

Nice post

http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg

At the top level there are mainly 3 types of joins:


INNER

OUTER

CROSS



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



INNER JOIN - fetches data if present in both the tables.



OUTER JOIN are of 3 types:



LEFT OUTER JOIN - fetches data if present in the left table.

RIGHT OUTER JOIN - fetches data if present in the right table.

FULL OUTER JOIN - fetches data if present in either of the two tables.

CROSS JOIN, as the name suggests, does [n X m] that joins everything to everything.

Similar to scenario where we simply lists the tables for joining (in the FROM clause of the SELECT statement), using commas to separate them.

  Points to be noted:

If you just mention JOIN then by default it is a INNER JOIN.

An OUTER join has to be LEFT RIGHT FULL you can not simply say OUTER JOIN.

You can drop OUTER keyword and just say LEFT JOIN or RIGHT JOIN or FULL JOIN

Tuesday, February 12, 2013

Must see for cancer worries

William Li: Can we eat to starve cancer? #TED : http://on.ted.com/gPm2