Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will creating an SQL table that already exists overwrite it?

Tags:

sql

database

Very simply:

If I create a table in a database, will it delete or overwrite another table of the same name?

I have a script in a game that needs a table of information. Can I simply run this every time the script starts?:

CREATE TABLE player_data ( UniqueID string, Money int )

So that when someone else runs the script on their game, the table will be created, but if they run it again, it wont be overwritten?

like image 472
Jonas Avatar asked Dec 24 '22 05:12

Jonas


2 Answers

It depends on which database engine you are using. In MySQL there's CREATE TABLE IF NOT EXISTS to make it will only try to create the table if it does not already exist.

So your statement would look like CREATE TABLE IF NOT EXISTS player_data ( UniqueID string, Money int ). If there was no table named player_data then a new table would be created. However if there was already a table named player_data then no modifications would occur to the database.

There are usually alternatives for most popular Database Engines to do something similar.

like image 113
Saqib Rokadia Avatar answered Jan 05 '23 00:01

Saqib Rokadia


but if they run it again, it wont be overwritten?

No. In general you have to DROP the table first before CREATE succeeds.

There are lots of exceptions to this when it comes to actual implementations, such as if the table is temporary or permanent, access rights where multiple users can have the same table name as long as they all use different user names. Look in your particular server version for such exceptions.

like image 28
Emacs User Avatar answered Jan 04 '23 23:01

Emacs User