How do I see all foreign keys to a table or column?

For a Table: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = ‘<database>’ AND REFERENCED_TABLE_NAME = ‘<table>’; For a Column: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = ‘<database>’ AND REFERENCED_TABLE_NAME = ‘<table>’ AND REFERENCED_COLUMN_NAME = ‘<column>’; Basically, we changed REFERENCED_TABLE_NAME with REFERENCED_COLUMN_NAME in the where clause.

Create new user in MySQL and give it full access to one database

Try this to create the user: CREATE USER ‘user’@’hostname’; Try this to give it access to the database dbTest: GRANT ALL PRIVILEGES ON dbTest.* To ‘user’@’hostname’ IDENTIFIED BY ‘password’; If you are running the code/site accessing MySQL on the same machine, hostname would be localhost. Now, the break down. GRANT – This is the command … Read more

How to get size of mysql database?

Run this query and you’ll probably get what you’re looking for: SELECT table_schema “DB Name”, ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) “DB Size in MB” FROM information_schema.tables GROUP BY table_schema; This query comes from the mysql forums, where there are more comprehensive instructions available.

When to use single quotes, double quotes, and backticks in MySQL

Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set (see below) It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the … Read more

‘IF’ in ‘SELECT’ statement – choose output value based on column values

SELECT id, IF(type=”P”, amount, amount * -1) as amount FROM report See http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html. Additionally, you could handle when the condition is null. In the case of a null amount: SELECT id, IF(type=”P”, IFNULL(amount,0), IFNULL(amount,0) * -1) as amount FROM report The part IFNULL(amount,0) means when amount is not null return amount else return 0.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)