Raised This Month: $12 Target: $400
 3% 

SQL create column if not exists


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-03-2018 , 05:46   SQL create column if not exists
Reply With Quote #1

What query to use if I want to create a column if it does not exist?
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-03-2018 , 08:20   Re: SQL create column if not exists
Reply With Quote #2

PHP Code:
CREATE TABLE IF NOT EXISTS tablename (column...) 
mug1wara is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-03-2018 , 09:07   Re: SQL create column if not exists
Reply With Quote #3

Quote:
Originally Posted by mug1wara View Post
PHP Code:
CREATE TABLE IF NOT EXISTS tablename (column...) 
Table already exists so the new column is not added.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-03-2018 , 10:03   Re: SQL create column if not exists
Reply With Quote #4

PHP Code:
CREATE TABLE tablename (columncolumn...) 
This will just overwrite data, but if you want it like this, you have to do all the columns + the ones you want to add.

It's because 1 slot = 1 column, eg. FetchInt(column number). We don't want to switch places on the column numbers.
mug1wara is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 08-03-2018 , 11:28   Re: SQL create column if not exists
Reply With Quote #5

You shouldn't dynamically create columns, that means your database design is wrong.

If you explain what kind of data you're trying to store, someone can probably give you the correct solution.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-03-2018 , 11:31   Re: SQL create column if not exists
Reply With Quote #6

@DJ Tsunami
My concern was to add columns to a database which is used by another plugin.
mug1wara is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-03-2018 , 15:16   Re: SQL create column if not exists
Reply With Quote #7

Quote:
Originally Posted by DJ Tsunami View Post
You shouldn't dynamically create columns, that means your database design is wrong.

If you explain what kind of data you're trying to store, someone can probably give you the correct solution.
It was wrong I wanted to fix it through code without wiping all data..
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 08-03-2018 at 15:16.
eyal282 is offline
Ermert1992
Member
Join Date: Jan 2012
Location: Germany
Old 08-03-2018 , 17:09   Re: SQL create column if not exists
Reply With Quote #8

Try this: CREATE TABLE tablename (column), of course you cann add more columns (column, column)
__________________
Mfg Jonas


Ermert1992 is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-03-2018 , 17:45   Re: SQL create column if not exists
Reply With Quote #9

@Ermert1992

That's what I said... >:[

Last edited by mug1wara; 08-03-2018 at 17:45.
mug1wara is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 08-03-2018 , 21:22   Re: SQL create column if not exists
Reply With Quote #10

You can use ALTER TABLE to add new columns.

ALTER TABLE tablename ADD columname columntype

The more rows your table has, the longer ALTER TABLE will take.

Edit: Having said that, this is an operation you should be doing manually and as a one-off thing.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 08-03-2018 at 22:55.
Powerlord is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:31.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode