Creating tables in MySQL - mysql

I created a table license_serial in my "database_name". But when I tried creating a second table named license, it says that it already exists.
Why is that ? My database contains only a license_serial.frm and a db.opt.
mysql> SHOW TABLES;
+---------------------+
| Tables_in_mobilemp3 |
+---------------------+
| license_serial |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from license;
ERROR 1146 (42S02): Table 'mobilemp3.license' doesn't exist
Creating the second table:
CREATE TABLE `license` (
`id` int(10) unsigned NOT NULL auto_increment,
`serial_id` int(10) unsigned NOT NULL,
`uid` bigint(20) unsigned NOT NULL,
`active` tinyint(1) NOT NULL,
`first_seen` timestamp NOT NULL default CURRENT_TIMESTAMP,
`last_seen` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `serial_uid` (`serial_id`,`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
gives the following error message:
ERROR 1050 (42S01): Table 'mobilemp3.license' already exists
EDIT:
And the solution is this(in this order):
drop database databasename;
create database databasename;
use databasename;

The only thing I can think of, is that the table was created e.g. by root and the user you are using does not have access to that table. In that case you cannot select from it (not sure if it would be filtered out in the show tables command though)
Log in as root to that database and check if that table exists.

Related

MariaDB / MySQL adds DEFAULT NULL to column definition, how to change that?

When I create table with this statement:
CREATE TABLE `change_log` (
`object_id` int(11)
)
MariaDB creates table change_log which has DEFAULT NULL in it's table definition for object_id column. That is, for the statement:
SHOW CREATE TABLE change_log
it returns:
CREATE TABLE `change_log` (
`object_id` INT(11) NULL DEFAULT NULL
)
How can I configure mariadb/mysql not to add DEFAULT NULL in this case?
I use MariaDB 10.2.14 on Windows 10
You only must add DEFAULT NULL like this:
CREATE TABLE `change_log` (
`object_id` int(11) DEFAULT NULL
) ENGINE=InnoDB;
SELECT ##sql_mode;
Do you see STRICT_ALL_TABLES or STRICT_TRANS_MODE?
What happens when you include (or exclude) those from ##sql_mode before the CREATE TABLE? Before the INSERT?

Got error 4239 'Trigger with given name already exists' from NDBCLUSTER

We are running a MySQL Cluster Version:
mysql> SELECT VERSION();
+------------------------------+
| VERSION() |
+------------------------------+
| 5.6.15-ndb-7.3.4-cluster-gpl |
+------------------------------+
Trying to create a table
CREATE TABLE xy (
xa VARCHAR(36) NOT NULL DEFAULT '',
xb VARCHAR(255) NOT NULL,
xc TIMESTAMP NOT NULL,
xd VARCHAR(36) DEFAULT NULL,
xe VARCHAR(36) DEFAULT NULL,
xf VARCHAR(255) DEFAULT NULL,
xg VARCHAR(255) DEFAULT NULL,
xh TEXT,
xi BIGINT(20) DEFAULT NULL,
xj VARCHAR(255) DEFAULT NULL,
xk VARCHAR(255) DEFAULT NULL,
xl VARCHAR(255) DEFAULT NULL,
xz VARCHAR(255) DEFAULT NULL,
xy VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (xa)
) engine=ndb;
brings me (using the direct input over command line):
ERROR 1296 (HY000): Got error 4239 'Trigger with given name already exists' from NDBCLUSTER
and via file:
ERROR 1296 (HY000) at line 8: Got error 4239 'Trigger with given name already exists' from NDBCLUSTER
But there are no mysql triggers:
mysql> SHOW triggers;
Empty set (0.00 sec)
and no tables:
mysql> show tables;
Empty set (0.01 sec)
Anyone got an idea?
Ok - We got it!
The MaxNoOfTriggers in config.ini has been reached.
From the Official documentation -> MaxNoOfTriggers:
Internal update, insert, and delete triggers are allocated for each unique hash index. (This means that three triggers are created for each unique hash index.) However, an ordered index requires only a single trigger object. Backups also use three trigger objects for each normal table in the cluster.
Replication between clusters also makes use of internal triggers.
This parameter sets the maximum number of trigger objects in the cluster.
The default value is 768.

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)

mysql- Prevent id increment on unique

I have a table with unique constraint, which is working fine.
But the problem is when a duplicate record is tried to be inserted it fails due to unique constraint but, increments the id and next valid record get the id with double increment.
How to prevent id from incrementing if insertion fails?
UPDATE
Here is the table structure
CREATE TABLE `crawl_links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`priority` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`last_crawled_date` date DEFAULT NULL,
`crawl_status` varchar(255) COLLATE utf8_unicode_ci DEFAULT 'New',
`server_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`site_name_id` int(11) DEFAULT NULL,
`last_fetched_by` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_url` (`url`)
) ENGINE=InnoDB AUTO_INCREMENT=149 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
NOTE: I have taken this from dump file created
I have a links database. I collect links from a crawler and store it in the db. The links collected can be duplicate. So to prevent, I added UNIQ constraint on url field. The constraint is successfully preventing the insertion of duplicate records. But however it increments the id even if the insert is failed. My id column is INT(11) and current snapshot of my db shows max(id) = 128961841 but number of records count(*) crawl_links = 700231.
First of all - I agree to SergeS, in general it should not matter at all if there are gaps between the ids.
But still I am curious, since I never had such behavior, so I tried to reproduce:
mysql> CREATE TABLE foo (
id TINYINT UNSIGNED NOT NULL auto_increment,
bar CHAR(2) NOT NULL,
PRIMARY KEY( id ),
UNIQUE( bar ) ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO foo SET bar = 'a';
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO foo SET bar = 'a';
ERROR 1062 (23000): Duplicate entry 'a' for key 2
mysql> INSERT INTO foo SET bar = 'b';
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM foo;
+----+-----+
| id | bar |
+----+-----+
| 1 | a |
| 2 | b |
+----+-----+
2 rows in set (0.00 sec)
I tried with InnoDB and MyISAM but was not able to build gaps that way. Could you please describe your table setup and your insert?
There is a command to manually set the next ID to be used:
ALTER TABLE tbl AUTO_INCREMENT = 100;
You'll probably have to check if the last INSERT falied due to violation of the UNIQUE contraint, then apply the query above when necessary.

How do I find if my a table is MyISAM or Innodb [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I check MySQL engine type for a specific table?
Assuming that users is a table following command does not reveal if users table is MyISAM or Innodb.
desc users;
How do I find what is the type of users table?
You can use SHOW TABLE STATUS to see table information.
SHOW TABLE STATUS WHERE `Name` = 'my_table';
Simply check the value of the Engine column in the returned dataset to know which engine the table is using.
SELECT ENGINE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='your_table_name'
AND TABLE_SCHEMA='your_database_name';
-- or use TABLE_SCHEMA=DATABASE() if you have a default one.
You can use SHOW CREATE TABLE and look for the ENGINE part in the response.
SHOW CREATE TABLE users;
Example:
CREATE TABLE innodb_table (id int, value int) ENGINE=INNODB;
CREATE TABLE myisam_table (id int, value int) ENGINE=MYISAM;
CREATE TABLE default_table (id int, value int);
Result for innodb_table:
SHOW CREATE TABLE innodb_table;
+--------------+----------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------------+----------------------------------------------------------------------------------------------------------------------------------+
| innodb_table | CREATE TABLE `innodb_table` (
`id` int(11) DEFAULT NULL,
`value` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------------+----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Result for myisam_table:
SHOW CREATE TABLE myisam_table;
+--------------+----------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------------+----------------------------------------------------------------------------------------------------------------------------------+
| myisam_table | CREATE TABLE `myisam_table` (
`id` int(11) DEFAULT NULL,
`value` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+--------------+----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Result for default_table:
SHOW CREATE TABLE default_table;
+---------------+-----------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------+
| default_table | CREATE TABLE `default_table` (
`id` int(11) DEFAULT NULL,
`value` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)