how create VIEW for local work, without update in TABLE? - php

How to create a VIEW that it does not update data in a TABLE? For example, I want to delete rows only in the VIEW and the TABLE, on which the established VIEW, that there should be no deletions ... how to do it?
like this does not work ...
"CREATE OR REPLACE VIEW $prefix"
. "Test_View AS SELECT * FROM Table1 WITH LOCAL CHECK OPTION";
it is rather to check ... please, help!

If you want to have a VIEW where you can't update/delete data, add a distinct (e.g. on a unique key, where it essentially doesn't have an effect).
See the MySQL Ref on updatable views:
There are also certain other constructs that make a view nonupdatable. To be more specific, a view is not updatable if it contains any of the following:
Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth)
DISTINCT
GROUP BY
...

Deleting Rows from a view will delete them from the corresponding table.
You really should alter your view definition to exclude the rows you do not want.

You don't want to be using a view for this. You would need a second table that is updated by a on insert / on update trigger, if you want the table to exist permanently. Otherwise just create a temporary table for each query by using INSERT..SELECT statements.

Related

Create single line SQL entry from multi line query and post it into a new table

I have a SQL table_1 with the following data:
There are multiple entries with and the same MSISDN gets repeated, but with different servicCodes in each line.
I would like to copy the info into a new table_2, but with only one entry per MSISDN and some extra columns with the services assigned to it. Can this be done with a single SQL query, or is it be best to use PHP to do a distinct query and loop through the results?
Thanks.
With SQL you can create a second table with a column with the servicCodes concatenated. You can use the GROUP BY to aggregate around the MSISDN column.
SELECT MSISDN, GROUP_CONCAT(servicCodes) as servicCodes_concatenated
FROM table
GROUP BY MSISDN
If you want to create multiple columns to have the values of servicCodes it's easier to use PHP.
Use INSERT ... SELECT syntax so you can filter and modify rows in single query. https://dev.mysql.com/doc/refman/5.7/en/insert-select.html
I'd guess what you're looking for is called a Pivot Table.
See mysql pivot table
Everything is most probably said there, so just one comment on them:
As you get dynamic column names per definition it's hard(er) to deal with those columns lateron. Thus you most probably need some more sophisticated procedures to make further/other use of that data than just reporting it.

mysql update view automatically when related table structure changed ?

I have table _house with field 'soft_delete' default is 0.
Then I have a View to check undeleted entries, hence I have
CREATE VIEW house AS
SELECT * FROM _house where soft_delete = 0;
But the problem now is that everytime I modify table _house, I'll need to re-update my view so that is not broken.
So each time after modify table _house, I execute
ALTER VIEW house AS
SELECT * FROM _house where soft_delete = 0;
I wanted to find an easier way to execute above alter script, so I tried to create a procedure/function with 'alter view' inside, but mysql seems to prohibit me doing that.
Question :
Any other solution to simplify this 'redundant' actions ?
The view definition is “frozen” at creation time, so changes to the underlying tables afterward do not affect the view definition. For example, if a view is defined as SELECT * on a table, new columns added to the table later do not become part of the view.
SOURCE

Proper Indexing MySQL

