How to pre-set MySQL user variables in config files - mysql

MySQL allows usage of User-defined variables How to declare a variable in MySQL?
Is there a way for me to pre-set one such constant, so that it's automatically ready to use in PHP scripts, CLI clients and whatnot? As if every session were automatically started with a
SET #whatever = 2.52;

Create a table, that keeps this value and select from it everywhere, when you need it. That will be a simple subselect in sql queries.

Related

Where does user-defined variables will be stored into mysql 8.0.16?

Suppose I defined two variables in mysql as follows:
SET #STUDENT_NAME = 'ABCD';
SET #STUDENT_AGE = 25;
Now, When I'm was selecting these two variables value using following:
SELECT #STUDENT_NAME, #STUDENT_AGE;
It was perfectly showing corresponding values, even if I switched the database.
But when I restarted mysql and tried to select these values, It was showing null values of these variables, So where would it be stored actually when I had set values?
Per the manual (emphasis mine):
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html
User-defined variables are session specific. A user variable defined by one client cannot be seen or used by other clients. (Exception: A user with access to the Performance Schema user_variables_by_thread table can see all user variables for all sessions.) All variables for a given client session are automatically freed when that client exits.

How to set mysql user variable at every new connection in Laravel 5.5?

Background: We rely heavily on views in our app and one of the ways in which we are looking to optimize them is by parameterization. We've got that aspect all figured out, but in order to make it work, I must be able to set a user parameter for the customer (tenant) id. I want to do this at a base level so that it is set for every single connection.
For example the script I want to run is simply:
Set #Param_CustomerId:=1234
where 1234 is the customer id based on the authentication.
Here's the question: Where can I set up code that will get run every time a new MySQL Connection is established?
I think you can run that code in the registerConnectionServices method of a class which would extend the default DatabaseServiceProvider (and use that extended provider instead of the default).

mysql define variable across all sessions

I have a table with a few variables (let's say tbl_param). I have to query tbl_param everytime I need to use one of them. I have a case where I need to use those variables inside triggers, so I have to query tbl_param each time those triggers are executed. But what if I am going to use the same variable for every user that connects to the db? It would be logical to set them only once, since it would not change often (only when the variable in question gets updated in tbl_param). I know I can set session variables, but it would not solve the problem as they would be acessible only for the duration of one connection. So when a new connection is made, I would need to query tbl_param again. Can I define, for instance, a new system variable that gets loaded when the server boots up and and that I could update it as tbl_param gets updated? Or is there another alternative?
There are system that can be defined in mysql.cnf (or mysql.ini) file; but this will require you to had file permissions on that file. On my local server (Ubuntu 20.04.2) it is in /etc/mysql/mariadb.conf.d/50-server.cnf. but this would not work on remote server; because we didn't have access to system files (etc).
I had found an alternative to this that would serve the purpose what you had in mind. SET a session variable (wait for it; i know session variables disappears in other sessions); but initialize it value to some value from a table. e.g initialize your session variable always on startup from a table (and update accordingly to table as required).
In case of using a PHP (MIS) to disable mysql triggers
To disable a trigger on some table for some specific record(s). instead of deleting the triggers and inserting the records and then recreating those triggers. just rewrite the trigger with a minor change. it would disable based on this session variable.
Then your MIS would always initialize a session variable to some value fetched from table. and based on this value skip or execute triggers.

MySQL audit trail using triggers and mysql session variables for web user_id. Good solution?

If I do an audit trail using triggers in MySQL, and I use MySQL session variables to store the PHP variables I need to store, something like:
SET #user_id = $user_id
SET #user_ip = $user_ip_address
and then use that information in the trigger, will that work? Is that a good solution?
what I fear is that if multiple users are making queries at the same time the #user_id session variable could change before the trigger is executed and it would be stored in the database as if the other user made the changes.
Can it happen?
So far as I know, variables are session-only, so you could have a thousand sessions using the same variable name without there being overlap.

Return variable number of columns in SQL Server 2008

My table contains the following fields, Name,Age,Salary,Phone,DOB. Based on a settings table, I have to select only some fields. For example, I say in settings, only Name and Phone is required. How can I do it using stored procedure ?
EDIT :
Which one is good.
Select the required fields from the table.
Select all columns and in ASP.NET page, use .Visibility property to hide or show columns
SQL is a fixed column language: columns can not be added or removed "on the fly"
You would need to use dynamic SQL to build a SELECT statement, or use IF statements to execute different ones. However, you open up caching, security and injection issues.
Personally, I'd ignore columns in the client code and have a simple, single efficient SQL query. The contract or API between SQL Server and the client should be static and predictable. If the settings table is applied in SQL Server, your client doesn't know what columns to expect. If your client does know, then it can ignore them.
After your edit, option 2, kind of.
But the data should be removed before being rendered in the page.
Keep it simple: don't try to optimise anything yet
You would need to have multiple different selects - based on your settings table - in your stored proc to return the different sets of data.
CREATE PROCEDURE dbo.YourProcedure(...)
AS BEGIN
DECLARE #Setting INT -- ?? whatever it is
SELECT #Setting = Choice FROM dbo.YourSettingsTable WHERE ....... ???
IF #Setting = 1
SELECT Name, Phone
FROM dbo.YourDataTable
ELSE
SELECT Name, Age, DOB, Phone, Salary
FROM dbo.YourDataTable
END
Using this approach, however, has its dangers - since the stored proc might return one set of data or quite another, your SQL Server query optimizer might make a very good decision on how to access the data for one setting - but when your setting changes, that execution plan will be totally outdated, thus potentially leading to horrible performance......
On the other hand - it might be easier to determine that setting before calling your stored proc - and then just pass in that setting as a stored proc parameter.
Or even better yet: have separate stored procs for each "scenario" - and then from the caller, call the appropriate stored proc depending on the value of your setting....
Create the sql you want dynamically then execute it with exec.
declare #sql varchar(500)
set #sql = 'select 123'
exec (#sql)
The above code should help you understand what you need to know.
Have a stored procedure for each set of fields you want to select
Allow the list of field names to be passed in as a parameter