SQL-MYSQL Can't create Database - mysql

I always get the error:
CREATE DATABASE bundesliga ERRORCODE 1007 CANT CREATE DATABASE bundesliga, database exists
Here is my code:
CREATE DATABASE bundesliga;
DROP TABLE IF EXISTS Liga;
CREATE TABLE Liga (
);
DROP TABLE IF EXISTS Spiel;
CREATE TABLE Spiel ();

Your Database Bundesliga already exists.
you have to drop your database first and then recreate it.
DROP DATABASE bundesliga;
CREATE DATABASE bundesliga;
DROP TABLE IF EXISTS Liga;
CREATE TABLE Liga (
);
DROP TABLE IF EXISTS Spiel;
CREATE TABLE Spiel ();
or use this
CREATE DATABASE IF NOT EXISTS bundesliga;
and to check if database exist.
SHOW DATABASES LIKE 'bundesliga';

You can use an IF NOT EXISTS clause to prevent the error:
CREATE DATABASE IF NOT EXISTS bundesliga;
If the database already exists, this does nothing. If it doesn't exist, it will be created.

Your database already exists. See error code 1007 here.

Your Database Bundesliga already exists. If you want to recrate you have to drop this before.

Related

flyway create table migration if already exists

I have a create table migration, I had to run flyway repair and because that table already exists in database I can't perform flyway migrate , it fails with
Caused by: java.sql.SQLSyntaxErrorException: Table 'TABLE_NAME' already exists
error message.
Is there any way to fix it without dropping database ? Since it already was populated with data.
I ended up creating those rules for creating a migration:
If creating a new table, always use IF NOT EXISTS (thank to #ShaharT 's advice)
CREATE TABLE IF NOT EXISTS table_name(
--params
);
When altering a TABLE
We are declaring a procedure that can handle the 1060 MySQL error (column already exists) and adding ALGORITHM and LOCK arguments.
delimiter ;;
DROP PROCEDURE IF EXISTS alterNoFailure;;
create procedure alterNoFailure ()
begin
declare continue handler for 1060 begin end;
ALTER TABLE table_name
ADD COLUMN column_name double DEFAULT 0 NOT NULL,ALGORITHM=INPLACE, LOCK=NONE;
end;;
call alterNoFailure();;
You can specify multiple ALTER queries inside the procedure

dump from phpMyAdmin syntax error

So I am getting error from a CREATE TABLE even though that command is a dump from phpMyAdmin. I exported the the database and is trying to modify the structure through a text editor instead. I have not modified this command in anyway ( I have modified other parts). If I remove this CREATE TABLE then importing the .sql file goes through without a problem.
Just remove --6 in front of CREATE TABLE statement
--6 CREATE TABLE IF NOT EXISTS `owner` ( ...
^^^^
Should be just
CREATE TABLE IF NOT EXISTS `owner` ( ...
Here is SQLFiddle demo

Error in event creation using MySQL

Am using the below code to create an EVENT in MYSQL. In this time i want to drop and create a table using a query.
Drop Event if exists EVT_UP_TIMESHEET;
CREATE EVENT EVT_UP_TIMESHEET
ON SCHEDULE EVERY '1' Day
STARTS '2012-08-01 12:00:00'
DO
Drop table if exists tbl_temp;
create table tbl_temp as ( SELECT e.userid AS Employee_ID,
e.memo AS Employee_Name,
e.Department AS Department,
.....
It returns the following error:
ERROR : Table tbl_temp already exists.
Please help me to do this.
Use CREATE TABLE IF NOT EXISTS tbl_temp instead of create table tbl_temp
Or to delete the table you can use TRUNCATE TABLE instead of DROP TABLE and to create you can use INSERT...SELECT instead of CREATE TABLE.
As a workaround - try to use TRUNCATE TABLE and INSERT...SELECT statements instead of DROP/CREATE TABLE.

MySql - DROP table 'whatever' if exist ELSE create table 'whatever'?

I simply want to drop the table 'whatever' if it exist and then recreate table 'whatever' in a single query if possible.
DROP TABLE IF EXISTS `whatever` ELSE
CREATE TABLE `whatever`
Any idea ?
CREATE TABLE `whatever` IF NOT EXISTS ELSE TRUNCATE `whatever`
Use TRUNCATE to empty the table and reset cardinality instead of deleting the table and recreating it.

MySql - Create Table If Not Exists Else Truncate?

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.
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:
CREATE TEMPORARY TABLE IF NOT EXISTS fubar ( id int, name varchar(80) )
TRUNCATE TABLE fubar
INSERT INTO fubar SELECT * FROM barfu
or just drop and recreate
DROP TABLE IF EXISTS fubar
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.)
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
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
how about:
DROP TABLE IF EXISTS fubar;
CREATE TABLE fubar;
Or did you mean you just want to do it with a single query?
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!
If you're using PHP, use mysql_list_tables to check that the table exists before TRUNCATE it.