MySQL - Find table from constraint name - mysql

I have an error popping up that throws an error like:
IntegrityError: (999, "Duplicate entry 'XXXXX' for key 'constraint_name_here_uniq'")
So I have the constraint name, is there an easy way to find out what table, columns are referenced in the mysql command line? It's a very large database and tried poking around a few tables with SHOW CREATE TABLE with no luck, I also tried DESC <constraint name> but that didn't work either.

This should work:
select *
from information_schema.KEY_COLUMN_USAGE
where CONSTRAINT_NAME ='constraint_name_here_uniq';
Example:
mysql> use information_schema;
Database changed
mysql> select * from KEY_COLUMN_USAGE where CONSTRAINT_NAME ='user_has_notification_types_user_idx' \G
*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: kanboard
CONSTRAINT_NAME: user_has_notification_types_user_idx
TABLE_CATALOG: def
TABLE_SCHEMA: kanboard
TABLE_NAME: user_has_notification_types
COLUMN_NAME: user_id
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
*************************** 2. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: kanboard
CONSTRAINT_NAME: user_has_notification_types_user_idx
TABLE_CATALOG: def
TABLE_SCHEMA: kanboard
TABLE_NAME: user_has_notification_types
COLUMN_NAME: notification_type
ORDINAL_POSITION: 2
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
2 rows in set (1.70 sec)
And the table using the index:
mysql> use kanboard;
Database changed
mysql> show create table user_has_notification_types;
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_has_notification_types | CREATE TABLE `user_has_notification_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`notification_type` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_has_notification_types_user_idx` (`user_id`,`notification_type`),
CONSTRAINT `user_has_notification_types_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)

Related

MySQL 'NOT NULL' Error

I have question about 'NOT NULL' error. I need to make a table with variable type: 'SERIAL' and length of 7. Here is an error that SQL sends to me:
SQL query:
CREATE TABLE `table42`.`CheckOuts ` (
`CheckOutID` SERIAL( 7 ) NOT NULL
) ENGINE = MYISAM ;
The error that is returned:
#1064 - 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 '(7) NOT NULL) ENGINE = MyISAM' at line 1
I think your problem is that SERIAL isn't supported in that method for MySQL. Docs suggest you should be using AUTO_INCREMENT instead:
see: http://www.sqlines.com/mysql/auto_increment
also: What is the difference between SERIAL and AUTO_INCREMENT in mysql
These suggest you should be creating your table with the following example syntax:
create table myfriends (
id int primary key auto_increment,
frnd_name varchar(50) not null
);
The SERIAL type already has NOT NULL as part of its definition, so your NOT NULL declaration is redundant
Error is not due to NOT NULL, although SERIAL already includes NOT NULL, an error does not occur when duplicating it. The cause of the error is for the length that you try to assign (7):
mysql> DROP TABLE IF EXISTS `CheckOuts`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `CheckOuts` (
-> -- `CheckOutID` SERIAL(7) NOT NULL
-> `CheckOutID` SERIAL NOT NULL
-> ) ENGINE=MYISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW CREATE TABLE `CheckOuts`\G
*************************** 1. row ***************************
Table: CheckOuts
Create Table: CREATE TABLE `CheckOuts` (
`CheckOutID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
UNIQUE KEY `CheckOutID` (`CheckOutID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
I don't understand what you're trying to do, but if you need to assign a length, you'll need something like:
mysql> DROP TABLE IF EXISTS `CheckOuts`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `CheckOuts` (
-> `CheckOutID` INT(7) UNSIGNED SERIAL DEFAULT VALUE
-> ) ENGINE=MYISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW CREATE TABLE `CheckOuts`\G
*************************** 1. row ***************************
Table: CheckOuts
Create Table: CREATE TABLE `CheckOuts` (
`CheckOutID` int(7) unsigned NOT NULL AUTO_INCREMENT,
UNIQUE KEY `CheckOutID` (`CheckOutID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
See documentation:
11.1.1 Numeric Type Overview
...
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
UNIQUE.
SERIAL DEFAULT VALUE in the definition of an integer column is an
alias for NOT NULL AUTO_INCREMENT UNIQUE.
...

Where oh where is my FULLTEXT index?

Okay, I'm fully prepared to be told this is something dumb. I've got a table like so:
mysql> show create table node\G
*************************** 1. row ***************************
Table: node
Create Table: CREATE TABLE `node` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`graph` varchar(100) CHARACTER SET latin1 DEFAULT NULL,
`subject` varchar(200) NOT NULL,
`predicate` varchar(200) NOT NULL,
`object` mediumtext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `nodeindex` (`graph`(20),`subject`(100),`predicate`(100),`object`(100)),
KEY `ix_node_subject` (`subject`),
KEY `ix_node_graph` (`graph`),
KEY `ix_node_object` (`object`(255)),
KEY `ix_node_predicate` (`predicate`),
KEY `node_po` (`predicate`,`object`(130)),
KEY `node_so` (`subject`,`object`(130)),
KEY `node_sp` (`subject`,`predicate`(130)),
FULLTEXT KEY `node_search` (`object`)
) ENGINE=MyISAM AUTO_INCREMENT=574161093 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
Note the line FULLTEXT KEYnode_search(object). When I try the query
mysql> select count(*) from node where match(object) against ('Entrepreneur');
I get the error
ERROR 1191 (HY000): Can't find FULLTEXT index matching the column list
Duh?
Update
I tried an ANALYZE TABLE to no aval
mysql> analyze table node;
+------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------+---------+----------+----------+
| xxxxxxxxxxx.node | analyze | status | OK |
+------------------+---------+----------+----------+
1 row in set (21 min 13.86 sec)

Foreign key and the primary key in the same table

