I want to create a table in mysql as:
create table interest(Lend float,
year int,
rate float,
interest_accumulated float);
Now, here in the above table I need to fill up the lend amount (principle), and time (year) and rate of interest and I need to get interest accumulated automatically filled up using the formula
I = lend(multiplied)year(multiplied)rate/100
without typing in the value to the table.
Also I need to protect the integrity by not allowing manual entry to the interest_accumulated column. And I want the interest_accumulated column to be in the same table ie "tablename" interest.
Is it possible to define such a format in mysql version 5.6.30-1(debian) system?
What you want to use is a virtual/generated column.
You can read about details here.
Edit:
However it's not available in mysql 5.6. If upgrade is not an option, you can have a 'before insert' trigger, that would calculate the value on insert. Guess 'before update' would be needed to avoid manual changes.
Related
I am trying to make a database for hospital visits and I want to alter the "Visit" table and add a new column which shows the total cost for the patients medication (by multiplying charge(from table "medication")*quantity(from table "getsmed")).
However, I wrote this code in MySQL workbench but it won't run and would under line the world "as" with the caption ("as" is not valid in this position, expecting: BIT, BOOL, BOOLEAN, DATETIME, TIME, ENUM...)
alter table visit
add total_charge as (medication.Mcharge*getsmed.Quantity)
;
In MySQL, you need to specify a type when adding a computed column:
alter table visit
add total_charge decimal(20, 4) as (Mcharge * Quantity);
However, a computed column can only directly reference columns values in the same row. It cannot "reach out" to other tables. This gives you two choices:
Use a user-defined function to retrieve a value from another table.
Use a view rather than a computed column.
I would recommend the second solution.
I can't actually suggest any specific code, because you have not provided enough information in the question.
I have a table that is used to store the latest actions the user did (like a ctrl+z for the program), but I want to limit this table to about 200 entries, and after that, every new entry would delete the oldest in the table.
Is there any option to make the table behave this way on SQL or do I need to add some code to the program to do it?
I've seen this kind of idea before, but I've rarely seen a case where it was a good idea.
Your table would need these columns in addition to columns for the normal data.
A column of type integer to hold the row number.
A column of type timestamp (standard SQL timestamp) to hold the time of the last update.
The normal approach to limit this table to 200 rows would be to add a check constraint to the column of row numbers. For example, CHECK (row_num between 1 and 200). MySQL doesn't enforce check constraints, so instead you'll need to use a foreign key reference to a table of row numbers (1 to 200).
All insert statements will need to determine whether the table is full, examine the time of the last update, and either a) insert a new row with a new row number, or b) delete the oldest row or overwrite it.
My advice? Renegotiate this requirement.
Assuming that "200" is not a hard limit, in other words if the number of entries occasionally went over that by a small amount it would be OK...
Don't do the pruning on line, do it as an off line process, run as often as needed to keep the totals per user from not getting "too high".
For example, one such solution would be to fire the SQL that does that query every hour using crontab.
Assuming I have the following table named "contacts":
id|name|age
1|John|5
2|Amy|2
3|Eric|6
Is there some easy way to check whether or not this table changes much like how a sha/md5 hash works when getting the checksum for a file on your computer?
So for example, if a new row was added to this table, or if a value was changed within the table, the "hash" or some generated value shows that the table has changed.
If there is no direct mechanism, what is the best way to do this (could be some arbirary hash mechanism, as long as the method puts emphasis on performance and minimizing latency)? Could it be applied to multiple tables?
There is no direct mechanism to get that information through SQL.
You could consider adding an additional LastModified column to each row. To know the last time the table was modified, select the maximum value for that column.
You could achieve a similar outcome by using a trigger on the table for INSERT, UPDATE and DELETE, which updates a separate table with the last modified timestamp.
If you want to know if something has changed, you need something to compare. For example a date. You can add a table with two columns, the tablename and the timestamp, and program a trigger for the events on the table you are interested to control, so this trigger will update the timestamp column of this control table.
If the table isn't too big, you could take a copy of the entire table. When you want to check for changes, you can then query the old vs. new data.
drop table backup_table_name;
CREATE TABLE backup_table_name LIKE table_name;
INSERT INTO backup_table_name SELECT * FROM `table_name`;
I have a table that one of this record has been changed.
Is there any MySQL function to get the date of this updating?
No. There is no way to get that modification date unless you explicitly stored it somewhere else (through a trigger or application side technique).
Well it depends on how you are using the data value.
but mostly it is a good practice to have both created_at and updated_at as attributes for your table.
OR if you want to keep each update date info for a record then store them in different table. before each update in main table insert existing row in tracking table from main table.
I am currently working on a web service that stores and displays money currency data.
I have two MySQL tables, CurrencyTable and CurrencyValueTable.
The CurrencyTable holds the names of the currencies as well as their description and so forth, like so:
CREATE TABLE CurrencyTable ( name VARCHAR(20), description TEXT, .... );
The CurrencyValueTable holds the values of the currencies during the day - a new value is inserted every 2 minutes when the market is open. The table looks like this:
CREATE TABLE CurrencyValueTable ( currency_name VARCHAR(20), value FLOAT, 'datetime' DATETIME, ....);
I have two questions regarding this design:
1) I have more than 200 currencies. Is it better to have a separate CurrencyValueTable for each currency or hold them all in one table?
2) I need to be able to show the current (latest) value of the currency. Is it better to just insert such a field to the CurrencyTable and update it every two minutes or is it better to use a statement like:
SELECT value FROM CurrencyValueTable ORDER BY 'datetime' DESC LIMIT 1
The second option seems slower.. I am leaning towards the first one (which is also easier to implement).
Any input would be greatly appreciated!!
p.s. - please ignore SQL syntax / other errors, I typed it off the top of my head..
Thanks!
To your questions:
I would use one table. Especially if you need to report on or compare data from multiple currencies, it will be incredibly improved by sticking to one table.
If you don't have a need to track the history of each currency's value, then go ahead and just update a single value -- but in that case, why even have a separate table? You can just add "latest value" as a field in the currency table and update it there. If you do need to track history, then you will need the two tables and the SQL you posted will work.
As an aside, instead of FLOAT I would use DECIMAL(10,2). After MySQL 5.0, this will actually have improved results when it comes to currency handling with rounding.
It is better to have one table holding all currencies
If there is need for historical prices, then the table needs to hold them. A reasonable compromise in many situations is to split the price table into a full list of historical prices and another table which only has the current prices.
Using data type float can be troublesome. Please be sure you know what you are doing. If not, use a database currency data type.
As your webservice is transactional it is better if you'd have to access less tables at the same time. Since you will be reading and writing a lot, I would suggest having a single table.
Its better to insert a field to the CurrencyTable and update it rather than hitting two tables for a single request.