expression behind generated column in MySQL - mysql

I just created a generated column 'gc1' of stored type in my table tbl1.
When I am running this command:
DESCRIBE tbl1;
It shows me the 'STORED GENERATED' for gc1 in the Extra field.
When I am checking triggers using SHOW TRIGGERS; command,
it is not showing me anything related to the generated column.
I am not able to get the expression used to generate the column gc1.
I tried the MySQL documentation as well but got nothing.
Any help would be highly appreciated.

Related

Save MySql 'Show' result in db

So I'm kind of stumped.
I have a MySql project that involves a database table that is being manipulated and altered by scripts on a regular basis. This isn't so unusual, but I need to automate a script to run (after hours, when changes aren't happening) that would save the result of the following:
SHOW CREATE TABLE [table-name];
This command generates the ready-to-run script that would create the (empty) table in it's current state.
In SqlWorkbench and Navicat it displays the result of this SHOW command in a field in a result set, as if it was the result of a SELECT statement.
Ideally, I want to take into a variable in a procedure, and change the table name; adding a '-mm-dd-yyyy' to end of it, so I could show the day-to-day changes in the table schema on an active server.
However, I can't seem to be able to do that. Unlike a Select result set, I can't use it like that. I can't get it in a variable, or save it to a temporary, or physical table or anything. I even tried to return this as a value in a function, from which I got the error that a function cannot return a result set - which explains why it's displayed like one in the db clients.
I suspect that this is a security thing in MySql? If so, I can totally understand why and see the dangers exposed to a hacker, but this isn't a public-facing box at all, and I have full root/admin access to it. Hopefully somebody has already tackled this problem before.
This is on MySql 8, btw.
[Edit] After my first initial comments, I need to add; I'm not concerned about the data with this question whatsoever, but rather just these schema changes.
What I'd really -like- to do is this:
SELECT `Create Table` FROM ( SHOW CREATE TABLE carts )
But this seems to be mixing apples and oranges, as SHOW and SELECT aren't created equal, although they both seem to return the same sort of object
You cannot do it in the MySQL stored procedure language.
https://dev.mysql.com/doc/refman/8.0/en/show.html says:
Many MySQL APIs (such as PHP) enable you to treat the result returned from a SHOW statement as you would a result set from a SELECT; see Chapter 29, Connectors and APIs, or your API documentation for more information. In addition, you can work in SQL with results from queries on tables in the INFORMATION_SCHEMA database, which you cannot easily do with results from SHOW statements. See Chapter 26, INFORMATION_SCHEMA Tables.
What is absent from this paragraph is any mention of treating the results of SHOW commands like the results of SELECT queries in other contexts. There is no support for setting a variable to the result of a SHOW command, or using INTO, or running SHOW in a subquery.
So you can capture the result returned by a SHOW command in a client programming language (Java, Python, PHP, etc.), and I suggest you do this.
In theory, all the information used by SHOW CREATE TABLE is accessible in the INFORMATION_SCHEMA tables (mostly TABLES and COLUMNS), but formatting a complete CREATE TABLE statement is a non-trivial exercise, and I wouldn't attempt it. For one thing, there are new features in every release of MySQL, e.g. new data types and table options, etc. So even if you could come up with the right query to produce this output, in a couple of years it would be out of date and it would be a thankless code maintenance chore to update it.
The closest solution I can think of, in pure MySQL, is to regularly clone the table structure (no data), like so:
CREATE TABLE backup_20220618 LIKE my_table;
As far as I know, to get your hands on the full explicit CREATE TABLE statement, as a string, would require the use of an external tool like mysqldump which was designed specifically for that purpose.

Modifying table entries from LibreOffice Base, possible?

