简体   繁体   中英

MySQL - Can I prevent a row from being SELECTed?

I have a table that has a lot of different queries going to it.

I want to add a feature that will allow me to essentially disable specified rows, so that they do not show up anywhere on the website.

Now, the obvious solution is to create a new column and add " AND ACTIVE=TRUE " to every SELECT query on the website. However, there are so many different SELECT queries, that it would be very unpractical to hunt down every single one. I would be bound to miss some.

Is there a more practical way to disable rows?

Consider creating a view which selects all columns from your table, but applies the condition ACTIVE = TRUE after you have created the ACTIVE column as you proposed in your question. If you rename the original table and create the view with the table's former name, you may continue using it in your application seamlessly with the new limiting condition applied.

CREATE VIEW the_table_name AS (
  SELECT
    col1,
    col2,
    col3
  FROM the_renamed_table
  WHERE ACTIVE = TRUE
);

If performance doesn't seem to scale, you may need to add an index on the ACTIVE column.

There is no way in MySQL to set access on a row-level. You will have to do it in PHP. If you are using a database abstraction layer, such as Doctrine, there are ways to implement such functionality quickly. If all of your queries are written directly in the code, you are out of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM