Mysql Query SELECT auto_increment given null - mysql

query create table :
CREATE TABLE `tb_master_status` (
`tbms_id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`tbms_desc` varchar(255) DEFAULT NULL,
`tbms_id_parent` bigint DEFAULT NULL,
PRIMARY KEY (`tbms_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
after that I added some data to the table ..
but when I run this query, it returns NULL value :
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = "tb_master_status"
Why did it happen ?

you can try the permission for the user and following method
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "yourDatabaseName"
AND TABLE_NAME = "yourTableName"

Related

How to generate the table schema from INFORMATION_SCHEMA in MySQL?

I trying to generate table structure using INFORMATION_SCHEMA in MySQL.
I need the same output as
SHOW CREATE TABLE Mytablename;
My intention is to generation create table script for list of tables in mysql.
please help. I need to take table scripts for 100 tables. like below
CREATE TABLE `customer_list` (
`ID` smallint(5) unsigned NOT NULL DEFAULT '0',
`name` varchar(91) DEFAULT NULL,
`address` varchar(50) NOT NULL,
`zip_code` varchar(10) DEFAULT NULL,
`phone` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`country` varchar(50) NOT NULL,
`notes` varchar(6) NOT NULL DEFAULT '',
`SID` tinyint(3) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SHOW CREATE TABLE your_table_name;
The SQL query to get the table structure from the INFORMATION SCHEMA is as follows, where the database name is "dbname" and the table name is "Mytablename":
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'Mytablename';

SQL select for serial column

Assume I have a data with columns :
aa1, aa2, aa3, aa4, aa5, aa6
and so on.
I 'm looking for a select query where I can just mention something like :
select aa[1 to n] from table...
Is there any way to do it directly ?
Thanks
In MariaDB there is a system settings in database information_schema where are defined all schemas, tables, columns, etc.
Using this you can list all details about the table and use it to compose the "dynamic" query as you need.
Example of table definition:
CREATE TABLE `tablename` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`aa1` INT(11) NULL DEFAULT NULL,
`aa2` INT(11) NULL DEFAULT NULL,
`aa3` INT(11) NULL DEFAULT NULL,
`aa4` INT(11) NULL DEFAULT NULL,
`aa5` INT(11) NULL DEFAULT NULL,
`aa6` INT(11) NULL DEFAULT NULL,
`aa7` INT(11) NULL DEFAULT NULL,
`aa8` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_bin'
ENGINE=InnoDB
AUTO_INCREMENT=3
;
The query to information_schema to list the tables:
SELECT GROUP_CONCAT(COLUMN_NAME) FROM information_schema.`COLUMNS`
WHERE TABLE_SCHEMA = 'stackoverflow'
AND TABLE_NAME = 'tablename'
AND COLUMN_NAME LIKE 'aa%'
And the result:
aa1,aa2,aa3,aa4,aa5,aa6,aa7,aa8
So you can then built your query as needed.
Edit:
In SQL documentation is said: "Do not use INFORMATION_SCHEMA views to determine the schema of an object. The only reliable way to find the schema of a object is to query the sys.objects catalog view."

Slow query when use order by with limit 1

I have a table in mysql with over 80M records. In MYISAM engine.
When I run this query
SELECT id FROM mytable WHERE (key1=-5) AND key2=467476 ORDER BY id DESC LIMIT 1
query is slow and after 5 minutes I must kill query to release table.
But when i just increase limit size this query work successfully in 44ms
For example (I just increase limit size):
SELECT id FROM mytable WHERE (key1=-5) AND key2=467476 ORDER BY id DESC LIMIT 2
Now to solve this issue i try the following query and this work successfully (this is temporary solution)
SELECT id FROM (SELECT id FROM mytable WHERE (key1=-5) AND key2=467476 ORDER BY id DESC LIMIT 2) AS tbl ORDER BY id DESC LIMIT 1
Note
: id is primary and auto increment !
Update:
key1 ==> folder_id
key2 ==> userid
CREATE TABLE `bm60_mails` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL DEFAULT '0',
`betreff` varchar(255) CHARACTER SET utf8 NOT NULL,
`von` varchar(255) CHARACTER SET utf8 NOT NULL,
`an` varchar(255) CHARACTER SET utf8 NOT NULL,
`cc` varchar(255) CHARACTER SET utf8 NOT NULL,
`body` longtext NOT NULL,
`folder` int(11) NOT NULL,
`datum` int(11) NOT NULL DEFAULT '0',
`trashstamp` int(11) NOT NULL,
`priority` enum('low','normal','high') NOT NULL,
`fetched` int(11) NOT NULL DEFAULT '0',
`msg_id` varchar(128) CHARACTER SET utf8 NOT NULL,
`virnam` varchar(128) CHARACTER SET utf8 NOT NULL,
`trained` tinyint(4) NOT NULL DEFAULT '0',
`refs` text CHARACTER SET utf8 NOT NULL,
`flags` int(11) NOT NULL DEFAULT '-1',
`size` int(11) NOT NULL,
`color` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `mailUser` (`userid`),
KEY `mailFlags` (`flags`),
KEY `mailFolder` (`folder`)
) ENGINE=MyISAM AUTO_INCREMENT=95953499 DEFAULT CHARSET=latin1
I suspect if you create a compound index on (key1, key2, id) your problem will vanish.
The id column is also included because that's not implicit in MyISAM tables, althought it is in InnoDB tables.

Table creation failed: (1064) You have an error in your SQL syntax

What's wrong with my code?
$query = "CREATE TABLE IF NOT EXISTS 'data_table' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'name' VARCHAR(255) NOT NULL,
'active' TINYINT NOT NULL DEFAULT '1',
'create_date' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'income' INT DEFAULT NULL,
PRIMARY KEY ('id')
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
$createTable = mysqli_query($connect, $query);
you do not need single quotes for the table and column names try without that:you can use " or backticks instead.
CREATE TABLE IF NOT EXISTS data_table(
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
name VARCHAR( 255 ) NOT NULL ,
active TINYINT NOT NULL DEFAULT '1',
create_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
income INT DEFAULT NULL ,
PRIMARY KEY ( id )
) ENGINE = MYISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT =1
Try:
$query = "CREATE TABLE IF NOT EXISTS `data_table` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`active` TINYINT NOT NULL DEFAULT 1,
`create_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`income` INT DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
Note that the single quote is replaced with the backtick

how to make my select query faster in mysql?

I've got a table in mysql:
CREATE TABLE `pdd_data` (
`pdd_id` INT(16) NOT NULL primary key auto_increment,
`vin` varchar(32) NOT NULL,
`time` TIMESTAMP NOT NULL,
`cmd` varchar(16) NOT NULL,
`data` varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
I insert 1,000,000 records into pdd_data, and I'll use queries frequently in the future as below:
select * from pdd_data where cmd = 4599;
select * from pdd_data where vin = 400;
select * from pdd_data where vin = 400 and cmd = 4599;
Currently, the query time is about 1.20sec~1.90sec. Could anyone give me some suggestions on how to make this query faster?
p.s. I create a table using index:
CREATE TABLE `pdd_data1` (
`pdd_id` INT(16) NOT NULL primary key auto_increment,
`vin` varchar(32) NOT NULL,
`time` TIMESTAMP NOT NULL,
`cmd` varchar(16) NOT NULL,
`data` varchar(128) NOT NULL,
index idx_vin_cmd (vin(32), cmd(16))
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
But no improvement on select query.
My suggestion is do not use select *. Instead of using select * use select pdd_id, vin,time, cmd, data. This will definitely reduce your execution time.