Check If MySQL Table Exists Without Using "Select From" Syntax

Check if MySQL table exists without using select from syntax?

If you want to be correct, use INFORMATION_SCHEMA.

SELECT * 
FROM information_schema.tables
WHERE table_schema = 'yourdb'
AND table_name = 'testtable'
LIMIT 1;

Alternatively, you can use SHOW TABLES

SHOW TABLES LIKE 'yourtable';

If there is a row in the resultset, table exists.

Checking that a table exists on MySQL

Try using the information_schema to ask if the table exists. Something like

SELECT 
*
FROM
information_schema
WHERE TABLE_NAME = "$table_array"

Take a look through everything the information_schema holds, you will be pleasantly surprised by the information it has stored about your databases :)

Check if a table exists in mysql

You can try this codeigniter function to check table already exist.

   if ($this->db->table_exists('tbl_user')) {
// table exists (Your query)
} else {
// table does not exist (Create table query)
}

sql: select if table exists

You may handle this in two steps. First, check if the table exists in your Postgres database, and, if so, then select all records:

sql = "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = %s"
cur.execute(sql, ('your_table',))
if cur.fetchone()[0]:
sql_all = "SELECT * FROM your_table"
cur = connection.cursor() # get a new cursor
cur.execute(sql_all)
# process result set etc.

SQL Check if table exists then Create Table & Insert into same query

You could use CREATE TABLE ... SELECT syntax:

var query = "CREATE TABLE IF NOT EXISTS projects (project_name VARCHAR(10)) SELECT '" + projectName + "' AS project_name";

Demo on rextester

How to check if a table exists in MySQL using PHP PDO?

So i understand you will create the table, if it does not exist and insert data. So call first

$pdo->query("CREATE TABLE $TABLE IF NOT EXISTS;");

It will do nothing, when table exists.

And then insert your data.

$pdo->query("INSERT INTO $TABLE ... ");

No 'if then else' in PHP!

Check if table exists in SQL Server

For queries like this it is always best to use an INFORMATION_SCHEMA view. These views are (mostly) standard across many different databases and rarely change from version to version.

To check if a table exists use:

IF (EXISTS (SELECT * 
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'TheSchema'
AND TABLE_NAME = 'TheTable'))
BEGIN
--Do Stuff
END


Related Topics



Leave a reply



Submit