error with "as" when creating a computed column - mysql

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.

Related

Generating a big range of numbers in MySQL

How do I generate a range of numbers in one column in MySQL? I'm looking for any soluton to make numbers range that starts from 500000000 and ends on 889999999.
It seems that may want to use an AUTO_INCREMENT in the column value you want. You can set the starting value to the one you desire in this way.
Also, you can only have one AUTO_INCREMENT column in a given table.
CREATE TABLE your_table (
column_1 INT NOT NULL AUTO_INCREMENT = 500000000
--Add other columns
)
If you already have a table with the AUTO_INCREMENT column, just set the value to the one you want.
ALTER TABLE your_table AUTO_INCREMENT = 500000000;
If what you want is to insert rows with those numbers, use a loop.
Just for fun, generate the range in a text file, by any means available.
Unload to a text file.
Load that text file to your table. You don not say if you are constrained by how long this takes. It sounds like you just want a table of a single column of INT with lower and upper limits.
MySQL should just handle these numbers,this is not really a "big" range, seriously.
Do you want to constrain the values in the column to
{500000000..889999999}?
Or do you want to know how to define a column
to hold these values?
Do you want a written procedure to generate
these numbers for you?
Do you want us to size this for you?
Do you want us to write a script or program to load these?
All of these answers are available with minimal sweat. Keywords are MySQL,Integer, Types.
We cannot see your problem because your question does not describe a problem.
Tell us what you tried, and tell us what happened...
Otherwise just add them, you are still in INT territory (-2Gi..2Gi), not BIGINT yet.
Switch to MariaDB, then JOIN to a pseudo-table called seq_500000000_to_889999999.

Relating multiple columns in same table

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.

CREATE TABLE once but INSERT INTO twice

I have tried repeatedly to use a combination of "CREATE TABLE" and "ALTER TABLE...ADD" and "UPDATE..SET...WHERE" to produce an online survey which has two parts. Part one you add some user input and part two is just the same -- you input some values. It has become clear to me that, for whatever reason, when I use the above combination of SQL statements, the user input from the second portion of the questionnaire does NOT get fed into MySQL. The fields for the second part of the survey remain BLANK. I have literally tried it dozens of times. Failing everything else, I decided to CREATE TABLE with ALL the input text fields (in other words, I did not just the use the text fields from the first portion and then use ALTER TABLE to add columns from the second portion -- no -- as I mentioned, this does NOT work). What I tried instead was, on my first PHP file, I had:
$sql = "CREATE TABLE hollywoodmovies (ID INT(10), Year INT(10),
LeadActor VARCHAR(60), StudioName VARCHAR(20), PRIMARY KEY(ID))";
And then the second PHP file deals with the online survey part one:
$sql = "INSERT INTO hollywoodmovies (ID, Year, LeadActor, StudioName)
VALUES ('$uniqueid', '$year', '$actorname', '$studiolist')";
This is despite the fact that online survey part one has only two form fields: ID and YEAR
Now, the second portion of the survey, I did EXACTLY the same as the above:
$sql = "INSERT INTO hollywoodmovies (ID, Year, LeadActor, StudioName)
VALUES ('$uniqueid', '$year', '$actorname', '$studiolist')";
And this is despite the fact that I again have only two form fields for the second portion of the questionnaire/survey: LEADACTOR and STUDIONAME.
What is happening at run-time is that ALL desired fields ARE being allocated their respective values -- unlike when I used ALTER TABLE and UPDATE, at which time the input from the second portion of the survey went missing. So this is good -- up to a point. What is happening, however, is that I am getting TWO records (rows), not one. In the first record, the first two columns have user input appearing in them, but the last two are blank. And in the second row, the first two columns are blank, but the last two columns have data. Is there ANY way I can have just one record, and not two? (Would it be possible to use the word NULL somewhere to make the blanks disappear, giving me just one record?) Remember, my particular SQL server is VERY troublesome -- I cannot use ALTER or UPDATE or whatever else because my server just doesn't like it. I can only use CREATE in combination with "INSERT INTO...VALUES". Ay ideas, please?
INSERT will certainly create 2 rows, especially if you don't have a PK restraing on the ID column and are using that in both INSERT statements.
on your second statement to populate LEADACTOR and STUDIONAME do an UPDATE
e.g. UPDATE hollywoodmovies SET StudioName = $studioname, LeadActor = $leadactor WHERE ID = $uniqueid

How to best check if a SQL table contents have not changed?

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`;

SQL query to copy table with specified columns

I've a table that 'll populate columns dynamically like col1,col2,col3... at runtime and i'm copying this table into another table having columns col1,col2,col3,col4,col5 more than this i.e. maximum number of columns it support. But currently when ever i copied dynamic generated table into current table having max columns it giving me error like
Dynamic table columns:
DateInterval, DataType, Seqno, Channel1_data, Channel1_status, Channel2_data, Channel2_status
Table columns used for copying dynamic table:
DateInterval, DataType, Seqno, Channel1_data, Channel1_status, Channel2_data, Channel2_status, Channel3_data, Channel3_status, Channel4_data, Channel4_status
Query:
SELECT DateInterval, DataType, Seqno, Channel1_data, Channel1_status, Channel2_data, Channel2_status, Channel3_data, Channel3_status, Channel4_data, Channel4_status
FROM #TableName
'No value given for one or more required parameters'
Tell me how can i overcome this problem.
Thanks,
#nag
Nag review this posting: Is it ever okay to violate the first normal form
In this posting you will find a way i solved a problem where i needed a variable number of fields, that would grow and shring over time, in a table. It has minimal internal storage while still allowing for enough growth room for my need if the criteria was met.