AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   SQL create column if not exists (https://forums.alliedmods.net/showthread.php?t=309665)

eyal282 08-03-2018 05:46

SQL create column if not exists
 
What query to use if I want to create a column if it does not exist?

mug1wara 08-03-2018 08:20

Re: SQL create column if not exists
 
PHP Code:

CREATE TABLE IF NOT EXISTS tablename (column...) 


eyal282 08-03-2018 09:07

Re: SQL create column if not exists
 
Quote:

Originally Posted by mug1wara (Post 2607880)
PHP Code:

CREATE TABLE IF NOT EXISTS tablename (column...) 


Table already exists so the new column is not added.

mug1wara 08-03-2018 10:03

Re: SQL create column if not exists
 
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.

DJ Tsunami 08-03-2018 11:28

Re: SQL create column if not exists
 
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.

mug1wara 08-03-2018 11:31

Re: SQL create column if not exists
 
@DJ Tsunami
My concern was to add columns to a database which is used by another plugin.

eyal282 08-03-2018 15:16

Re: SQL create column if not exists
 
Quote:

Originally Posted by DJ Tsunami (Post 2607914)
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..

Ermert1992 08-03-2018 17:09

Re: SQL create column if not exists
 
Try this: CREATE TABLE tablename (column), of course you cann add more columns (column, column)

mug1wara 08-03-2018 17:45

Re: SQL create column if not exists
 
@Ermert1992

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

Powerlord 08-03-2018 21:22

Re: SQL create column if not exists
 
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.


All times are GMT -4. The time now is 06:44.

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