Check whether a column exists or not in a table? - mysql

If you don't know what are the columns(or how many columns are there) in a table, how to check whether a particular column exists or not in that table using MySQL?
For example,
A table "DemoTable" contains columns Col1, Col2, Col3 with some data. And you write a select query like,
Select col1, col2, col3, col4 from DemoTable. But col4 does not exists in the DemoTable, so it will through an error. How to check whether col4 exists in the DemoTable in the select query or before the select query? Without a procedure.
There are two tables with same name in two DB's, in those tables there are few columns with same name, while others columns have different names. For example, DemoTable has columns col1, col2, col3, col4 in one DB and in other DB, DemoTable has columns col1, col2, col3, col5, col6. But the condition is, a single select query should be used to fetch data from the table without an error, so we need to check whether that columns first exists or not. Query like Select col1, col2, col5 from DemoTable from any DB.

Related

UPSERT with non-unique index

I need to implement concurrent-safe UPSERT using a non-unique key and avoid unnecessary auto-increment of ID.
Traditional INSERT ... ON DUPLICATE KEY doesn't work for me, so I'm performing:
INSERT INTO table (col1, col2, col3, col4, col5)
SELECT 1, 2, 'value3', 'value4', 'value5'
WHERE NOT EXISTS (SELECT 1
FROM table
WHERE col3 = 'value3'
AND col4 = 'value4'
AND col5 = 'value5')
then if it results in no row inserted, I'm performing:
UPDATE table
SET col1 = col1 + 1,
col2 = MAX(col2, 2)
WHERE col3 = 'value3'
AND col4 = 'value4'
AND col5 = 'value5'
There's an index:
CREATE INDEX ON table (col3, col4, col5)
It is non-unique as there are legacy data that does not allow me to declare it unique. Newer records, however, should not have duplicated (col3, col4, col5) rows.
Unsurprisingly, using the given INSERT statement I'm getting mixed results trying to execute it concurrently from two sessions. I can see the second session blocking until the first one commits its transaction, but then the second transaction is also able to insert a new row sometimes (or sometimes it achieves the expected of avoiding to insert a duplicate (col3, col4, col5) row).
I'm currently performing manual unique-check after the insert:
SELECT COUNT(1)
FROM table
WHERE col3 = 'value3'
AND col4 = 'value4'
AND col5 = 'value5'
but I've also tried:
INSERT INTO table (col1, col2, col3, col4, col5)
SELECT 1, 2, 'value3', 'value4', 'value5'
WHERE NOT EXISTS (SELECT 1
FROM table
WHERE col3 = 'value3'
AND col4 = 'value4'
AND col5 = 'value5'
FOR UPDATE)
which appears to work with the examples I'm always getting a duplicate (col3, col4, col5) row, otherwise. Is the given FOR UPDATE usage reliable for the purpose of ensuring no duplicate (col3, col4, col5) row will be inserted?
I'm using READ-COMMITTED transaction isolation.
MySQL 8.0.13 and higher supports functional key parts that index expression values rather than column or column prefix values. (link)
Because you have a unique field, i am assuming this is col, you can add an index like:
CREATE unique INDEX idx2 ON `mytable` ((col1>42),col3, col4, col5);
Where 42 should be the next auto-increment for col1.
Newly create records will be unique on the 3 columns, without affecting your 'old' data.
It is even possible to update the old data (as long as col1<=42).

how to find out a result set by which column ? if we write 3 columns in where clause with OR Operators on single table