As far as indexing goes, is it proper to index all fields that will be searched (within a WHERE) clause to speed up SELECTS? For example my database contains a profiles table which stores user information such as name, intrestCode,zip, description, and email. The profile record is identified by a PRIMARY id column which uniquely corresponds with the userid. I made zip and intrestCode an index since profiles will be searched by zip and possibly intrestCode (SELECT `blah`,`blah`... FROM profile WHERE zip=?, SELECT `blah`,`blah`... FROM profile WHERE zip=? && intrestCode=?). Am I doing it right?
Sounds basically correct. You should be aware that you can't use two different indices on the same table in the same query, so if you ran
CREATE INDEX `zip` ON `profile` (`zip`);
CREATE INDEX `intrestCode` ON `profile` (`intrestCode`);
then the query
SELECT `blah`,`blah`... FROM profile WHERE zip=? && intrestCode=?
can only look up one table from the index. The secret here is that you can create a single index on two tables, like so:
CREATE INDEX `zip+intrestCode` ON `profile` (`zip`, `intrestCode`);
MySQL can use this for queries that use either zip alone in the WHERE clause, or use both zip and intrestCode, but not for queries that use only intrestCode in the WHERE clause.
(This is because each index covers the whole table. If MySQL were to try and look up zip and intrestCode from different tables, then it would be retrieving lots of irrelevant rows from the second index. Therefore, it only looks at one index. If you want it to use the index on both columns, you need to have one index that includes both columns.)
Indexing relation datbases is a bit of an art. The best approach is to look at all the different queries you will be making and putting indices on the columns involved. Use EXPLAIN to see what indices a query is going to use.
However, remember that MySQL can only use one index from a table at a time. This is why you can put an index on multiple columns at once. And MySQL can use a multi-column index if it only needs the columns in the start of the index. In your example, I would put a multi-column index zip and intrestCode together because that will help both queries. That way you don't need a separate index on just zip.
I believe basically yes. You should monitor your database to encounter further problems and optimize your queries and tables based on the results too.
Generally you want to add indexes to columns that you will join on or have in your where clause. Remember that an index on two columns is different than one index on each of the two columns. Also if you have an index on multiple columns the order matters.
Say you have two columns A and B and have an two column index in the order A then B. There are three cases:
Where clause against column A: index is used
Where clause against column B: index is NOT used
Where clause against columns A and B: index is used
Like staticsan says, indexing is an art and there are no rules that apply 100% of the time. Use the explain plan to see how your query is performing and make tweaks accordingly.

Checking if MySQL Tables Exist and if not create them

What would be the best way to check if tables exist in a MySQL DB? I was looking at a few code examples and have seen it done a couple of ways.
What I am trying to do is create tables like tag_tagnamehere so each tag has their own table with a link to the post/page ID.
So what I'm thinking is when people inset a list of TAGS I would loop through them and if the table is not found, create it and make and entry to that post/page ID.
CREATE TABLE IF NOT EXISTS
Maybe I didn't understand your design/table structure, although each tag shouldn't reside in its own table. You should have a table of tags, and a normalised scenario where other tables (if need be) define other tag attributes
Create table has that built in.
Create table Foo if not exists
But you probably shouldn't do this. Note how its done here with a PostTag table
CREATE TABLE IF NOT EXISTS <table_name>...
How about directly from a MySQL Query?
CREATE TABLE someTable IF NOT EXISTS

php do something for every record in the database

I have two tables in the database(videos and viewData) .
Im trying to build a script that runs for each record in the "videos" table and does something using the "videoID" field for that specific entry in the "videos" table. The does something part would be dumping some data into the viewData table.
Would I need to store all the records in an array before calling the loop? An example of a loop like this would be really helpful. Also in a way that could be potentially scalable that wouldn't hurt the server too much if there were a 1000+ records in the "videos" table.
Thanks,
Dave
Try to avoid the loop at all costs. Think set based processing, which means handle the entire set of rows within one SQL command.
I'm not entirely sure what you are attempting to do, as your question is a little vague. however, here are two possibly ways to handle what you are trying to do using set based thinking.
You can do a JOIN in an UPDATE, essentially selecting from the parent table and UPDATEing the child table for all rows in a single UPDATE command.
UPDATE c
SET Col1=p.Col1
FROM ParentTable p
INNER JOIN ChildTable c On p.ParentID=c.ParentID
WHERE ...
you can also INSERT based on a SELECT, so you would create one row from each row returned in the SELECT, like:
INSERT INTO ChildTable
(Col1, Col2, Col3, Col4)
SELECT
p.ColA, p.ColB, 'constant value', p.ColC-p.ColD
FROM ParentTable p
WHERE...
Working with a database in the loop isn't a good practice. It is good to select all table data into an array by one query and work with this array in future.
Do you have access by other means to MySQL tables? Like with MySQL Administrator or another tool, even by command line?
This is because it would be much more time, resources and everything else, doing that directly in the database, through a query or a database function.
I would do that this way.
But for the sake of clarity, unless you are storing the videos themselves inside database tables, 1000 records are not a problem. Maybe 10,000 would be.
General tip: do just what you need to do.
If you only need to operate upon data, do this on the database.
If you only need to check one field in one table, use SELECT your_field FROM your_table instead of SELECT * FROM your_table.

Categories