Select auto_increment field value - mysql

I'm trying to compare the auto increment field value of the current table with a constant value, the method I tried is below query:
WHERE (SELECT column_name FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name='clients') = '1'
It won't show any output, Is there anything wrong with it?

WHERE (SELECT AUTO_INCREMENT FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name='clients') = <your number>
You should try something like above

Related

Sql query to return column name, data type, data length, if a column is null

I have the following query:
SELECT column_name,
data_type
date_length
FROM information_schema.columns
WHERE table_schema = ’bookidz_ro_dev’
AND table_name = ’rack_level’;
And I get this error: #1054 - Unknown column '’bookidz_ro_dev’' in 'where clause'
even though the db name is correct and so is the table name.
As i tested your code is good, but those ’ quotations are not correct.
SELECT
*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'bookidz_ro_dev'
AND TABLE_NAME = 'rack_level';
I used * because there is no Date_Length column in information_schema.columns
Looks like You are using wrong quotations, try it with correct one:
SELECT column_name,
data_type
date_length
FROM information_schema.columns
WHERE table_schema = 'bookidz_ro_dev'
AND table_name = 'rack_level';

How to check if the table exists before inserting data?

How to make such as: IF (table if exists types) THEN ... END ?
Try this:
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = '[database name]'
AND table_name = '[table name]';
Save the result and you can use it in an if statement

How to search for truncated data with the original data in MySQL

Here is a database structure to illustrate my question
CREATE TABLE `company` (
`company_id` int(11) NOT NULL,
`name` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
When I add a company that has a name longer than 10 characters it will be truncated. So when I search the company with its full name I cannot find it.
Question: How can I get the maximum length of the « name » column wich is 10 in this example to be able to truncate and do the research with « SUBSTRING() »
NB : The context doesn’t permit to change the maximum length of the
name
The best way to do this is by querying the information_schema. Something like this should work for you.
select character_maximum_length
from information_schema.columns
where table_schema = database()
and table_name = 'company'
and column_name = 'name'
You'll get a one row resultset with the length you want.
You can make this into part of your query with a (nasty!) query pattern like this.
select company_id, name
from company
where name = substring('A long search string', 1, (
select character_maximum_length
from information_schema.columns
where table_schema = database()
and table_name = 'company'
and column_name = 'name') )
Or you could try this (more readable) consecutive pair of queries using a session variable.
select #max := character_maximum_length
from information_schema.columns
where table_schema = database()
and table_name = 'hotels'
and column_name = 'name' ;
select company_id, name
from company
where name = substring('A long search string', 1, #max)
You mean you're searching something like
SELECT * FROM company WHERE company = LEFT('something longer than 10 chars', 10);
?
LEFT() documentation
To get the length of a varchar column you have to query the information_schema.
SELECT character_maximum_length
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'company'
AND column_name = 'name';
So your query becomes
SELECT *
FROM company
WHERE company = LEFT('something longer than 10 chars', (
SELECT character_maximum_length
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'company'
AND column_name = 'name';
)
);

How can I get the AUTO_INCREMENT value in Sequel?

In MySQL, I can get the auto-increment value by:
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
How do I query this information in Sequel? When I tried DB.run and DB.get, it didn't seem to work.
If nothing else, Sequel makes it easy to send raw SQL:
DB["SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DatabaseName' AND TABLE_NAME = 'TableName'"]
From the documentation:
Using raw SQL
DB.run "CREATE TABLE users (name VARCHAR(255) NOT NULL, age INT(3) NOT NULL)"
dataset = DB["SELECT age FROM users WHERE name = ?", name]
dataset.map(:age)
DB.fetch("SELECT name FROM users") do |row|
p row[:name]
end

SQL Server: Checking the datatype of a column

How to check if a column in a table has a specific datatype?
For example, how to check if a column in SQL Server table is of datatype char(11)?
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'char'
and CHARACTER_MAXIMUM_LENGTH = 11
and TABLE_NAME = 'your_table'
using syscolumns:
SELECT name FROM SYSCOLUMNS
where length = 11
and xtype = 175 --char type
select case when DATA_TYPE= 'char' then 'T' else 'F' end,
case when CHARACTER_MAXIMUM_LENGTH = 11 then 'T' else 'F' end
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'MY_COLUMN_NAME'
and TABLE_NAME = 'MY_TABLE_NAME'
Sql Query for Checking the Data Type of Column
ALTER TABLE table_name MODIFY COLUMN column_name data_type;