How do I disable a column in a MySQL table? - mysql

I'm updating an old MySQL database and find that several columns in various tables are not needed. But rather than just remove the columns, I'd like to disable them temporarily so I can test the code and see how many things break before I decide to complete remove the column. How can I tell the server to throw an error if a particular column is accessed?

You may rename table oldname to newname and create view using oldname. that view look like column removed table. if have some problem. you just change name table and view.

You might want to consider using a view for this. As an example I created this fiddle : http://sqlfiddle.com/#!2/a8947/2
Take a look at the two queries. One selects the table with the column you aren't sure you need (which I called "legacyData"). The other selects the view. You can see that in the view, the column "legacyData" no longer exists.
I don't think this will work if you are writing from the table, but if you are reading from the table a view is a viable option.
The steps you'd want to take in production to try it out would be:
Rename your table ('myTable' to 'myTableDontUse', for example)
Create the view based on the newly renamed table
When you name the view, name it the table's original name (myTable in this example)
By doing that, you've replaced your table with a view. This approach would allow you to see if applications are accessing the legacyData field of that table. If not, you can remove the view, rename your table, and drop the column in question.
Performance typically is not great with a view especially if you have a lot of data, so be careful of that. In a similar situation, I started out with a view but due to performance reasons (table had 2M+ rows), we changed to a normal table that was instead populated via triggers.

Related

What is MySQL View for?

MySQL Documentation explains how to create a view. However it doesn't explain why should I make a MySQL View in the first place.
Thus, my question is, what is MySQL View? What is it for? At what circumstances should I make or not make one?
Quoting the documentation
The view definition is “frozen” at creation time and is not affected by subsequent changes to the definitions of the underlying tables.
I don't see how creating a view would be beneficial, so please, enlighten me.
View the data without storing the data into the object.
Restrict the view of a table i.e. can hide some of columns in the tables.
Join two or more tables and show it as one object to user.
Restrict the access of a table so that nobody can insert the rows into the table.
Where I work, we use the views as big painful querys that we need many times, we decided to create views for them instead of doing a manual query and when we need to access the information
SELECT * FROM view WHERE (anything)

If we make any change in view of a table, will it change the actual table data?

If we make any change in view of a table, will it change the actual table?
If we do update or delete on a view of table will it change the value of the actual table?
Yes. It will. Depending on the view it will fail or not. Usually views with joins will fail since it doesn't know how to change it and in what order.
If it's just a single table, non-aggregated view it will always be able to insert/update/delete.
As Nicholas Krasnov commented:
If it's a key preserved "view with joins" it will still allow for a base table (row appears at most once) be updatable.
Yes, it will (for non read only view).
If you create the view with READ ONLY option, then you can not change it.
The simple conclusion is:
If you could make change to a view, then it will change the actual table, if the view is not updatable, then you can't make change to it.

Create columns that autoincrement with name in a MySQL Database

I know that this might seem like a strange question, but let me try and explain it. I have a database table called 'plan' and in it the first column is called 'username' and the columns after it are called 'question1', 'question2' and so on. I now need to add a hundred or so more columns named like this, but it would be nice to have a sql statement that would automatically do that for me.
I know this wasn't set up in the best way, but if you have a solution, please let me know :)
There isn't any SQL command or feature that would do this automatically; sure you can generate the alter table statements and add the columns programmatically; however, your design would be terribly flawed.
Instead of adding columns, you should create a table containing the question, the user_id (or username, whatever is the PK) to hold the records. If you need to identify a question by number (or ID), simply add another column called question_id.
Write the query in sql to excel. Seperate the incrementing number. Drag down until excel row 100. Hard to explain but i guess you ll figure it out. You'll have 100 incrementing add column sql statements. copy paste run it on a query tool.

mysql don't return results if not from statement but from INDEX table or something

I think my question was a little confusing.....It confused me :)
Working on a media site as a take-over project and it has a custom CMS. The client wants the ability to activate/deactivate media....sort of like Wordpress's publish/unpublish feature.
Instead of digging through all the code looking for mysql queries (which I'm not opposed to), I was wondering if you can add a sort of INDEX to a table that won't let it return result rows if that rows "active" column = let's say 0.
Just trying to be lazy and learn something at the same time, heh.
I don't need examples of queries to make it happen, btw.
What you describe is called a "view". Here is a page describing how to create them in MySQL: http://dev.mysql.com/doc/refman/5.0/en/create-view.html. However, in most cases you will still have to alter your code to use the view instead of the table.
You can consider create a view (which contains active record only)
AND swap the view name to actual table name instead, so you can achieve the negative filtering without changing any of your source code.

How to delete from a database?

I know of two ways to delete data from a database table
DELETE it forever
Use a flag like isActive/isDeleted
Now the problem with isActive is that I have to track everywhere in my SQL queries that whether the record is active or not. Using DELETE however gets rid of the data forever.
What would be the best way to backup this data?
Assuming I have multiple tables in a database, should I have a common function which just backs everything up and stores it in another table (in XML probably?) or is there any other way.
I am using MySQL but am curious about techniques used in other DBs as well.
Replace the table with a view that hides the inactive items.
Or write a trigger on DELETE that backs up the row to an archive table.
You could use a trigger that fires on deleting records to back them up into some kind of graveyard table.
You could use an isDeleted column and defien a view which selects all columns except isDeleted with the condition isDeleted=false. Then have all your stps work only with the view.
You could maintain a history table, where you back the record up and time stamp
One of the biggest reasons for not deleting data is that it may be required for a relation - for example the the user may decide to delete an old customer from the database, but you still need the customer record because it is referenced by old invoices (which may have a much longer lifespan).
Based on this the best solution is often the "IsDeleted" type of column, combined with a view (Quassnoi has mentioned partitioning, which can help with performance issues that might pop up due to a lot of invisible data).
You can partition your tables on the DELETED column and define the views which would include the condition:
… AND deleted = 0
This will make the queries over the active data just as simple and efficient.
Well, if you were using SqlServer you can use triggers, which will allow you to move the record to a deleted table.