I have a database table. That table has 4 columns. In 3 columns members(values) want to access the 4th column value.
So here i don't want to write same query for every member. I want to write only single query. So is it possible with single query? If possible how I can know which column has given those result set?
select 4thcolumn from tablename where lstcolumn=?1 or 2ndcolumn=?2 or 3rdcolumn=?3;
Using OR is a solution (but that requires you to repeat the parameter three times):
SELECT col4 FROM mytable col1 =:myvalue OR col2 =:myvalue OR col3 = :myvalue;
One solution to shorten the query (and pass a unique parameter) is to use IN:
SELECT col4 FROM mytable WHERE :myvalue IN (col1, col2, col3)
If you want to know which column matched, then this gets longer. In MySQL you can do:
SELECT
col4,
col1 = :myvalue is_col1,
col2 = :myvalue is_col2,
col3 = :myvalue is_col3
FROM mytable
WHERE :myvalue IN (col1, col2, col3)
This adds three columns in the result set: is_col1, is_col2, is_col3; the column(s) that matched will have value 1, other(s) will show 0.

Can I import an exiting table on mysql and override some existing data?

I have a mysql table in my host named article, I want to import article.sql from the localhost to save the changes that I have created on localhost, how can I import that table without deleting it? There are tables connected to article table that's why as much as possible I'm avoiding to delete it.
Consider the following steps:
Open your localhost exported article.sql file (a simple text file).
Change the CREATE TABLE SQL statement to reference a different target table such as article_temp in order to not conflict with live, production database table, article.
Again, change any INSERT INTO, UPDATE, and any other queries to reference a different target table.
Run the .sql file on the live server. MySQL allows the source command. Be sure to back up original table just in case during this process.
Run any one of the three append queries below with either LEFT JOIN NULL, NOT IN, or NOT EXISTS clauses (an often debated question in database industry on which is more efficient). The key is to find a comparative/matching value (i.e,. article name). Any one below should work, no need to run all of them (but of course each checks for duplicates!)
SQL Append Queries
INSERT INTO article (col1, col2, col3, col4, col5)
SELECT col1, col2, col3, col4, col5
FROM article_temp
LEFT JOIN article.value = article_temp.value
WHERE article.value IS NULL;
INSERT INTO article (col1, col2, col3, col4, col5)
SELECT col1, col2, col3, col4, col5
FROM article_temp
WHERE article_temp.value NOT IN
(SELECT value FROM article);
INSERT INTO article (col1, col2, col3, col4, col5)
SELECT col1, col2, col3, col4, col5
FROM article_temp
WHERE NOT EXISTS
(SELECT value FROM article
WHERE article.value = article_temp.value);
Of course once done with the staging and migration, drop the temp table:
DROP TABLE article_temp;

How to insert data from one table to anther table?

I have two tables,
tblA(id, num, col1, col2, col3),
tblB(col1, col2, col3)
col1, col2 and col3 are the same in both tables. Now I have following sql:
declare #num...(same type as num)
insert into tblA
select #num, * from tblB
id in tblA is an indentity column.
But I got following error,
Column name or number of supplied values does not match table definition.
Can anyone help me to fix it?
Can you please try providing the column names as well,
declare #num...(same type as num)
insert into tblA(num, col1, col2, col3)
select #num, * from tblB
Please don't worry about identity column as it will get filled automatically.
Just INSERT using named columns, and skip the identity column - it will be filled automatically:
INSERT INTO tblA (num, col1, col2, col3) SELECT #Num, col1, col2, col3 FROM tblB
I think the error message is quite explicative: the SELECT and the INSERT has to have the same number of columns.
in your case
declare #num...(same type as num)
insert into tblA(num,col1, col2, col3)
select #num,col1, col2, col3 from tblB
if the key on tblA is not auto-generated you have to consider it in the INSERT
more info here
It simply based on your column name they should be of same type:
insert into tblA(col1,col2,col3)
select col1,col2,col3
from tblB

SQL Synchronising large table data

Hi i m using SQL 2008 R2.What is the faster way to insert data of 10 mil. records from one table A into another empty table B.
Table A and B dont have same schema similar but not the same.
It's only 10 million rows. Just use a normal INSERT
INSERT tableB (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM tableA
or
--assumes no table b already
SELECT col1, col2, col3, ..., INTO TableB FROM tableA
.. now add some columns etc
Your using sql?
why not create a new table
and then insert records from table a into new table b??