I need to create a relationship between two attributes in the same table. So both primary key and the foreign keys are in the same table. Here I have a table called User_Type. Primary key is User_ID. It should be the foreign key of Parent_ID.
Ex:
User_Type
User_ID
User_Name
Parent_ID
User_Type_Division
But when I'm creating the relationship I get an error like this.
Cannot add or update a child row: a foreign key constraint fails
(mydb.user_type, CONSTRAINT Parent_User_Type FOREIGN KEY
(Parent_ID) REFERENCES user_type (User_ID) ON DELETE NO ACTION
ON UPDATE NO ACTION)").
Is there any way available to avoid this error. Please someone let me know.
And Here I have given the Query of the table.
CREATE TABLE IF NOT EXISTS `user_type` (
`User_ID` int(11) NOT NULL AUTO_INCREMENT,
`User_Name` varchar(45) NOT NULL,
`Parent_ID` int(11) DEFAULT NULL,
`User_Type_Division` varchar(45) DEFAULT NULL,
`User_ID_Format` varchar(45) DEFAULT NULL,
`Data_Entered_Person` varchar(45) DEFAULT NULL,
`Entered_Time` varchar(45) DEFAULT NULL,
PRIMARY KEY (`User_ID`),
UNIQUE KEY `User_Name_UNIQUE` (`User_Name`),
KEY `ParentUserType` (`Parent_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
There is a way to add and enforce foreign key on existing data.
Step 1: You need to stop foreign key checks for the current session.
SET FOREIGN_KEY_CHECKS=0;
Step 2: Add foreign key to your table.
ALTER TABLE myTable
ADD CONSTRAINT fk_name FOREIGN KEY ( columnName ) REFERENCES ...
Step 3: Enable foreign key checks.
SET FOREIGN_KEY_CHECKS=1;
Working Example:
mysql> create table fkchk( i int not null primary key auto_increment, n int );
Query OK, 0 rows affected (0.26 sec)
mysql> insert into fkchk(n) values ( 0 );
Query OK, 1 row affected (0.10 sec)
mysql> show variables like '%fore%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| foreign_key_checks | ON |
+--------------------+-------+
1 row in set (0.00 sec)
mysql> alter table fkchk
> add constraint fk_n foreign key (n) references fkchk(i)
> on delete no action;
ERROR 1452 (23000): Cannot add or update a child row:
a foreign key constraint fails
(`test`.<result 2 when explaining filename '#sql-6fc_14'>,
CONSTRAINT `fk_n` FOREIGN KEY (`n`)
REFERENCES `fkchk` (`i`) ON DELETE NO ACTION)
mysql> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%fore%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| foreign_key_checks | OFF |
+--------------------+-------+
1 row in set (0.00 sec)
mysql> alter table fkchk
> add constraint fk_n foreign key (n) references fkchk(i)
> on delete no action;
Query OK, 0 rows affected (0.50 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into fkchk(n) values ( 0 );
Query OK, 1 row affected (0.32 sec)
mysql> select * from fkchk;
+---+------+
| i | n |
+---+------+
| 1 | 0 |
| 2 | 0 |
+---+------+
2 rows in set (0.00 sec)
mysql> SET FOREIGN_KEY_CHECKS=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%fore%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| foreign_key_checks | ON |
+--------------------+-------+
1 row in set (0.00 sec)
mysql> insert into fkchk(n) values ( 1 );
Query OK, 1 row affected (0.12 sec)
mysql> insert into fkchk(n) values ( 0 );
ERROR 1452 (23000): Cannot add or update a child row:
a foreign key constraint fails
(`test`.`fkchk`,
CONSTRAINT `fk_n` FOREIGN KEY (`n`)
REFERENCES `fkchk` (`i`) ON DELETE NO ACTION)
mysql>
mysql> select * from fkchk;
+---+------+
| i | n |
+---+------+
| 1 | 0 |
| 2 | 0 |
| 3 | 1 |
+---+------+
3 rows in set (0.00 sec)
mysql>
Double check that the User_ID column and the Parent_ID column are of the same datatype.
Also, your User_ID key column is set to NOT NULL, but your Parent_ID column is set to null. that could be the problem. Set Parent_ID to NOT NULL and then try creating the FK relationship again. This is one of the options I was talking about. However, if you already have data in your table, this could be an issue too.

Issue with Collation in Mysql

I have a table as
mysql> show create table tbl_name\G
************* 1. row *************
Table: tbl_name
Create Table: CREATE TABLE `tbl_name` (
`name` char(15) CHARACTER SET latin1 DEFAULT NULL,
`id` int(5) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin7 COLLATE=latin7_estonian_cs
1 row in set (0.00 sec)
The DATA In Table is as
mysql> select * from tbl_name;
name id
manaf 1
manaf 2
MANAF 3
MAnaf 4
4 rows in set (0.00 sec)
Now i want the Case Sensitivity in all the records
mysql> select distinct(name) from tbl_name;
It should return the 3 rows in result but it is returing just 1.why..??
As I have set the table collation as latin7_estonian_cs
try this
SELECT DISTINCT BINARY value FROM tableName
Reference

Does MySQL rebuild a column's index, if the column is renamed?

The column name is the only thing that is changing... will MySQL rebuild that index?
create table t1(id int, name varchar(100));
alter table t11 add index name_idx(name);
mysql> show create table t11;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| t11 | CREATE TABLE `t11` (
`id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
KEY `name_idx` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
alter table t11 change column name name1 varchar(100);
mysql> show create table t11;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| t11 | CREATE TABLE `t11` (
`id` int(11) DEFAULT NULL,
`name1` varchar(100) DEFAULT NULL,
KEY `name_idx` (`name1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql will auto change the indexes for u
and does not rebuild index
Wouldn't have thought so for C-ISAM, not sure about INNODB. What happens when you try it?