MySql - Create Table If Not Exists Else Truncate?

2019-02-05 20:05发布

Here is the updated question:

the current query is doing something like:

$sql1 = "TRUNCATE TABLE fubar";
$sql2 = "CREATE TEMPORARY TABLE IF NOT EXISTS fubar SELECT id, name FROM barfu";

The first time the method containing this is run, it generates an error message on the truncate since the table doesn't exist yet.

Is my only option to do the CREATE TABLE, run the TRUNCATE TABLE, and then fill the table? (3 separate queries)

original question was:

I've been having a hard time trying to figure out if the following is possible in MySql without having to write block sql:

CREATE TABLE fubar IF NOT EXISTS ELSE TRUNCATE TABLE fubar

If I run truncate separately before the create table, and the table doesn't exist, then I get an error message. I'm trying to eliminate that error message without having to add any more queries.

This code will be executed using PHP.

6条回答
\"骚年 ilove
2楼-- · 2019-02-05 20:26

execute any query if table exists.

Usage: call Edit_table(database-name,table-name,query-string);

  • Procedure will check for existence of table-name under database-name and will execute query-string if it exists. Following is the stored procedure:
DELIMITER $$

DROP PROCEDURE IF EXISTS `Edit_table` $$
CREATE PROCEDURE `Edit_table` (in_db_nm varchar(20), in_tbl_nm varchar(20), in_your_query varchar(200))
DETERMINISTIC
BEGIN

DECLARE var_table_count INT;

select count(*) INTO @var_table_count from information_schema.TABLES where TABLE_NAME=in_tbl_nm and TABLE_SCHEMA=in_db_nm;
IF (@var_table_count > 0) THEN
  SET @in_your_query = in_your_query;
  #SELECT @in_your_query;
  PREPARE my_query FROM @in_your_query;
  EXECUTE my_query;

ELSE
  select "Table Not Found";
END IF;

END $$
DELIMITER ;

More on Mysql

查看更多
Juvenile、少年°
3楼-- · 2019-02-05 20:34

how about:

DROP TABLE IF EXISTS fubar;
CREATE TABLE fubar;

Or did you mean you just want to do it with a single query?

查看更多
ら.Afraid
4楼-- · 2019-02-05 20:35

OK then, not bad. To be more specific, the current query is doing something like:

$sql1 = "TRUNCATE TABLE fubar";
$sql2 = "CREATE TEMPORARY TABLE IF NOT EXISTS fubar SELECT id, name FROM barfu";

The first time the method containing this is run, it generates an error message on the truncate since the table doesn't exist yet.

Is my only option to do the "CREATE TABLE", run the "TRUNCATE TABLE", and then fill the table? (3 separate queries)

PS - thanks for responding so quickly!

查看更多
别忘想泡老子
5楼-- · 2019-02-05 20:36

If you're using PHP, use mysql_list_tables to check that the table exists before TRUNCATE it.

查看更多
不美不萌又怎样
6楼-- · 2019-02-05 20:48

shmuel613, it would be better to update your original question rather than replying. It's best if there's a single place containing the complete question rather than having it spread out in a discussion.

Ben's answer is reasonable, except he seems to have a 'not' where he doesn't want one. Dropping the table only if it doesn't exist isn't quite right.

You will indeed need multiple statements. Either conditionally create then populate:

  1. CREATE TEMPORARY TABLE IF NOT EXISTS fubar ( id int, name varchar(80) )
  2. TRUNCATE TABLE fubar
  3. INSERT INTO fubar SELECT * FROM barfu

or just drop and recreate

  1. DROP TABLE IF EXISTS fubar
  2. CREATE TEMPORARY TABLE fubar SELECT id, name FROM barfu

With pure SQL those are your two real classes of solutions. I like the second better.

(With a stored procedure you could reduce it to a single statement. Something like: TruncateAndPopulate(fubar) But by the time you write the code for TruncateAndPopulate() you'll spend more time than just using the SQL above.)

查看更多
地球回转人心会变
7楼-- · 2019-02-05 20:50

You could do the truncate after the 'create if not exists'. That way it will always exist... and always be empty at that point.

CREATE TABLE fubar IF NOT EXISTS
TRUNCATE TABLE fubar
查看更多
登录 后发表回答