I have a database where I need to drop some foreign keys, but I don't know beforehand whether the foreign keys still exist.
I've found some stored procedures (http://forums.mysql.com/read.php?97,218825,247526) that does the trick, but I don't want to create a stored procedure for this.
I've tried to use the query inside the stored procedure, but I get an error using "IF EXISTS (SELECT NULL FROM etc.. etc...
Can I only use IF EXISTS in stored procedures?
right now, the only thing I can run is
SELECT * FROM information_schema.TABLE_CONSTRAINTS
WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema'
AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';
and I've tried this too
IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = parm_key_name) THEN
(...) do something (...)
END IF;
but I get a You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF' at line 1
I've looked for examples in forums with simple queries and I can't make sense of why this isn't working.
NOTE: Edit to correct broken link
You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table
SELECT * FROM information_schema.TABLE_CONSTRAINTS T;
you need to be a ROOT user to access the information_schema.
USING this table you can find the table, db and whether it has foreign key.
Hope this helps if you dont wanna use IF EXIST and Stored Procedure. But I am Sure you can use IF EXIST can be used for non stored procedure queries....
Why don't You use the table "INFORMATION_SCHEMA" to this?
SELECT *
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table
select
concat(table_name, '.', column_name) as 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
information_schema.key_column_usage
where
referenced_table_name is not null;
HELP: see this link list-foreign-keys-in-mysql
Related
I hope someone can help me with this. I'm moving a MySQL database (WordPress) to a MySQL Cluster running Master-Master replication. When I'm trying to import the SQL to the database I get the following error message;
Plugin group_replication reported: 'Table wpmk_actionscheduler_actions does not have any PRIMARY KEY. This is not compatible with Group Replication.'
Ok, so I know what this means or I thought I did. When I inspect this table in PHPmyAdmin, I can see this table does have a primary key. I ran the following command to find the tables without a primary key;
SELECT
tab.table_schema AS database_name,
tab.table_name AS table_name,
tab.table_rows AS table_rows
FROM information_schema.tables tab
LEFT JOIN information_schema.table_constraints tco
ON (tab.table_schema = tco.table_schema
AND tab.table_name = tco.table_name
AND tco.constraint_type = 'PRIMARY KEY')
WHERE
tab.table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
AND tco.constraint_type IS NULL
AND tab.table_type = 'BASE TABLE';
But this doesn't return any tables (because they all have keys). And yet, my damn cluster is saying a key is missing when importing the SQL. I'm totally stuck.
I attached a screenshot of the table before I exported it. What am I missing here?
I fixed this myself by downloading MySQL Workbench, opening the .SQL and editing the create statements to include an ID column and set it as the primary key. Then I removed the alter statements which were supposed to add primary keys. I guess these are updates from developers.
I have a large database with many tables, and "on-delete" action is set to cascade for all of them. Is it possible to change this option to "No action" in one go without having to open each and every table and each and every relation properties in Workbench ?
I presume it is not possible in Workbench interface, but I would bet it can be done programmatically using cursors that loop through all tables and change relations
Is necessary to drop the constraint and add it with ON DELETE CASCADE.
You can have the list of all tables with the SQL to:
1-drop all
SELECT concat('ALTER TABLE ' , TABLE_NAME,' DROP FOREIGN KEY ' ,CONSTRAINT_NAME ,';' )
FROM information_schema.REFERENTIAL_CONSTRAINTS
WHERE constraint_schema = '<database>';
2- add all
SELECT concat('ALTER TABLE ' , TABLE_NAME,' ADD FOREIGN KEY (', column_name ,') REFERENCES ', REFERENCED_TABLE_NAME ,' (', REFERENCED_COLUMN_NAME,') ON DELETE CASCADE;' )
FROM information_schema.key_column_usage where
constraint_name <>'PRIMARY'
AND TABLE_SCHEMA = '<database>';
You can copy the result sql of the queries above and execute.
NOTE: Don't execute the first 'Drop all' before you have stored the information to create again given by the 2nd query (in a text file for example).
Since I am not much of a DB guy, I have a query.I use Mysql.
I am given a table which has around 12 columns and that table has a PRIMARY key,UNIQUE & a FOREIGN key defined.
Is there a way to find on which columns the constraints are defined?
I came across one query:
SHOW INDEX FROM tablt_name;
But It does not give a clear idea, only the primary key column is displayed by the above query.
If there is any other way to get the info, pls help
You can try like thisL
USE information_schema;
SELECT table_name,
column_name,
constraint_name,
referenced_table_name,
referenced_column_name
FROM key_column_usage
WHERE table_schema = ""
AND table_name = ""
AND referenced_column_name IS NOT NULL;
or
DESCRIBE table_name
Having the following DDL in MySql 5.5.x.
create table if not exists t1 (col1 int);
and
CREATE PROCEDURE my_proc()
BEGIN
select inexistent_column from t1;
END;
Why the procedure is created without errors?
I expect it to throw that inexistent_column is not known or does not exists.
How can I make MySql sql compiler check for table definitions?
UPDATE: Rephrase
I just want a way to avoid a typo in a column while developing, meaning, creating a brand new procedure. Is there any way to do this? Maybe that should've been the question to ask from the start.
The server will only do syntax checking, but you can check for the existence of columns yourself. All existing tables and columns are defined in the information_schema.
You could use a query like this to check if your column exists:
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'your_database_name'
AND TABLE_NAME = 'your_table_name'
AND COLUMN_NAME = 'some_column'
I have a script that drops a load of tables using DROP TABLE IF EXISTS, this works.
There is also a delete in this script to DELETE a row from another table that I do not manage. This table may or may not exist.Is there any to check the table exists before attempting to delete a row?
this needs to work for MYSQL and SQLServer
thanks
Alex
To check in SQL SERVER,
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'TheSchema' AND TABLE_NAME = 'TheTable'))
BEGIN
--Do Stuff
END
To check in mysql:
You simply count:
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = '[database name]'
AND table_name = '[table name]';
This one deletes the row and does not complain if it can't.
DELETE IGNORE FROM table WHERE id=1
source here.
For SQL Server: You could use:
IF OBJECT_ID('tablename','U') IS NOT NULL
I dont think you'll find a common syntax between SQL server and my SQL. I mean, you can check if the table exsits on SQL Server using something like:
if exists(select * from sys.objects where name like 'table_name')
but mySql would have its own catalog.
Unless you write a script like:
if (sql_server) then
if exists(select * from sys.objects where name like 'table_name')
else --mySQl
--execute the mysql script
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLE_NAME]') AND type in (N'U'))
It seems to me right the first item in the "Related" column on the right side answers your question.... Check if table exists in SQL Server
For MySQL
show tables like "test1";
For SQL Server
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'testSchema' AND TABLE_NAME = 'test1'
A question you want to ask yourself (in terms of database design): Why are you trying to delete rows from a table you are not sure exists? If it doesn't, but you expect it does, wouldn't you rather create the table than not delete it?
Anyway, Chris Gesslers answer does exactly what you are asking in SQL Server, but there is some smell here.
The construct in MySQL you can use is
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'databasename'
AND table_name = 'tablename'
and check for results
you can use bellow code:
DECLARE #TABLENAME VARCHAR(20)='TableName';
IF (OBJECT_ID(#TABLENAME) IS NOT NULL )
BEGIN
execute(N'TRUNCATE TABLE ' + #TABLENAME + '' );
END
ELSE
BEGIN
PRINT 'Table NOT Exists'
END