Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

08 November, 2013

MySQL – 'mysql' is not recognised as an internal or external command

Problem:
'mysql' is not recognized as an internal or external command, operable program or batch file.


Solution:
To solve the above the error message, please follow the below steps:

  1. Open Control Panel
  2. Then open System and Click on Advanced system Settings
  3. Select “Advanced” Tab and click on Environment Variables
  4. In System variables, Select Path and Click on Edit button. At the end of Variable value text box, write the path of MySQL bin directory.

For example
C:\Program Files\MySQL\MySQL Server 5.6\bin;

20 March, 2013

Check Foreign key exists in MySQL

If you want to add/drop foreign key then you need to check is foreign key is exist or not.

IF NOT EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE
                   CONSTRAINT_SCHEMA = DATABASE() AND
                   CONSTRAINT_NAME   = 'fk_rabbits_main_page' AND
                   CONSTRAINT_TYPE   = 'FOREIGN KEY') THEN
   ALTER TABLE `rabbitsADD CONSTRAINT `fk_rabbits_main_page`
                             FOREIGN KEY (`main_page_id`)
                             REFERENCES `rabbit_pages` (`id`);
END IF
 
 
Using following query you will get more information about foreign key with column details.
 
 SELECT 
    TABLE_NAME,COLUMN_NAME,
    CONSTRAINT_NAME,
    REFERENCED_TABLE_NAME,
    REFERENCED_COLUMN_NAME 
FROM 
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE 
    TABLE_NAME='TABLENAME' 
    AND REFERENCED_TABLE_NAME = 'REFERENCED_TABLE_NAME' 
    AND CONSTRAINT_NAME='FK_Key_Name'
    AND REFERENCED_COLUMN_NAME='Code'; 

15 February, 2013

MySQL CONCAT address into string seperated by comma

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

NULLIF(expr1,expr2) Returns NULL if expr1 = expr2 is true, otherwise returns expr1.
SO your query could be:

SELECT 
  CONCAT_WS(', ', 
       NULLIF(location_address1, ''),
       NULLIF(location_address2, ''),
       NULLIF(location_town, ''), 
       NULLIF(location_region, ''), 
       NULLIF(location_postcode, ''), 
       NULLIF(country_name, '')
  ) AS address
FROM
   countries c      
WHERE
   c.country_id = locations.country_id LIMIT 1