What I am currently looking to do is write a script where I use CREATE TABLE IF NOT EXISTS statements to create missing tables. How exactly could I go about adding in some sort of output to the user who is using this script? It is essentially a long line of create table statements and I would like it to print out something like "this_table was added" at the end of the script. I want to output something for the tables that are not found, and are created.
CREATE TABLE IF NOT EXISTS`user_group` (
`key` varchar(255) NOT NULL,
`version` int(11) NOT NULL,
`display_order` int(11) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`color` varchar(10) DEFAULT NULL,
PRIMARY KEY (`key`)
) ENGINE=genericDB DEFAULT CHARSET=latin1;
Define a delimiter to run two queries together
mysql> delimiter //
mysql> create table Y(id int primary key)//select "Y Created";//
Query OK, 0 rows affected (0.13 sec)
+-----------+
| Y Created |
+-----------+
| Y Created |
+-----------+
1 row in set (0.00 sec)
Related
I am looking to decrease the quantity of stock(stockQuantity) each time customer purchases in main_product_info.
I am trying to use the below code but it looks like its not working, i am a new to DB so not sure if i am using the correct code.
Also as in the below code i am directly updating the quantity against the productNumner or shall i use join(i have seen few post around but couldn't undersrtand) and including the main_product_sub_category(as it has the primary key ) as well, just want to understand the correct way of doing it.
Error: stockQuantity is not defined.
Any suggestions please.
connection.query("UPDATE main_product_info SET stockQuantity=? WHERE main_product_info.producNumber=?", [stockQuantity - 1, checkQuantity], function (err, result) {}
// here i am looking to decrese stockQuantity by 1
-main_product_info
-Snippet of code using show create table,
mysql> SHOW CREATE TABLE main_Products_category;
+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| main_Products_category | CREATE TABLE `main_products_category` (
`productId` int NOT NULL AUTO_INCREMENT,
`productCategory` varchar(45) NOT NULL,
PRIMARY KEY (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=105 DEFAULT CHARSET=utf8 |
+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> SHOW CREATE TABLE main_products_sub_category;
+----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| main_products_sub_category | CREATE TABLE `main_products_sub_category` (
`main_Products_sub_category_id` varchar(45) NOT NULL,
`main_Products_sub_category_name` varchar(45) DEFAULT NULL,
`main_Products_category_productId` int NOT NULL,
PRIMARY KEY (`main_Products_sub_category_id`),
KEY `fk_main_Products_sub_category_main_Products_category1_idx` (`main_Products_category_productId`),
CONSTRAINT `fk_main_Products_sub_category_main_Products_category1` FOREIGN KEY (`main_Products_category_productId`) REFERENCES `main_products_category` (`productId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SHOW CREATE TABLE main_product_info;
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| main_product_info | CREATE TABLE `main_product_info` (
`producInfoId` varchar(45) NOT NULL,
`productDescription` mediumtext,
`stockQuantity` int DEFAULT NULL,
`producNumber` int DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`main_Products_sub_category_main_Products_sub_category_id` varchar(45) NOT NULL,
PRIMARY KEY (`producInfoId`),
KEY `fk_main_Product_Info_main_Products_sub_category1_idx` (`main_Products_sub_category_main_Products_sub_category_id`),
CONSTRAINT `fk_main_Product_Info_main_Products_sub_category1` FOREIGN KEY (`main_Products_sub_category_main_Products_sub_category_id`) REFERENCES `main_products_sub_category` (`main_Products_sub_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
You would do it in mysql direcly without parameter
connection.query("UPDATE main_product_info SET stockQuantity= stockQuantity - 1 WHERE main_product_info.producNumber=?", [checkQuantity], function (err, result) {}
// here i am looking to decrese stockQuantity by 1
I have table with this structure:
CREATE TABLE IF NOT EXISTS `message` (
`id` bigint(20) NOT NULL,
`appKey` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`text` text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and primary key (id,appKey)
I would like to have id autoincrement for every appKey independently. So I use trigger.
CREATE TRIGGER `messageId` BEFORE INSERT ON `message`
FOR EACH ROW begin
declare v_messageId bigint(20) unsigned default 0;
SELECT max(id) INTO v_messageId FROM message WHERE appKey = new.appKey;
if (v_messageId IS NULL) THEN
set v_messageId=0;
end if;
SET new.id=v_messageId + 1;
end
It works fine until i tried to insert two rows from two database connection (I am using connection pool in application) at same time. First row is inserted. But second throws error ER_DUP_ENTRY: Duplicate entry '18-secretkey' for key 'PRIMARY'.
I know why this is happening. My question is: Is MySQL possible to achieves this task or I have to use different database (probably PostgreSQL because of advisory lock)?
EDIT:
In table I have this rows:
id | appKey | text
---+--------+-----------
1 | key 1 | something
2 | key 1 | something
1 | key 2 | something
2 | key 2 | something
3 | key 1 | something
And the error is after I try to insert this two rows:
appKey | text
-------+-------
key1 | something
key1 | something
Personally I'd strongly suggest you don't manage ids on your own. There is no business value in doing so.
Now in case you want to stick with it, for some reason, at least make use of LAST_INSERT_ID(expr). It's the only multi-user safe way to generate sequences in MySQL. Also you would need additional table to store sequences per appKey.
CREATE TABLE message_seq (
appKey VARCHAR(64) NOT NULL PRIMARY KEY,
seq BIGINT(20) NOT NULL
);
Your trigger then will look like
DELIMITER //
CREATE TRIGGER `messageId`
BEFORE INSERT ON `message`
FOR EACH ROW
BEGIN
INSERT INTO message_seq(appkey, seq) VALUES (NEW.appKey, LAST_INSERT_ID(1))
ON DUPLICATE KEY UPDATE seq = LAST_INSERT_ID(seq + 1);
SET NEW.id = LAST_INSERT_ID();
END//
DELIMITER ;
Here is a SQLFiddle demo
Further reading:
LAST_INSERT_ID()
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.
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.
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)