I've successfully connected LibreOffice Base with MySQL data base server. I've tested if I modify my table from host (free hosting service on internet) then the changes are reflected when refreshing the table object in LO Base.
But my question is, can I modify DB table directly from LO Base? I guess that it's possible using sql queries from LO Base, but how? Please give me some insights or tutorials. Thanks.
The normal way to alter a table:
Tools -> SQL
Enter an ALTER TABLE command and press Execute button.
A way that works, even though it complains that no result set is returned:
Create a query in SQL view.
Enter ALTER TABLE command.
Click button in toolbar to mark it as Run SQL command directly. Or Edit -> Run SQL command directly.
Close the query and double-click to run it.
My guess is it could be done with a macro as well, similar to https://forum.openoffice.org/en/forum/viewtopic.php?f=5&t=75763 but using ALTER TABLE.
For more ideas see https://forum.openoffice.org/en/forum/viewtopic.php?f=61&t=37687.
EDIT:
Inserting new row data in a form is easier than altering the table. First, make sure this works:
Double-click on your table under Tables.
Insert -> Record, or enter data in the last new row.
If Insert -> Record is disabled, then you need to set up the table for editing. Make sure that your connection to the database allows editing. Also the table must have a primary key.
Once you can insert records in Table view, it's time to create the form:
Under Forms, Use Wizard to Create Form.
Select your table and press >> to include all fields.
Click Finish.
Now you should be able to open the form and enter data into the final new row.
More complete instructions with examples are at http://www.open-of-course.org/courses/mod/url/view.php?id=786.

How to build a database schema in sql fiddle?

I tried using the text dll but i'm getting this message"
Unable to find consistent column separator in header row
I viewed their sample file but its not clear how to create and insert values in the database
Is it k to copy our create code in text dll box...
can any one help me....
I have found that when getting data out of SQL server, if you have any kind of convert or cast functions in your select statement and don't specify a column name then this error is triggered when pasting data into SQL fiddle text to DDL. (No column name) will show up unless you alias your column name after the convert/cast by using:
AS 'Your Column Name'
Example:
CONVERT(date, Dt_Approv) AS Dt_Approv
This gets rid of the (No column name) in the DDL that causes the error. It is at least something to try, it worked for me.
EDIT:
Not sure if I answered you the way you were hoping. To use the text to DDL tool on SQL Fiddle, all you have to do is paste in the tabular data from your databse or spreadsheet. From that the program will build the DDL, hence text to DDL :)
No need for create or insert statements when using text to DDL, it will write them for you.

When saving to a model, created and modified aren't automatically populated by CakePHP. Using SQL Server

Hi when saving to a model, my created and modified fields aren't automatically populated by CakePHP. It was automatically populated when I was using MySQL but now it isn't. I'm not using NOW() back when I was still using MySQL. Why is it? Also when a field's value is not set 'NULL' (with quotes) is inserted causing errors because SQL Server says I can't insert a string to a field of type smallint/date etc. How do I fix this?
Thanks in advance!
I would set NULL as a keyword rather than quoting it, which I imagine is why your database thinks that it's a string.
Have you double checked the schema of the database to ensure that the created and modified fields are still DATETIME fields.
Also you say "SQL Server", and mention MySQL, so I assume that you are now using MSSQL?

Any way to view ALTER TABLE after its been executed? -MySQL

In the same way SHOW CREATE TABLE tblname; brings back what was previously executed, is there anyway to view the SQL of an ALTER TABLE query?
Please help?
One small clarification: show create table does not actually "bring back what was previously executed". It just shows you the DDL that would create the table from scratch. The table may have been created and then altered many times, but show create table reflects the current state of the table.
As for finding any alter table statements that ran on the table recently, the best bet is the binary log.
First check to see if binary logging is enabled:
show variable like 'log_bin';
If it is, find the binary log for the relevant time period, use mysqlbinlog to convert it to SQL, then grep for the relevant table name to find the alter table statement you are looking for.
Tools:
Maatkit.
Red-Gate's MySQL Schema & Data Compare
Toad
SQLYog
MySQL Diff
Manual:
First check to see if binary logging is enabled:
show variable like 'log_bin';
Then,
mysqlbinlog /var/log/mysql/mysql-bin.000001 | grep 'alter table *tablename*' > test.file
Go to test.file to see the alter statements.
If your MySQL instance has logging turned on, you can view the logs. This would be the best option.
If your system has history turned on, you can start a client back up from the same system and try the up arrow. You might be able to see the command there.
If you know the user that ran the command and they happened to run it directly from a command line, the same history trick might work.