Friday, September 28, 2012

How to remove duplicate rows in MySQL

Sometimes your table contains duplicate keys due to the fact that you forgot to add a primary key to the initial design. Something like that
CREATE TABLE employee (
ssn INT NOT NULL,
name VARCHAR(20) NOT NULL 
);
Yeah, Bad database design!!

Here are different ways to fix it
1- You realize that a primary(unique) key is missing, so you add it
ALTER IGNORE TABLE employee ADD PRIMARY KEY (ssn);  
or
ALTER IGNORE TABLE employee ADD UNIQUE KEY (ssn); 
notice the IGNORE keyword.
You could later reverse that constraint by
ALTER TABLE employee DROP index ssn;  
or 
ALTER TABLE employee DROP index ssn; 
2- Delete all but one using LIMIT keyword

DELETE FROM employee WHERE ssn=x LIMIT n; 
where x is the value of the duplicated attribute and n is the number of required deleted rows, i.e. less than the total duplicated rows by 1.
example:
if you have a duplicated ssn = 123456789 repeated 10 times and you want to delete 9 of them

DELETE FROM employee WHERE ssn=123456789 LIMIT 9; 
You can do a COUNT on that ssn before you apply the previous command.

3- Using a temporary table
CREATE TEMPORARY TABLE employee_temp AS SELECT DISTINCT * FROM employee;
DELETE * FROM employee;
INSERT INTO employee SELECT * FROM employee_temp; 

 

Saturday, September 1, 2012

How to install .bin file in linux

1. Use chmod command to make it executable. Open the Terminal and write the following command
chmod +x file.bin
2. Run the file using the following command in the Terminal
./file.bin

Wednesday, August 15, 2012

Struts 2 - The requested resource () is not available

You are here probably because you have just created your first Struts 2 application and after deploying it on a server, you got the following message:

“HTTP Status 404 -
type Status report
message
descriptionThe requested resource () is not available.

This problem is a result of having more or less than the required jar files added to the build path.
Here is the solution, make sure to include exactly these jar files in the build path.
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.jar
xwork-core-2.3.4.jar

It should work.

Note: The numbers at the end of the file names may be different for the Struts release you work with.